Skip to content

Commit

Permalink
Added for 18 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 18, 2024
1 parent 05cc131 commit 8ef737f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
98 changes: 98 additions & 0 deletions GeeksForGeeks/18-1-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/18-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * logn)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/18-1-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)
19 changes: 19 additions & 0 deletions LeetCode/18-1-24/Solution.java
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;
}
}

0 comments on commit 8ef737f

Please sign in to comment.