-
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
6a455cc
commit 230dc59
Showing
4 changed files
with
81 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,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; | ||
} | ||
} |
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^2) | ||
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(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,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); | ||
} | ||
} |