-
Notifications
You must be signed in to change notification settings - Fork 2
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
Pranav Anil Bhole
committed
Jan 15, 2021
1 parent
9af5df0
commit 42636a6
Showing
25 changed files
with
1,216 additions
and
3 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/com/bhole/advanced_ds/dp/basic/MinArrayJump.java
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,38 @@ | ||
package com.bhole.advanced_ds.dp.basic; | ||
|
||
public class MinArrayJump { | ||
|
||
/* | ||
DP works in way by traversing the subproblems from end to start. | ||
dp[i] = min jump required to reach end from i. | ||
Starts with base case dp[end] = 0; | ||
we iterate i from end-1 to start; | ||
on every i, if possible jump >0 then | ||
try all j jump and minimize dp[j] | ||
set dp[i] = dp[j]+1; | ||
if jump is not possible at i, set dp[i] = -1; | ||
*/ | ||
public int solve(int arr[]) { | ||
if (arr.length==0) return -1; | ||
int dp[] = new int[arr.length]; | ||
int end= arr.length-1; | ||
dp[end] = 0; | ||
for (int i=end-1; i>=0; i--) { | ||
if (arr[i]>0) { | ||
int min = Integer.MAX_VALUE; | ||
for (int k=i+1; k<=(i+arr[i]) && k<=end; k++) { | ||
if (dp[k]>=0) { | ||
min=Math.min(dp[k], min); | ||
} | ||
} | ||
dp[i] = 1+min; | ||
} else { | ||
dp[i] = -1; | ||
} | ||
} | ||
return dp[0]; | ||
} | ||
{ | ||
|
||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/bhole/advanced_ds/dp/medium/ContinuousSubArraySum.java
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,46 @@ | ||
package com.bhole.advanced_ds.dp.medium; | ||
|
||
import com.bhole.advanced_ds.random.medium.Tuple; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ContinuousSubArraySum { | ||
|
||
/* | ||
Return start and end index tuple of continuous sub array sum | ||
*/ | ||
public Tuple continuousSubArraySum(int[] nums, int sum) { | ||
if (nums.length == 0) return new Tuple(-1,-1); | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
int curSum = 0; | ||
int minLength = nums.length; | ||
int start = -1, end =-1; | ||
for (int i=0; i<nums.length; i++) { | ||
curSum+=nums[i]; | ||
if (curSum-sum == 0) { | ||
if (i+1 < minLength) { | ||
start = 0; | ||
end = i; | ||
minLength = end-start+1; | ||
} | ||
} | ||
if (map.containsKey(curSum-sum)) { | ||
int index = map.get(curSum-sum)+1; | ||
if (i-index+1 < minLength) { | ||
start = index; | ||
end = i; | ||
minLength = end-start+1; | ||
} | ||
} | ||
map.put(curSum, i); | ||
} | ||
return new Tuple(start,end); | ||
} | ||
|
||
public static void main(String args[]) { | ||
ContinuousSubArraySum subArraySum = new ContinuousSubArraySum(); | ||
int arr[] = {3,1,4,2, 7}; | ||
System.out.println(subArraySum.continuousSubArraySum(arr, 7));; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/bhole/advanced_ds/random/easy/JavaCollectionsExamples.java
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,63 @@ | ||
package com.bhole.advanced_ds.random.easy; | ||
|
||
import java.util.*; | ||
|
||
public class JavaCollectionsExamples { | ||
public void treeSet() { | ||
/* | ||
log n insertion, o(n logn) to sort | ||
Not a threadsafe. | ||
*/ | ||
Set<CustomNode> set = new TreeSet<CustomNode>(new Comparator<CustomNode>() { | ||
@Override | ||
public int compare(CustomNode customNode, CustomNode t1) { | ||
return customNode.val-t1.val; | ||
} | ||
}); | ||
|
||
set.add(new CustomNode(10)); | ||
set.add(new CustomNode(7)); | ||
set.add(new CustomNode(20)); | ||
|
||
set.stream().forEach(System.out::println); | ||
} | ||
|
||
public void treeMap() { | ||
// Log n operations like contains,add,remove, sorted map base on keys | ||
TreeMap<CustomNode, Integer> treeMap = new TreeMap<>(new Comparator<CustomNode>() { | ||
@Override | ||
public int compare(CustomNode customNode, CustomNode t1) { | ||
return customNode.val-t1.val; | ||
} | ||
}); | ||
|
||
treeMap.put(new CustomNode(10), 10); | ||
treeMap.put(new CustomNode(7), 7); | ||
treeMap.put(new CustomNode(20), 20); | ||
java.util.Map.Entry<CustomNode, Integer> entry = treeMap.pollFirstEntry(); | ||
System.out.println(entry); | ||
}; | ||
|
||
public void linkedHashSet() { | ||
// maintains the order of inserted elements | ||
Set<CustomNode> set = new LinkedHashSet(); | ||
} | ||
|
||
|
||
class CustomNode { | ||
int val; | ||
public CustomNode(int val) { | ||
this.val = val; | ||
} | ||
@Override | ||
public String toString() { | ||
return "("+val+")"; | ||
} | ||
} | ||
|
||
public static void main(String args[]) { | ||
JavaCollectionsExamples examples = new JavaCollectionsExamples(); | ||
//examples.treeSet(); | ||
examples.treeMap(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/bhole/advanced_ds/random/easy/KthLargest.java
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,34 @@ | ||
package com.bhole.advanced_ds.random.easy; | ||
|
||
import java.awt.event.KeyAdapter; | ||
import java.util.Arrays; | ||
import java.util.PriorityQueue; | ||
|
||
public class KthLargest { | ||
|
||
PriorityQueue<Integer> priorityQueue = new PriorityQueue(); | ||
int k; | ||
|
||
public KthLargest(int k, int[] nums) { | ||
this.k = k; | ||
Arrays.stream(nums).forEach(n-> { | ||
System.out.println(add(n)); | ||
}); | ||
} | ||
|
||
public int add(int val) { | ||
priorityQueue.add(val); | ||
if (priorityQueue.size() > k) { | ||
priorityQueue.poll(); | ||
} | ||
return priorityQueue.peek(); | ||
} | ||
|
||
public static void main(String args[]) { | ||
KthLargest kthLargest = new KthLargest(3, new int[]{4,5,8,2}); | ||
Arrays.stream(new int[]{3,5,10,9,4}).forEach(n-> { | ||
System.out.println(" : "+kthLargest.add(n)); | ||
}); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/bhole/advanced_ds/random/easy/PrimePermutations.java
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,45 @@ | ||
package com.bhole.advanced_ds.random.easy; | ||
|
||
public class PrimePermutations { | ||
public int numPrimeArrangements(int n) { | ||
if (n<=0) return 0; | ||
if (n==1) return 1; | ||
int mod = (int)Math.pow(10,9) +7; | ||
System.out.println("num="+n); | ||
boolean primes[] = new boolean[n+1]; | ||
int c=0; | ||
for (int i=2; i<=n; i++) { | ||
if (primes[i] == false) { | ||
c++; | ||
System.out.println(i+" is prime, count="+c); | ||
for (int k=2; (k*i)<=n; k++) { | ||
primes[k*i] = true; | ||
} | ||
} | ||
} | ||
|
||
long pm = 1; | ||
int primeCount = c; | ||
int nonPrimeCount = (n-c); | ||
System.out.println(String.format("primeCount=%d, n=%d, nonPrimeCount=%d, c=%d",primeCount, n, nonPrimeCount, c)); | ||
|
||
for (int i=1; i<=n; i++) { | ||
if (primes[i]==false) { | ||
if(primeCount>0) pm = pm*primeCount; | ||
primeCount--; | ||
} else { | ||
if (nonPrimeCount>0) pm=pm*nonPrimeCount; | ||
nonPrimeCount--; | ||
} | ||
System.out.println(pm); | ||
pm=pm%mod; | ||
} | ||
return (int)pm; | ||
} | ||
public static void main(String args[]) { | ||
PrimePermutations primePermutations = new PrimePermutations(); | ||
System.out.println(primePermutations.numPrimeArrangements(100)); | ||
double m = 10/0; | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/bhole/advanced_ds/random/easy/SubDomainVisitCount.java
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,46 @@ | ||
package com.bhole.advanced_ds.random.easy; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/* | ||
input: ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"] | ||
output: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"] | ||
*/ | ||
public class SubDomainVisitCount { | ||
public List<String> subdomainVisits(String[] cpdomains) { | ||
if (cpdomains.length == 0) { | ||
return new ArrayList<>(); | ||
} | ||
Map<String, Integer> res = new HashMap(); | ||
Arrays.stream(cpdomains).forEach(cpdomain-> { | ||
if (cpdomain!=null) { | ||
String arr[] = cpdomain.trim().split("\\s+"); | ||
if (arr.length >= 2) { | ||
try { | ||
int count = Integer.parseInt(arr[0]); | ||
String domain = arr[1]; | ||
for (int i=domain.length()-1; i>=0; i--) { | ||
if (domain.charAt(i) == '.' || i==0) { | ||
String tmp = domain.substring(i==0?0:i+1); | ||
if(res.containsKey(tmp)) { | ||
res.put(tmp, count+res.get(tmp)); | ||
} else { | ||
res.put(tmp, count); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}); | ||
return res.entrySet().stream().map(e-> e.getValue()+" "+e.getKey()).collect(Collectors.toList()); | ||
} | ||
public static void main(String args[]) { | ||
SubDomainVisitCount subDomainVisitCount = new SubDomainVisitCount(); | ||
List<String> res = subDomainVisitCount.subdomainVisits(new String[] {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"}); | ||
System.out.println(Arrays.toString(res.toArray())); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/bhole/advanced_ds/random/easy/TrailingNumberOfZerosInNFactorial.java
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,30 @@ | ||
package com.bhole.advanced_ds.random.easy; | ||
|
||
public class TrailingNumberOfZerosInNFactorial { | ||
/* | ||
Given an integer n, return the number of trailing zeroes in n!. | ||
idea is count the number of 5 s any i from 0 to n. | ||
that count would be trailing zeros; | ||
*/ | ||
public int trailingZeroes(int n) { | ||
int count = 0; | ||
for (int i=1; i<=n; i++) { | ||
int num = i; | ||
while (num>0) { | ||
if (num>=5 && num%5==0) { | ||
System.out.print(", "+i); | ||
count+=1; | ||
num = num/5; | ||
} else { | ||
num = 0; | ||
} | ||
} | ||
|
||
} | ||
return count; | ||
} | ||
public static void main(String args[]) { | ||
TrailingNumberOfZerosInNFactorial zerosInNFactorial = new TrailingNumberOfZerosInNFactorial(); | ||
System.out.println(zerosInNFactorial.trailingZeroes(30)); | ||
} | ||
} |
Oops, something went wrong.