diff --git a/GeeksForGeeks/18-1-24/GFG.java b/GeeksForGeeks/18-1-24/GFG.java new file mode 100644 index 0000000..7478e75 --- /dev/null +++ b/GeeksForGeeks/18-1-24/GFG.java @@ -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 V = new ArrayList(); + + // 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() { + @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; + } +} diff --git a/GeeksForGeeks/18-1-24/README.md b/GeeksForGeeks/18-1-24/README.md new file mode 100644 index 0000000..fbf4193 --- /dev/null +++ b/GeeksForGeeks/18-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n * logn) +Space complexity - O(n) diff --git a/LeetCode/18-1-24/README.md b/LeetCode/18-1-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/18-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/18-1-24/Solution.java b/LeetCode/18-1-24/Solution.java new file mode 100644 index 0000000..35e0d08 --- /dev/null +++ b/LeetCode/18-1-24/Solution.java @@ -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; + } +}