-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56f77e7
commit ae08e07
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n * log(ladder)) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |