Skip to content

Commit

Permalink
Added codes for 5 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 5, 2024
1 parent 6a455cc commit 230dc59
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
57 changes: 57 additions & 0 deletions GeeksForGeeks/April/05-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//{ 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());
while(T-->0)
{
int n = Integer.parseInt(br.readLine().trim());
String s = br.readLine().trim();
String[] s1 = s.split(" ");
int[] nums = new int[n];
for(int i = 0; i < n; i++)
nums[i] = Integer.parseInt(s1[i]);
Solution ob = new Solution();
long ans = ob.min_operations(nums);
System.out.println(ans);
}
}
}

// } Driver Code Ends


//User function Template for Java

class Solution
{
public int min_operations(int []nums)
{
// Code here
int n = nums.length;
int dp[] = new int[n];
Arrays.fill(dp,1);
int maxi = 1;

for(int i = 1;i < n;i++)
{
for(int j = 0;j < i;j++)
{
if(nums[i]-nums[j]>=i-j)
{
dp[i] = Math.max(dp[i],dp[j]+1);
maxi = Math.max(maxi,dp[i]);
}
}
}

return n-maxi;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/05-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/April/05-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
20 changes: 20 additions & 0 deletions LeetCode/April/05-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution
{
public String makeGood(String s)
{
StringBuilder sb = new StringBuilder();

for (char c : s.toCharArray())
if (sb.length() > 0 && isBadPair(sb.charAt(sb.length() - 1), c))
sb.deleteCharAt(sb.length() - 1);
else
sb.append(c);

return sb.toString();
}

private boolean isBadPair(char a, char b)
{
return a != b && Character.toLowerCase(a) == Character.toLowerCase(b);
}
}

0 comments on commit 230dc59

Please sign in to comment.