Skip to content

Commit

Permalink
Added codes for 7 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 7, 2024
1 parent 9166b95 commit 3b7f93f
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
82 changes: 82 additions & 0 deletions GeeksForGeeks/June/07-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//{ Driver Code Starts
import java.io.*;
import java.lang.*;
import java.util.*;

class Main {

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim()); // Inputting the testcases
while (t-- > 0) {

// taking size of array
int n = Integer.parseInt(br.readLine().trim());
int l[] = new int[n];
int r[] = new int[n];
String inputLine[] = br.readLine().trim().split(" ");

// adding elements to array L
for (int i = 0; i < n; i++) {
l[i] = Integer.parseInt(inputLine[i]);
}
inputLine = br.readLine().trim().split(" ");
int maxx = Integer.MIN_VALUE;

// adding elements to array R
for (int i = 0; i < n; i++) {
r[i] = Integer.parseInt(inputLine[i]);
if (r[i] > maxx) {
maxx = r[i];
}
}

Solution obj = new Solution();

// calling maxOccured() function
System.out.println(obj.maxOccured(n, l, r, maxx));
}
}
}


// } Driver Code Ends
// L[] and R[] are input ranges
// n : size of array
// maxx: maximum element in R[]
// arr[]: declared globally with size equal to 1000000

class Solution
{
// Function to find the maximum occurred integer in all ranges.
public static int maxOccured(int n, int l[], int r[], int maxx)
{
int[] arr = new int[maxx+2];

for (int i=0; i<n; i++)
{
arr[l[i]]++;
arr[r[i]+1]--;
}

int maxfreq = arr[0];
int result = 0;

for (int i=1; i<=maxx; i++)
{
arr[i] += arr[i-1];
if (arr[i] > maxfreq)
{
maxfreq = arr[i];
result = i;
}
}

return result;
}
}

//{ Driver Code Starts.

// } Driver Code Ends
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/07-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n + maxx)
Space complexity - O(maxx)
2 changes: 2 additions & 0 deletions LeetCode/June/07-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(k*n + s*m)
SPace complexity - O(k*n*26)
75 changes: 75 additions & 0 deletions LeetCode/June/07-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class Solution
{
class Trie
{
Trie child[]=new Trie[26];
boolean isEnd;
}

public String replaceWords(List<String> dictionary, String sentence)
{
Trie root = new Trie();

for (String s: dictionary)
{
insert(root,s);
}

StringBuilder res = new StringBuilder();
String words[] = sentence.split(" ");

for (int i=0; i<words.length; i++)
{
res.append(search(root,words[i]));
res.append(" ");
}

res.setLength(res.length()-1);
return res.toString();
}

public String search(Trie root, String key)
{
Trie curr = root;
StringBuilder temp = new StringBuilder();

for (int i=0; i<key.length(); i++)
{
int index=key.charAt(i)-'a';

if (curr.child[index] == null)
{
return key;
}

temp.append(key.charAt(i));
curr=curr.child[index];

if (curr.isEnd == true)
{
return temp.toString();
}
}

return key;
}

public void insert(Trie root, String key)
{
Trie curr=root;

for (int i=0; i<key.length(); i++)
{
int index = key.charAt(i)-'a';

if (curr.child[index] == null)
{
curr.child[index] = new Trie();
}

curr = curr.child[index];
}

curr.isEnd = true;
}
}

0 comments on commit 3b7f93f

Please sign in to comment.