-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9166b95
commit 3b7f93f
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n + maxx) | ||
Space complexity - O(maxx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |