From 639576137ff80c55f584556552ff5c959052a9ed Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Thu, 11 Jan 2024 11:30:04 +0530 Subject: [PATCH] Added for 11 Jan --- GeeksForGeeks/11-1-24/GFG.java | 66 +++++++++++++++++++++++++++++++++ GeeksForGeeks/11-1-24/README.md | 2 + LeetCode/11-1-24/README.md | 2 + LeetCode/11-1-24/Solution.java | 36 ++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 GeeksForGeeks/11-1-24/GFG.java create mode 100644 GeeksForGeeks/11-1-24/README.md create mode 100644 LeetCode/11-1-24/README.md create mode 100644 LeetCode/11-1-24/Solution.java diff --git a/GeeksForGeeks/11-1-24/GFG.java b/GeeksForGeeks/11-1-24/GFG.java new file mode 100644 index 0000000..eb3c31c --- /dev/null +++ b/GeeksForGeeks/11-1-24/GFG.java @@ -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 st=new Stack(); + int i=0; + while(i0 && !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(); + } +} diff --git a/GeeksForGeeks/11-1-24/README.md b/GeeksForGeeks/11-1-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/GeeksForGeeks/11-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/11-1-24/README.md b/LeetCode/11-1-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/LeetCode/11-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/11-1-24/Solution.java b/LeetCode/11-1-24/Solution.java new file mode 100644 index 0000000..4e10642 --- /dev/null +++ b/LeetCode/11-1-24/Solution.java @@ -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)); + } +}