From 35b4dfc5a14a5f8b709aefd015b63bc29c2ece1d Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Wed, 31 Jan 2024 21:35:46 +0530 Subject: [PATCH] Added codes for 30 and 31 Jan --- GeeksForGeeks/January/30-1-24/GFG.java | 62 ++++++++++++++ GeeksForGeeks/January/30-1-24/README.md | 2 + GeeksForGeeks/January/31-1-24/GFG.java | 106 ++++++++++++++++++++++++ GeeksForGeeks/January/31-1-24/README.md | 2 + LeetCode/January/30-1-24/README.md | 2 + LeetCode/January/30-1-24/Solution.java | 30 +++++++ LeetCode/January/31-1-24/README.md | 2 + LeetCode/January/31-1-24/Solution.java | 21 +++++ 8 files changed, 227 insertions(+) create mode 100644 GeeksForGeeks/January/30-1-24/GFG.java create mode 100644 GeeksForGeeks/January/30-1-24/README.md create mode 100644 GeeksForGeeks/January/31-1-24/GFG.java create mode 100644 GeeksForGeeks/January/31-1-24/README.md create mode 100644 LeetCode/January/30-1-24/README.md create mode 100644 LeetCode/January/30-1-24/Solution.java create mode 100644 LeetCode/January/31-1-24/README.md create mode 100644 LeetCode/January/31-1-24/Solution.java diff --git a/GeeksForGeeks/January/30-1-24/GFG.java b/GeeksForGeeks/January/30-1-24/GFG.java new file mode 100644 index 0000000..b2a7196 --- /dev/null +++ b/GeeksForGeeks/January/30-1-24/GFG.java @@ -0,0 +1,62 @@ +//{ Driver Code Starts +//Initial Template for Java +import java.io.*; +import java.util.*; +class GFG{ + public static void main(String args[]) throws IOException + { + BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); + int t = Integer.parseInt(read.readLine()); + while(t-- > 0){ + String input_line[] = read.readLine().trim().split("\\s+"); + int n1 = Integer.parseInt(input_line[0]); + int n2 = Integer.parseInt(input_line[1]); + int n3 = Integer.parseInt(input_line[2]); + input_line = read.readLine().trim().split("\\s+"); + String A = input_line[0]; + String B = input_line[1]; + String C = input_line[2]; + Solution obj = new Solution(); + System.out.println(obj.LCSof3(A, B, C, n1, n2, n3)); + } + } +} +// } Driver Code Ends + + +//User function Template for Java +class Solution +{ + static int help(String A,String B,String C,int i,int j,int k,int n1,int n2,int n3,int dp[][][]) + { + if(i==n1 || j==n2 || k==n3) + return 0; + if(dp[i][j][k]!=-1) + return dp[i][j][k]; + if(A.charAt(i)==B.charAt(j) && A.charAt(i)==C.charAt(k)) + { + return dp[i][j][k]=1+help(A,B,C,i+1,j+1,k+1,n1,n2,n3,dp); + } + + int a = help(A,B,C,i+1,j,k,n1,n2,n3,dp); + int b = help(A,B,C,i,j+1,k,n1,n2,n3,dp); + int c = help(A,B,C,i,j,k+1,n1,n2,n3,dp); + + return dp[i][j][k]= Math.max(a,Math.max(b,c)); + } + + + int LCSof3(String A, String B, String C, int n1, int n2, int n3) + { + // code here + int dp[][][] = new int[n1][n2][n3]; + for(int temp[][]:dp) + { + for(int temp2[]:temp) + { + Arrays.fill(temp2,-1); + } + } + return help(A,B,C,0,0,0,n1,n2,n3,dp); + } +} diff --git a/GeeksForGeeks/January/30-1-24/README.md b/GeeksForGeeks/January/30-1-24/README.md new file mode 100644 index 0000000..4654edf --- /dev/null +++ b/GeeksForGeeks/January/30-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n1 * n2 * n3) +Space complexity - O(n1 * n2 * n3) diff --git a/GeeksForGeeks/January/31-1-24/GFG.java b/GeeksForGeeks/January/31-1-24/GFG.java new file mode 100644 index 0000000..ac4772f --- /dev/null +++ b/GeeksForGeeks/January/31-1-24/GFG.java @@ -0,0 +1,106 @@ +//{ Driver Code Starts +// Initial Template for Java + +/*package whatever //do not write package name here */ + +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) { + int n = sc.nextInt(); + sc.nextLine(); + String[] keys = sc.nextLine().split(" "); + + TrieNode root = new TrieNode(); + for (int i = 0; i < n; i++) { + insert(root, keys[i]); + } + String abc = sc.nextLine(); + if (search(root, abc)) + System.out.println(1); + else + System.out.println(0); + } + } + + static final int ALPHABET_SIZE = 26; + + // trie node + static class TrieNode { + TrieNode[] children = new TrieNode[ALPHABET_SIZE]; + + // isEndOfWord is true if the node represents + // end of a word + boolean isEndOfWord; + + TrieNode() { + isEndOfWord = false; + for (int i = 0; i < ALPHABET_SIZE; i++) children[i] = null; + } + }; + + +// } Driver Code Ends +// User function Template for Java + +/* +static final int ALPHABET_SIZE = 26; + + // trie node + static class TrieNode { + TrieNode[] children = new TrieNode[ALPHABET_SIZE]; + + // isEndOfWord is true if the node represents + // end of a word + boolean isEndOfWord; + + TrieNode() { + isEndOfWord = false; + for (int i = 0; i < ALPHABET_SIZE; i++) children[i] = null; + } + }; +*/ +//Function to insert string into TRIE. +static void insert(TrieNode root, String key) +{ + // Your code here + TrieNode curr = root; + for(int level=0; level> op = Map.of( + "+", (a, b) -> a + b, + "-", (a, b) -> a - b, + "*", (a, b) -> a * b, + "/", (a, b) -> a / b); + + + Deque stack = new ArrayDeque<>(); + + for (final String token : tokens) + { + if (op.containsKey(token)) + { + long b = stack.pop(); + long a = stack.pop(); + stack.push(op.get(token).apply(a, b)); + } + else + { + stack.push(Long.parseLong(token)); + } + } + + return stack.pop().intValue(); + } +} diff --git a/LeetCode/January/31-1-24/README.md b/LeetCode/January/31-1-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/LeetCode/January/31-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/January/31-1-24/Solution.java b/LeetCode/January/31-1-24/Solution.java new file mode 100644 index 0000000..4e2873b --- /dev/null +++ b/LeetCode/January/31-1-24/Solution.java @@ -0,0 +1,21 @@ +class Solution +{ + public int[] dailyTemperatures(int[] temperatures) + { + int[] ans = new int[temperatures.length]; + Deque stack = new ArrayDeque<>(); // a decreasing stack + + for (int i = 0; i < temperatures.length; ++i) + { + while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) + { + int index = stack.pop(); + ans[index] = i - index; + } + + stack.push(i); + } + + return ans; + } +}