Skip to content

Commit

Permalink
Added codes for 30 and 31 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 31, 2024
1 parent 8b7cf19 commit 35b4dfc
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 0 deletions.
62 changes: 62 additions & 0 deletions GeeksForGeeks/January/30-1-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/January/30-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n1 * n2 * n3)
Space complexity - O(n1 * n2 * n3)
106 changes: 106 additions & 0 deletions GeeksForGeeks/January/31-1-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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<key.length(); level++)
{
int idx = key.charAt(level)-'a';
if(curr.children[idx] == null)
{
curr.children[idx] = new TrieNode();
}
curr = curr.children[idx];
}
curr.isEndOfWord = true;
}


//Function to use TRIE data structure and search the given string.
static boolean search(TrieNode root, String key)
{
// Your code here
TrieNode curr = root;
for(int level=0; level<key.length(); level++)
{
int idx = key.charAt(level)-'a';
if(curr.children[idx] == null)
{
return false;
}
curr = curr.children[idx];
}
return curr.isEndOfWord == true;
}


//{ Driver Code Starts.
}
// } Driver Code Ends
2 changes: 2 additions & 0 deletions GeeksForGeeks/January/31-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m)
Space complexity - O(m)
2 changes: 2 additions & 0 deletions LeetCode/January/30-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)
30 changes: 30 additions & 0 deletions LeetCode/January/30-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution
{
public int evalRPN(String[] tokens)
{
final Map<String, BinaryOperator<Long>> op = Map.of(
"+", (a, b) -> a + b,
"-", (a, b) -> a - b,
"*", (a, b) -> a * b,
"/", (a, b) -> a / b);


Deque<Long> 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();
}
}
2 changes: 2 additions & 0 deletions LeetCode/January/31-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)
21 changes: 21 additions & 0 deletions LeetCode/January/31-1-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution
{
public int[] dailyTemperatures(int[] temperatures)
{
int[] ans = new int[temperatures.length];
Deque<Integer> 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;
}
}

0 comments on commit 35b4dfc

Please sign in to comment.