diff --git a/topics/topics/array/problems/IntervalCount.java b/topics/topics/array/problems/IntervalCount.java new file mode 100644 index 0000000..99c113d --- /dev/null +++ b/topics/topics/array/problems/IntervalCount.java @@ -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; + + } + +} diff --git a/topics/topics/dp/lcs/MinimumInsertionDeletionToCreateString.java b/topics/topics/dp/lcs/MinimumInsertionDeletionToCreateString.java new file mode 100644 index 0000000..35ae054 --- /dev/null +++ b/topics/topics/dp/lcs/MinimumInsertionDeletionToCreateString.java @@ -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]; + } + +} diff --git a/topics/topics/graph/problems/PathWithMaxProbability.java b/topics/topics/graph/problems/PathWithMaxProbability.java index 80adf0e..de8d6c9 100644 --- a/topics/topics/graph/problems/PathWithMaxProbability.java +++ b/topics/topics/graph/problems/PathWithMaxProbability.java @@ -1,6 +1,8 @@ package graph.problems; -import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.PriorityQueue; public class PathWithMaxProbability { @@ -10,37 +12,80 @@ public static void main(String[] args) { int n = 3; double[] succProb = new double[]{0.5, 0.5, 0.2}; int startNode = 0; - int endNode = 3; + int endNode = 2; + PathWithMaxProbability path = new PathWithMaxProbability(); + System.out.println(path.traversal(graph, n, succProb, startNode, endNode)); - traversal(graph, n, succProb, startNode, endNode); + } + + class NodeToProbability { + int node; + double prob; + + public NodeToProbability(int node, double prob) { + this.node = node; + this.prob = prob; + } + public int getNode() { + return node; + } + + public double getProb() { + return prob; + } } - private static void traversal(int[][] graph, int n, double[] succProb, int startNode, int endNode) { + private double traversal(int[][] graph, int n, double[] succProb, int startNode, int endNode) { - boolean[] visited = new boolean[n]; - traversal(graph, startNode, endNode, visited); + + ArrayList> probabilityGraph = new ArrayList<>(); + for (int i = 0; i < n; i++) { + probabilityGraph.add(new ArrayList<>()); + } + for (int i = 0; i < graph.length; i++) { + int to = graph[i][0]; + int from = graph[i][1]; + double prob = succProb[i]; + probabilityGraph.get(to).add(new NodeToProbability(from, prob)); + probabilityGraph.get(from).add(new NodeToProbability(to, prob)); + } + + return findMaximumProbability(probabilityGraph, startNode, endNode, n); } - //written a plain dfs.. to compute - private static void traversal(int[][] graph, int startNode, int endNode, boolean[] visited) { + private double findMaximumProbability(ArrayList> probabilityGraph, int srcNode, int destNode, int nodes) { - ArrayDeque queue = new ArrayDeque<>(); - queue.add(startNode); - visited[startNode] = true; + boolean[] visited = new boolean[nodes]; + double[] probability = new double[nodes]; + probability[srcNode] = 1; + Comparator comp = (c1, c2) -> Double.compare(c2.prob, c1.prob); + PriorityQueue queue = new PriorityQueue<>(comp); + queue.add(new NodeToProbability(srcNode, 1)); + visited[srcNode] = true; - while(!queue.isEmpty()){ - Integer top = queue.remove(); - System.out.println(top+" "); - for(int neigh: graph[top]){ - if(!visited[neigh]){ - visited[neigh] = true; - queue.add(neigh); + while (!queue.isEmpty()) { + NodeToProbability top = queue.remove(); + int topNode = top.node; + double topProb = top.prob; + for (NodeToProbability neighbour : probabilityGraph.get(topNode)) { + int neighNode = neighbour.node; + double probFromTopToNeigh = neighbour.prob; + if (!visited[neighNode] || probability[neighNode] < topProb * probFromTopToNeigh) { + visited[neighNode] = true; + if (probability[neighNode] < topProb * probFromTopToNeigh) { + probability[neighNode] = topProb * probFromTopToNeigh; + queue.add(new NodeToProbability(neighNode, probability[neighNode])); + } } + } } + return probability[destNode]; + } + } diff --git a/topics/topics/graph/problems/input b/topics/topics/graph/problems/input new file mode 100644 index 0000000..546daed --- /dev/null +++ b/topics/topics/graph/problems/input @@ -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}} \ No newline at end of file diff --git a/topics/topics/linkedlist/problems/LRUCache.java b/topics/topics/linkedlist/problems/LRUCache.java new file mode 100644 index 0000000..fcdf93d --- /dev/null +++ b/topics/topics/linkedlist/problems/LRUCache.java @@ -0,0 +1,193 @@ +package linkedlist.problems; + +import java.util.HashMap; +import java.util.PriorityQueue; + +public class LRUCache { + + class KeyToTimeStamp { + int key; + int timeStamp; + + public KeyToTimeStamp(int key, int timeStamp) { + this.key = key; + this.timeStamp = timeStamp; + } + + public int getKey() { + return key; + } + + public int getTimeStamp() { + return timeStamp; + } + } + + HashMap map = new HashMap<>(); + HashMap mapKeyToTSObject = new HashMap<>(); + PriorityQueue queue = new PriorityQueue<>((c1, c2) -> Integer.compare(c1.getTimeStamp(), c2.getTimeStamp())); + int capacity = 3; + int time = -1; + + public LRUCache(int capacity) { + this.capacity = capacity; + this.time = 0; + } + + public int get(int key) { + int answer = -1; + if (map.containsKey(key)) { + answer = map.get(key); + } else { + return answer; + } + KeyToTimeStamp keyToTimeStamp = mapKeyToTSObject.get(key); + queue.remove(keyToTimeStamp); + + KeyToTimeStamp keyToTimeStamp1 = new KeyToTimeStamp(key, time); + queue.add(keyToTimeStamp1); + mapKeyToTSObject.put(key, keyToTimeStamp1); + + incrementTime(); + + return answer; + } + + public void put(int key, int value) { + + if (queue.size() == capacity && !map.containsKey(key)) { + //remove. + KeyToTimeStamp keyToTimeStamp = queue.remove(); + mapKeyToTSObject.remove(keyToTimeStamp.getKey()); + map.remove(keyToTimeStamp.getKey()); + } else if (map.containsKey(key)) { + //remove from queue - as new entry will come. + KeyToTimeStamp keyToTimeStamp = mapKeyToTSObject.get(key); + mapKeyToTSObject.remove(key); + queue.remove(keyToTimeStamp); + } + + map.put(key, value); + KeyToTimeStamp keyTimeStampObject = new KeyToTimeStamp(key, time); + queue.add(keyTimeStampObject); + mapKeyToTSObject.put(key, keyTimeStampObject); + + incrementTime(); + } + + private void incrementTime() { + time += 1; + } + + class DLLNode { + int key; + int data; + DLLNode left; + DLLNode right; + + public DLLNode(int key, int data) { + this.key = key; + this.data = data; + } + } + + DLLNode head; + DLLNode tail; + int size = 0; + HashMap mapKeyToNode = new HashMap<>(); + public void LRUCacheDLL(int capacity) { + this.capacity = capacity; + head.right = tail; + tail.left = head; + } + + // h -> 2 > 3 > t + public void putDLL(int key, int value) { + if(size == capacity && !mapKeyToNode.containsKey(key)) { + //delete the last entry. + DLLNode lastNode = tail.left; + DLLNode secondLastNode = lastNode.left; + secondLastNode.right = tail; + tail.left = secondLastNode; + mapKeyToNode.remove(lastNode.key); + } + else if(mapKeyToNode.containsKey(key)){ + //deletion. + DLLNode dllNode = mapKeyToNode.get(key); + DLLNode left = dllNode.left; + DLLNode right = dllNode.right; + left.right = right; + right.left = left; + } + + DLLNode newNode = new DLLNode(key, value); + DLLNode firstNode = head.right; + firstNode.left = newNode; + newNode.right = firstNode; + head.right = newNode; + newNode.left = head; + + incrementTime(); + } + + public int getDLL(int key) { + int answer = -1; + DLLNode node; + if(mapKeyToNode.containsKey(key)){ + node = mapKeyToNode.get(key); + answer = node.data; + } + else{ + return answer; + } + +// mapKeyToNode. + return answer; + } + + + public static void main(String[] args) { + + String[] operations = {"LRUCache", "put", "put", "put", "put", "put", "get", "put", "get", "get", "put", "get", "put", "put", "put", + "get", "put", "get", "get", "get", "get", "put", "put", "get", "get", "get", "put", "put", "get", "put", "get", + "put", "get", "get", "get", "put", "put", "put", "get", "put", "get", "get", "put", "put", "get", "put", "put", + "put", "put", "get", "put", "put", "get", "put", "put", "get", "put", "put", "put", "put", "put", "get", "put", + "put", "get", "put", "get", "get", "get", "put", "get", "get", "put", "put", "put", "put", "get", "put", "put", + "put", "put", "get", "get", "get", "put", "put", "put", "get", "put", "put", "put", "get", "put", "put", "put", + "get", "get", "get", "put", "put", "put", "put", "get", "put", "put", "put", "put", "put", "put", "put"}; + + int[][] inputs = new int[][]{{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}}; + + Integer[] expected = new Integer[]{null, null, null, null, null, null, -1, null, 19, 17, null, -1, null, null, null, -1, null, -1, 5, -1, 12, + null, null, 3, 5, 5, null, null, 1, null, -1, null, 30, 5, 30, null, null, null, -1, null, -1, 24, null, null, 18, null, null, null, + null, -1, null, null, 18, null, null, -1, null, null, null, null, null, 18, null, null, -1, null, 4, 29, 30, null, 12, -1, null, + null, null, null, 29, null, null, null, null, 17, 22, 18, null, null, null, -1, null, null, null, 20, null, null, null, -1, 18, + 18, null, null, null, null, 20, null, null, null, null, null, null, null}; + + LRUCache lruCache = new LRUCache(inputs[0][0]); + + for (int i = 1; i < operations.length; i++) { + if (operations[i].equals("put")) { + + lruCache.put(inputs[i][0], inputs[i][1]); + } else { + int input = inputs[i][0]; + int answer = lruCache.get(input); + if (answer != expected[i]) { + System.out.println("input:" + input + ", answer:" + answer + ", expected:" + expected[i]); + return; + } else { + System.out.println("answer:" + answer); + } + } + } + + } + +}