Skip to content

Commit

Permalink
Added for 11 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 11, 2024
1 parent af6ef3c commit 6395761
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
66 changes: 66 additions & 0 deletions GeeksForGeeks/11-1-24/GFG.java
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();
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/11-1-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)
2 changes: 2 additions & 0 deletions LeetCode/11-1-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)
36 changes: 36 additions & 0 deletions LeetCode/11-1-24/Solution.java
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));
}
}

0 comments on commit 6395761

Please sign in to comment.