-
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.
week of 26th august, graph, dp, ll, pq
- Loading branch information
1 parent
1a4d475
commit a41b25e
Showing
5 changed files
with
353 additions
and
18 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,28 @@ | ||
package array.problems; | ||
|
||
public class IntervalCount { | ||
|
||
public static void main(String[] args) { | ||
|
||
int[] startTime = {1, 2, 3}; | ||
int[] endTime = {3, 2, 7}; | ||
|
||
int queryTime = 4; | ||
|
||
System.out.println(studentsStudyingAtQueryTime(startTime, endTime, queryTime)); | ||
|
||
} | ||
|
||
private static int studentsStudyingAtQueryTime(int[] startTime, int[] endTime, int queryTime) { | ||
|
||
int count = 0; | ||
for (int i = 0; i < startTime.length; i++) { | ||
if (startTime[i] <= queryTime && endTime[i] >= queryTime) { | ||
count += 1; | ||
} | ||
} | ||
return count; | ||
|
||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
topics/topics/dp/lcs/MinimumInsertionDeletionToCreateString.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,68 @@ | ||
package dp.lcs; | ||
|
||
import java.util.Arrays; | ||
|
||
public class MinimumInsertionDeletionToCreateString { | ||
|
||
public static void main(String[] args) { | ||
|
||
String from = "abcde"; | ||
String to = "acdef"; | ||
MinimumInsertionDeletionToCreateString min = new MinimumInsertionDeletionToCreateString(); | ||
String lcs = min.convertString(from, to); | ||
|
||
System.out.println(lcs); | ||
|
||
int insertions = 0; | ||
int fromItr = 0; | ||
int lcsIter = 0; | ||
while (fromItr < from.length() && lcsIter < lcs.length()) { | ||
while (from.charAt(fromItr) != lcs.charAt(lcsIter)) { | ||
System.out.println("to remove:" + from.charAt(fromItr)); | ||
fromItr += 1; | ||
} | ||
System.out.println("matched:" + lcs.charAt(lcsIter)); | ||
fromItr += 1; | ||
lcsIter += 1; | ||
} | ||
while (fromItr < from.length()) { | ||
System.out.println(from.charAt(fromItr)); | ||
fromItr += 1; | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
||
private String convertString(String from, String to) { | ||
|
||
int[][] dp = new int[from.length()][to.length()]; | ||
for (int[] arr : dp) { | ||
Arrays.fill(arr, -1); | ||
} | ||
StringBuilder answer = new StringBuilder(); | ||
findCommonSubsequence(from, from.length() - 1, to, to.length() - 1, dp, answer); | ||
return answer.toString(); | ||
} | ||
|
||
private int findCommonSubsequence(String from, int fromLen, String to, int toLen, int[][] dp, StringBuilder answer) { | ||
if (fromLen < 0 || toLen < 0) { | ||
return 0; | ||
} | ||
if (dp[fromLen][toLen] != -1) { | ||
return dp[fromLen][toLen]; | ||
} | ||
|
||
if (from.charAt(fromLen) == to.charAt(toLen)) { | ||
answer.append(from.charAt(fromLen)); | ||
dp[fromLen][toLen] = 1 + findCommonSubsequence(from, fromLen - 1, to, toLen - 1, dp, answer); | ||
} else { | ||
dp[fromLen][toLen] = Integer.max(findCommonSubsequence(from, fromLen - 1, to, toLen, dp, answer), | ||
findCommonSubsequence(from, fromLen, to, toLen - 1, dp, answer)); | ||
|
||
} | ||
return dp[fromLen][toLen]; | ||
} | ||
|
||
} |
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
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 @@ | ||
{{10},{10,13},{3,17},{6,11},{10,5},{9,10},{13},{2,19},{2},{3},{5,25},{8},{9,22},{5,5},{1,30},{11},{9,12},{7},{5},{8},{9},{4,30},{9,3},{9},{10},{10},{6,14},{3,1},{3},{10,11},{8},{2,14},{1},{5},{4},{11,4},{12,24},{5,18},{13},{7,23},{8},{12},{3,27},{2,12},{5},{2,9},{13,4},{8,18},{1,7},{6},{9,29},{8,21},{5},{6,30},{1,12},{10},{4,15},{7,22},{11,26},{8,17},{9,29},{5},{3,4},{11,30},{12},{4,29},{3},{9},{6},{3,4},{1},{10},{3,29},{10,28},{1,20},{11,13},{3},{3,12},{3,8},{10,9},{3,26},{8},{7},{5},{13,17},{2,27},{11,15},{12},{9,19},{2,15},{3,16},{1},{12,17},{9,1},{6,19},{4},{5},{5},{8,1},{11,7},{5,2},{9,28},{1},{2,2},{7,4},{4,22},{7,24},{9,26},{13,28},{11,26}} |
Oops, something went wrong.