From 8ef737f17273f15cef034f76c41827ae7d05de26 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Thu, 18 Jan 2024 15:50:57 +0530 Subject: [PATCH] Added for 18 Jan --- GeeksForGeeks/18-1-24/GFG.java | 98 +++++++++++++++++++++++++++++++++ GeeksForGeeks/18-1-24/README.md | 2 + LeetCode/18-1-24/README.md | 2 + LeetCode/18-1-24/Solution.java | 19 +++++++ 4 files changed, 121 insertions(+) create mode 100644 GeeksForGeeks/18-1-24/GFG.java create mode 100644 GeeksForGeeks/18-1-24/README.md create mode 100644 LeetCode/18-1-24/README.md create mode 100644 LeetCode/18-1-24/Solution.java 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; + } +}