-
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
af6ef3c
commit 6395761
Showing
4 changed files
with
106 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,66 @@ | ||
//{ Driver Code Starts | ||
// Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GfG { | ||
public static void main(String args[]) { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while (t-- > 0) { | ||
String S = sc.next(); | ||
int K = sc.nextInt(); | ||
Solution obj = new Solution(); | ||
System.out.println(obj.removeKdigits(S, K)); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
// User function Template for Java | ||
|
||
class Solution | ||
{ | ||
public static String removeKdigits(String S, int K) | ||
{ | ||
// code here | ||
if(S.length()==K) | ||
{ | ||
return "0"; | ||
} | ||
|
||
Stack<Character> st=new Stack(); | ||
int i=0; | ||
while(i<S.length()) | ||
{ | ||
while(K>0 && !st.isEmpty() && st.peek()>S.charAt(i)) | ||
{ | ||
st.pop(); | ||
K--; | ||
} | ||
|
||
st.push(S.charAt(i)); | ||
i++; | ||
} | ||
|
||
while(K-->0) | ||
{ | ||
st.pop(); | ||
} | ||
|
||
StringBuilder sb=new StringBuilder(); | ||
while(!st.isEmpty()) | ||
{ | ||
sb.append(st.pop()); | ||
} | ||
|
||
sb=sb.reverse(); | ||
while(sb.length()>1 && sb.charAt(0)=='0') | ||
sb.deleteCharAt(0); | ||
|
||
return sb.toString(); | ||
} | ||
} |
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,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,36 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution | ||
{ | ||
public int maxAncestorDiff(TreeNode root) | ||
{ | ||
return maxAncestorDiff(root, root.val, root.val); | ||
} | ||
|
||
// Returns |the maximum - the minimum| of the tree. | ||
private int maxAncestorDiff(TreeNode root, int min, int max) | ||
{ | ||
if (root == null) | ||
return 0; | ||
|
||
min = Math.min(min, root.val); | ||
max = Math.max(max, root.val); | ||
final int l = maxAncestorDiff(root.left, min, max); | ||
final int r = maxAncestorDiff(root.right, min, max); | ||
|
||
return Math.max(max - min, Math.max(l, r)); | ||
} | ||
} |