Skip to content

Commit

Permalink
Added codes for 17 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 17, 2024
1 parent 56f77e7 commit ae08e07
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
58 changes: 58 additions & 0 deletions GeeksForGeeks/February/17-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//{ Driver Code Starts
//Initial Template for Java

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
public static void main(String[] args) throws IOException
{
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
int t =
Integer.parseInt(br.readLine().trim()); // Inputting the testcases
while(t-->0)
{
long n = Long.parseLong(br.readLine().trim());
long a[] = new long[(int)(n)];
// long getAnswer[] = new long[(int)(n)];
String inputLine[] = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Long.parseLong(inputLine[i]);
}

Solution obj = new Solution();
System.out.println(obj.countSub(a, n) ? 1:0);

}
}
}


// } Driver Code Ends


//User function Template for Java


class Solution
{
public boolean countSub(long arr[], long n)
{
// Your code goes here
for (int i = 0; i <= (n - 2) / 2; i++)
{
if (arr[2 * i + 1] > arr[i])
{
return false;
}

if (2 * i + 2 < n && arr[2 * i + 2] > arr[i])
{
return false;
}
}
return true;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/17-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/February/17-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * log(ladder))
Space complexity - O(n)
25 changes: 25 additions & 0 deletions LeetCode/February/17-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution
{
public int furthestBuilding(int[] heights, int bricks, int ladders)
{
Queue<Integer> minHeap = new PriorityQueue<>();

for (int i = 1; i < heights.length; ++i)
{
int diff = heights[i] - heights[i - 1];

if (diff <= 0)
continue;

minHeap.offer(diff);

if (minHeap.size() > ladders)
bricks -= minHeap.poll();

if (bricks < 0)
return i - 1;
}

return heights.length - 1;
}
}

0 comments on commit ae08e07

Please sign in to comment.