-
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
05cc131
commit 8ef737f
Showing
4 changed files
with
121 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,98 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.util.*; | ||
import java.io.*; | ||
|
||
class GFG { | ||
public static void main (String[] args)throws IOException { | ||
|
||
BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); | ||
PrintWriter out=new PrintWriter(System.out); | ||
int t = Integer.parseInt(in.readLine().trim()); | ||
while(t-- > 0) | ||
{ | ||
int n = Integer.parseInt(in.readLine().trim()); | ||
String s[]=in.readLine().trim().split(" "); | ||
|
||
int gallery[] = new int[n]; | ||
for(int i=0; i<n; i++) | ||
gallery[i] = Integer.parseInt(s[i]); | ||
Solution T = new Solution(); | ||
out.println(T.min_sprinklers(gallery,n)); | ||
} | ||
out.close(); | ||
|
||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class pair | ||
{ | ||
int x; | ||
int y; | ||
pair(int x1, int y1) | ||
{ | ||
x = x1; | ||
y = y1; | ||
} | ||
} | ||
|
||
|
||
class Solution | ||
{ | ||
int min_sprinklers(int arr[], int N) | ||
{ | ||
ArrayList<pair> V = new ArrayList<pair>(); | ||
|
||
// Traverse the array arr[] | ||
for (int i = 0; i < N; i++) | ||
{ | ||
if (arr[i] > -1) | ||
{ | ||
V.add(new pair(i - arr[i], i + arr[i])); | ||
} | ||
} | ||
|
||
Collections.sort(V, new Comparator<pair>() { | ||
@Override public int compare(pair p1, pair p2) | ||
{ | ||
return p1.x - p2.x; | ||
}}); | ||
|
||
int maxRight = 0; | ||
int res = 0; | ||
int i = 0; | ||
|
||
while (maxRight < N) | ||
{ | ||
if (i == V.size() || V.get(i).x > maxRight) | ||
return -1; | ||
|
||
int currMax = V.get(i).y; | ||
|
||
while (i + 1 < V.size() && V.get(i + 1).x <= maxRight) | ||
{ | ||
i++; | ||
currMax = Math.max(currMax, V.get(i).y); | ||
} | ||
|
||
if (currMax < maxRight) | ||
return -1; | ||
|
||
res++; | ||
maxRight = currMax + 1; | ||
i++; | ||
} | ||
|
||
return res; | ||
} | ||
} |
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 * logn) | ||
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,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,19 @@ | ||
class Solution | ||
{ | ||
// using 1d DP, but actually not | ||
public int climbStairs(int n) | ||
{ | ||
// declaring two variables to store the count | ||
int prev = 1; | ||
int prev2 = 1; | ||
int curr = 0; | ||
// Running for loop to count all possible ways | ||
for (int i = 2; i <= n; i++) | ||
{ | ||
curr = prev + prev2; | ||
prev2 = prev; | ||
prev = curr; | ||
} | ||
return prev; | ||
} | ||
} |