|
| 1 | +From 5271b0b06a74cd82da2715bca006c9431ab45447 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Pulkit Gupta-GGN <Pulkit_Gupta-GGN@external.mckinsey.com> |
| 3 | +Date: Mon, 5 Apr 2021 13:53:50 +0530 |
| 4 | +Subject: [PATCH] Work in progress |
| 5 | + |
| 6 | +--- |
| 7 | + .DS_Store | Bin 6148 -> 8196 bytes |
| 8 | + .../done/arrays/HotelBookingPossible.java | 6 +- |
| 9 | + .../done/dp/CoinChangingProblem.java | 37 +++ |
| 10 | + .../done/dp/Knapsack.java | 75 ++++++ |
| 11 | + .../done/graphs/ShortestPathBetweenNodes.java | 7 + |
| 12 | + .../done/stacks/NStackUsage.java | 41 ++++ |
| 13 | + .../done/trees/LongestConsecutiveBranch.java | 20 ++ |
| 14 | + ...ncludingNonRootAndNonLeafWithGivenSum.java | 94 ++++++++ |
| 15 | + .../todo/DeleteANodeFromBinaryTree.java | 7 + |
| 16 | + src/main/java/com/pulkit/jyoti/GraphNode.java | 137 +++++++++++ |
| 17 | + to be committed/BoxStackingProblem.java | 46 ++++ |
| 18 | + to be committed/ConnectNodesAtSameLevel.java | 181 ++++++++++++++ |
| 19 | + to be committed/CuttingARod.java | 65 +++++ |
| 20 | + .../FlattenBinaryTreeToSinglyLinkedList.java | 86 +++++++ |
| 21 | + to be committed/GenerateParentheses.java | 25 ++ |
| 22 | + to be committed/HouseThief.java | 25 ++ |
| 23 | + to be committed/KeyPadProblem.java | 51 ++++ |
| 24 | + to be committed/KeyPadProblemm.java | 59 +++++ |
| 25 | + to be committed/KnapsackProblem.java | 224 ++++++++++++++++++ |
| 26 | + to be committed/Permutation.java | 43 ++++ |
| 27 | + to be committed/PriorityQueueTrial.java | 14 ++ |
| 28 | + to be committed/RottenTomatoes.java | 27 +++ |
| 29 | + to be committed/SearchSystem.java | 88 +++++++ |
| 30 | + to be committed/SkyLineProblem.java | 70 ++++++ |
| 31 | + to be committed/SubSetSumProblem.java | 129 ++++++++++ |
| 32 | + to be committed/Subset.java | 39 +++ |
| 33 | + to be committed/Subtree.java | 49 ++++ |
| 34 | + to be committed/WordLadderII.java | 106 +++++++++ |
| 35 | + 28 files changed, 1748 insertions(+), 3 deletions(-) |
| 36 | + create mode 100644 src/main/java/com/pulkit/datastructures_algorithms/done/dp/CoinChangingProblem.java |
| 37 | + create mode 100644 src/main/java/com/pulkit/datastructures_algorithms/done/dp/Knapsack.java |
| 38 | + create mode 100644 src/main/java/com/pulkit/datastructures_algorithms/done/graphs/ShortestPathBetweenNodes.java |
| 39 | + create mode 100644 src/main/java/com/pulkit/datastructures_algorithms/done/stacks/NStackUsage.java |
| 40 | + create mode 100644 src/main/java/com/pulkit/datastructures_algorithms/done/trees/LongestConsecutiveBranch.java |
| 41 | + create mode 100644 src/main/java/com/pulkit/datastructures_algorithms/done/trees/PathsIncludingNonRootAndNonLeafWithGivenSum.java |
| 42 | + create mode 100644 src/main/java/com/pulkit/datastructures_algorithms/todo/DeleteANodeFromBinaryTree.java |
| 43 | + create mode 100644 src/main/java/com/pulkit/jyoti/GraphNode.java |
| 44 | + create mode 100644 to be committed/BoxStackingProblem.java |
| 45 | + create mode 100644 to be committed/ConnectNodesAtSameLevel.java |
| 46 | + create mode 100644 to be committed/CuttingARod.java |
| 47 | + create mode 100644 to be committed/FlattenBinaryTreeToSinglyLinkedList.java |
| 48 | + create mode 100644 to be committed/GenerateParentheses.java |
| 49 | + create mode 100644 to be committed/HouseThief.java |
| 50 | + create mode 100644 to be committed/KeyPadProblem.java |
| 51 | + create mode 100644 to be committed/KeyPadProblemm.java |
| 52 | + create mode 100644 to be committed/KnapsackProblem.java |
| 53 | + create mode 100644 to be committed/Permutation.java |
| 54 | + create mode 100644 to be committed/PriorityQueueTrial.java |
| 55 | + create mode 100644 to be committed/RottenTomatoes.java |
| 56 | + create mode 100644 to be committed/SearchSystem.java |
| 57 | + create mode 100644 to be committed/SkyLineProblem.java |
| 58 | + create mode 100644 to be committed/SubSetSumProblem.java |
| 59 | + create mode 100644 to be committed/Subset.java |
| 60 | + create mode 100644 to be committed/Subtree.java |
| 61 | + create mode 100644 to be committed/WordLadderII.java |
| 62 | + |
| 63 | +diff --git a/.DS_Store b/.DS_Store |
| 64 | +index bfe60e09190098dd3a2b121aa3f3627b9cef99b0..c8d01059bbb6ab0ddf8ab55cdb9b59f0296a8d03 100644 |
| 65 | +GIT binary patch |
| 66 | +delta 479 |
| 67 | +zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aMAD7Z0TH}hr%jz7$c**Q2SHn1=X |
| 68 | +zPUd0xx>=pIn=zK3p@boyL4hHOAr(j_1KGJim<bdu0g9w}=Hw?Q<>V)U%mdoO2E^X~ |
| 69 | +z!2l@2z(7Fv<Rh&8%*G~Wlf&5D>ItaLNjD5m&d)6Xn+qWb=*Z1?ae=yxBj2E9t4ir1 |
| 70 | +zN8E0zhj}SCKZOKsFkJ;1q&lxGxF|0tKQA5VBt}LChRr8LY?voD@CY(PJSV{oq+LPL |
| 71 | +ev{{hjJM(0I5zoo~JRBTArJxvO*c{I@hZz7L$Ydk{ |
| 72 | + |
| 73 | +delta 136 |
| 74 | +zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jH4hU^g=(_hcS{uhPs6`3$8D |
| 75 | +zC7wC?$w@i+Nl>NMKzhdJZvxX8H<pAlE@tQ85M%}l0f7KFkZ=VVwz2R#^JIP*Pms|J |
| 76 | +QOb}~8HZyFF=b6I{0K*R$9{>OV |
| 77 | + |
| 78 | +diff --git a/src/main/java/com/pulkit/datastructures_algorithms/done/arrays/HotelBookingPossible.java b/src/main/java/com/pulkit/datastructures_algorithms/done/arrays/HotelBookingPossible.java |
| 79 | +index 02a7276..4ba29ef 100644 |
| 80 | +--- a/src/main/java/com/pulkit/datastructures_algorithms/done/arrays/HotelBookingPossible.java |
| 81 | ++++ b/src/main/java/com/pulkit/datastructures_algorithms/done/arrays/HotelBookingPossible.java |
| 82 | +@@ -24,15 +24,15 @@ public class HotelBookingPossible { |
| 83 | + public static void main(String[] args) { |
| 84 | + List<Integer> arrive = Arrays.asList(1, 2, 3, 4); |
| 85 | + List<Integer> depart = Arrays.asList(10, 2, 6, 14); |
| 86 | +- int K = 4; //true |
| 87 | ++ int numberOfRooms = 4; //true |
| 88 | + |
| 89 | + //Another Test case |
| 90 | + //List<Integer> arrive = Arrays.asList(1, 2, 3); |
| 91 | + //List<Integer> depart = Arrays.asList(2, 3, 4); |
| 92 | +- //int K = 1; //true |
| 93 | ++ //int numberOfRooms = 1; //true |
| 94 | + |
| 95 | + HotelBookingPossible hotelBookingPossible = new HotelBookingPossible(); |
| 96 | +- System.out.println(hotelBookingPossible.hotel(arrive, depart, K)); |
| 97 | ++ System.out.println(hotelBookingPossible.hotel(arrive, depart, numberOfRooms)); |
| 98 | + } |
| 99 | + |
| 100 | + private boolean hotel(List<Integer> arrive, List<Integer> depart, int K) { |
| 101 | +diff --git a/src/main/java/com/pulkit/datastructures_algorithms/done/dp/CoinChangingProblem.java b/src/main/java/com/pulkit/datastructures_algorithms/done/dp/CoinChangingProblem.java |
| 102 | +new file mode 100644 |
| 103 | +index 0000000..03c72e2 |
| 104 | +--- /dev/null |
| 105 | ++++ b/src/main/java/com/pulkit/datastructures_algorithms/done/dp/CoinChangingProblem.java |
| 106 | +@@ -0,0 +1,37 @@ |
| 107 | ++package com.pulkit.datastructures_algorithms.done.dp; |
| 108 | ++ |
| 109 | ++public class CoinChangingProblem { |
| 110 | ++ public static void main(String[] args) { |
| 111 | ++ int coinsDenominationsAvailable[] = {1, 2, 5}; |
| 112 | ++ int amount = 7; |
| 113 | ++ Counter counter = new Counter(0); |
| 114 | ++ calculatePossibleChange(amount, coinsDenominationsAvailable, 0, 0, counter); |
| 115 | ++ System.out.println(counter.count); |
| 116 | ++ } |
| 117 | ++ |
| 118 | ++ static void calculatePossibleChange(int amount, int coins[], int currentSum, int currentIndex, Counter counter) { |
| 119 | ++ System.out.println("Executing for " + currentIndex + " " + currentSum); |
| 120 | ++ |
| 121 | ++ if (amount < 0) |
| 122 | ++ return; |
| 123 | ++ |
| 124 | ++ if (currentSum > amount) |
| 125 | ++ return; |
| 126 | ++ |
| 127 | ++ if (currentSum == amount) |
| 128 | ++ counter.count++; |
| 129 | ++ |
| 130 | ++ for (int index = currentIndex; index < coins.length; index++) { |
| 131 | ++ calculatePossibleChange(amount, coins, currentSum + coins[index], |
| 132 | ++ index, counter); |
| 133 | ++ } |
| 134 | ++ } |
| 135 | ++} |
| 136 | ++ |
| 137 | ++class Counter { |
| 138 | ++ int count; |
| 139 | ++ |
| 140 | ++ public Counter(int count) { |
| 141 | ++ this.count = count; |
| 142 | ++ } |
| 143 | ++} |
| 144 | +\ No newline at end of file |
| 145 | +diff --git a/src/main/java/com/pulkit/datastructures_algorithms/done/dp/Knapsack.java b/src/main/java/com/pulkit/datastructures_algorithms/done/dp/Knapsack.java |
| 146 | +new file mode 100644 |
| 147 | +index 0000000..943a18c |
| 148 | +--- /dev/null |
| 149 | ++++ b/src/main/java/com/pulkit/datastructures_algorithms/done/dp/Knapsack.java |
| 150 | +@@ -0,0 +1,75 @@ |
| 151 | ++package com.pulkit.datastructures_algorithms.done.dp; |
| 152 | ++ |
| 153 | ++import java.util.HashMap; |
| 154 | ++import java.util.Map; |
| 155 | ++import java.util.Objects; |
| 156 | ++ |
| 157 | ++public class Knapsack { |
| 158 | ++ public static void main(String[] args) { |
| 159 | ++ int[] profits = {3, 5, 7, 9, 11, 12}; |
| 160 | ++ int[] weights = {100, 110, 120, 220, 90, 100}; |
| 161 | ++ |
| 162 | ++ int maximumCapacity = 300; |
| 163 | ++ |
| 164 | ++ int maxProfit = findMaxProfit(profits, weights, 0, maximumCapacity, 0, new HashMap<>()); |
| 165 | ++ System.out.println(maxProfit); |
| 166 | ++ } |
| 167 | ++ |
| 168 | ++ //2^N (Exponential) |
| 169 | ++ private static int findMaxProfit(int[] profits, int[] weights, int currentProfit, int capacityRemaining, |
| 170 | ++ int index, Map<Pair, Integer> cache) { |
| 171 | ++ if (capacityRemaining < 0) |
| 172 | ++ return currentProfit - profits[index - 1]; |
| 173 | ++ |
| 174 | ++ if (profits.length == 0) |
| 175 | ++ return 0; |
| 176 | ++ |
| 177 | ++ if (capacityRemaining == 0) |
| 178 | ++ return currentProfit; |
| 179 | ++ |
| 180 | ++ Pair key = new Pair(currentProfit, capacityRemaining); |
| 181 | ++ |
| 182 | ++ if (cache.containsKey(key)) { |
| 183 | ++ return cache.get(key);//Dp = recursion + memoization |
| 184 | ++ |
| 185 | ++ } else { |
| 186 | ++ |
| 187 | ++ int profitWhenWeStoleCurrentItem = findMaxProfit(profits, weights, currentProfit + profits[index], |
| 188 | ++ capacityRemaining - weights[index], index++, cache); |
| 189 | ++ |
| 190 | ++ int profitWhenWeDidNotStoleCurrentItem = findMaxProfit(profits, weights, currentProfit, |
| 191 | ++ capacityRemaining, index++, cache); |
| 192 | ++ |
| 193 | ++ int finalResult = Math.max(profitWhenWeStoleCurrentItem, profitWhenWeDidNotStoleCurrentItem); |
| 194 | ++ |
| 195 | ++ Pair key1 = new Pair(currentProfit, capacityRemaining); |
| 196 | ++ cache.put(key1, finalResult); |
| 197 | ++ |
| 198 | ++ return finalResult; |
| 199 | ++ } |
| 200 | ++ } |
| 201 | ++ |
| 202 | ++ static class Pair { |
| 203 | ++ int currentProfit; |
| 204 | ++ int capacityRemaining; |
| 205 | ++ |
| 206 | ++ public Pair(int currentProfit, int capacityRemaining) { |
| 207 | ++ this.currentProfit = currentProfit; |
| 208 | ++ this.capacityRemaining = capacityRemaining; |
| 209 | ++ } |
| 210 | ++ |
| 211 | ++ @Override |
| 212 | ++ public boolean equals(Object o) { |
| 213 | ++ if (this == o) return true; |
| 214 | ++ if (!(o instanceof Pair)) return false; |
| 215 | ++ Pair pair = (Pair) o; |
| 216 | ++ return currentProfit == pair.currentProfit && |
| 217 | ++ capacityRemaining == pair.capacityRemaining; |
| 218 | ++ } |
| 219 | ++ |
| 220 | ++ @Override |
| 221 | ++ public int hashCode() { |
| 222 | ++ return Objects.hash(currentProfit, capacityRemaining); |
| 223 | ++ } |
| 224 | ++ } |
| 225 | ++} |
| 226 | +\ No newline at end of file |
| 227 | +diff --git a/src/main/java/com/pulkit/datastructures_algorithms/done/graphs/ShortestPathBetweenNodes.java b/src/main/java/com/pulkit/datastructures_algorithms/done/graphs/ShortestPathBetweenNodes.java |
| 228 | +new file mode 100644 |
| 229 | +index 0000000..428b49f |
| 230 | +--- /dev/null |
| 231 | ++++ b/src/main/java/com/pulkit/datastructures_algorithms/done/graphs/ShortestPathBetweenNodes.java |
| 232 | +@@ -0,0 +1,7 @@ |
| 233 | ++package com.pulkit.datastructures_algorithms.done.graphs; |
| 234 | ++ |
| 235 | ++public class ShortestPathBetweenNodes { |
| 236 | ++ public static void main(String[] args) { |
| 237 | ++ |
| 238 | ++ } |
| 239 | ++} |
| 240 | +\ No newline at end of file |
| 241 | +diff --git a/src/main/java/com/pulkit/datastructures_algorithms/done/stacks/NStackUsage.java b/src/main/java/com/pulkit/datastructures_algorithms/done/stacks/NStackUsage.java |
| 242 | +new file mode 100644 |
| 243 | +index 0000000..01272a5 |
| 244 | +--- /dev/null |
| 245 | ++++ b/src/main/java/com/pulkit/datastructures_algorithms/done/stacks/NStackUsage.java |
| 246 | +@@ -0,0 +1,41 @@ |
| 247 | ++package com.pulkit.datastructures_algorithms.done.stacks; |
| 248 | ++ |
| 249 | ++public class NStackUsage { |
| 250 | ++ public static void main(String[] args) { |
| 251 | ++ NStack stacks = new NStack(3, 10); |
| 252 | ++ stacks.put(0, 10); |
| 253 | ++ stacks.put(2, 11); |
| 254 | ++ System.out.println(stacks.pop(0)); |
| 255 | ++ System.out.println(stacks.pop(2)); |
| 256 | ++ } |
| 257 | ++} |
| 258 | ++ |
| 259 | ++class NStack { |
| 260 | ++ private int[] stackArray; |
| 261 | ++ private int[] stackHeadPointer; |
| 262 | ++ |
| 263 | ++ public NStack(int numberOfStack, int capacityOfEachStack) { |
| 264 | ++ stackArray = new int[numberOfStack * capacityOfEachStack]; |
| 265 | ++ stackHeadPointer = new int[numberOfStack]; |
| 266 | ++ initializeStackHeadPointer(capacityOfEachStack); |
| 267 | ++ } |
| 268 | ++ |
| 269 | ++ public void put(int stackNumber, int element) { |
| 270 | ++ stackArray[stackHeadPointer[stackNumber]] = element; |
| 271 | ++ stackHeadPointer[stackNumber]++; |
| 272 | ++ } |
| 273 | ++ |
| 274 | ++ public Integer pop(int stackNumber) { |
| 275 | ++ int currentHead = stackHeadPointer[stackNumber]; |
| 276 | ++ stackHeadPointer[stackNumber]--; |
| 277 | ++ return stackArray[currentHead]; |
| 278 | ++ } |
| 279 | ++ |
| 280 | ++ private void initializeStackHeadPointer(int capacityOfEachStack) { |
| 281 | ++ int startingIndex = 0; |
| 282 | ++ for (int index = 0; index < stackHeadPointer.length; index++) { |
| 283 | ++ stackHeadPointer[index] = startingIndex; |
| 284 | ++ startingIndex += capacityOfEachStack; |
| 285 | ++ } |
| 286 | ++ } |
| 287 | ++} |
| 288 | +\ No newline at end of file |
| 289 | +diff --git a/src/main/java/com/pulkit/datastructures_algorithms/done/trees/LongestConsecutiveBranch.java b/src/main/java/com/pulkit/datastructures_algorithms/done/trees/LongestConsecutiveBranch.java |
| 290 | +new file mode 100644 |
| 291 | +index 0000000..151166a |
| 292 | +--- /dev/null |
| 293 | ++++ b/src/main/java/com/pulkit/datastructures_algorithms/done/trees/LongestConsecutiveBranch.java |
| 294 | +@@ -0,0 +1,20 @@ |
| 295 | ++package com.pulkit.datastructures_algorithms.done.trees; |
| 296 | ++ |
| 297 | ++public class LongestConsecutiveBranch { |
| 298 | ++ public static void main(String[] args) { |
| 299 | ++ |
| 300 | ++ } |
| 301 | ++ |
| 302 | ++ static void find(TreeNode node) { |
| 303 | ++ if (node == null) |
| 304 | ++ return; |
| 305 | ++ |
| 306 | ++ if (node.leftChild.data == node.data + 1) { |
| 307 | ++ find(node.leftChild); |
| 308 | ++ } |
| 309 | ++ |
| 310 | ++ if (node.rightChild.data == node.data + 1) { |
| 311 | ++ find(node.rightChild); |
| 312 | ++ } |
| 313 | ++ } |
| 314 | ++} |
| 315 | +\ No newline at end of file |
| 316 | +diff --git a/src/main/java/com/pulkit/datastructures_algorithms/done/trees/PathsIncludingNonRootAndNonLeafWithGivenSum.java b/src/main/java/com/pulkit/datastructures_algorithms/done/trees/PathsIncludingNonRootAndNonLeafWithGivenSum.java |
| 317 | +new file mode 100644 |
| 318 | +index 0000000..3641ccd |
| 319 | +--- /dev/null |
| 320 | ++++ b/src/main/java/com/pulkit/datastructures_algorithms/done/trees/PathsIncludingNonRootAndNonLeafWithGivenSum.java |
| 321 | +@@ -0,0 +1,94 @@ |
| 322 | ++package com.pulkit.datastructures_algorithms.done.trees; |
| 323 | ++ |
| 324 | ++import java.util.LinkedList; |
| 325 | ++import java.util.List; |
| 326 | ++ |
| 327 | ++public class PathsIncludingNonRootAndNonLeafWithGivenSum { |
| 328 | ++ public static void main(String[] args) { |
| 329 | ++ //TreeNode root = singlePathWith7AsGivenSum(); |
| 330 | ++ |
| 331 | ++ TreeNode root = dualPathWith7AsGivenSum(); |
| 332 | ++ LinkedList<List<TreeNode>> paths = new LinkedList<>(); |
| 333 | ++ findPaths(new Sum(7), root, paths, new LinkedList<>()); |
| 334 | ++ } |
| 335 | ++ |
| 336 | ++ /* |
| 337 | ++ 1 |
| 338 | ++ 2 3 |
| 339 | ++ 4 5 3 |
| 340 | ++ */ |
| 341 | ++ |
| 342 | ++ public static void findPaths(Sum givenSum, TreeNode root, List<List<TreeNode>> paths, |
| 343 | ++ List<TreeNode> currentPath) { |
| 344 | ++ if (root == null) { |
| 345 | ++ return; |
| 346 | ++ } |
| 347 | ++ |
| 348 | ++ currentPath.add(root); |
| 349 | ++ givenSum.subtract(root.data); |
| 350 | ++ |
| 351 | ++ if (givenSum.value == 0) { |
| 352 | ++ List<TreeNode> tempList = new LinkedList<>(currentPath); |
| 353 | ++ paths.add(tempList); |
| 354 | ++ } |
| 355 | ++ |
| 356 | ++ if (givenSum.value < 0) { |
| 357 | ++ if (!currentPath.isEmpty()) { |
| 358 | ++ TreeNode topElement = currentPath.remove(0); |
| 359 | ++ givenSum.add(topElement.data); |
| 360 | ++ } |
| 361 | ++ } |
| 362 | ++ if (root.leftChild != null) { |
| 363 | ++ findPaths(givenSum, root.leftChild, paths, currentPath); |
| 364 | ++ if (!currentPath.isEmpty()) { |
| 365 | ++ currentPath.remove(currentPath.size() - 1); |
| 366 | ++ givenSum.add(root.leftChild.data); |
| 367 | ++ } |
| 368 | ++ } |
| 369 | ++ if (root.rightChild != null) { |
| 370 | ++ findPaths(givenSum, root.rightChild, paths, currentPath); |
| 371 | ++ if (!currentPath.isEmpty()) { |
| 372 | ++ currentPath.remove(currentPath.size() - 1); |
| 373 | ++ givenSum.add(root.rightChild.data); |
| 374 | ++ } |
| 375 | ++ } |
| 376 | ++ } |
| 377 | ++ |
| 378 | ++ private static class Sum { |
| 379 | ++ int value; |
| 380 | ++ |
| 381 | ++ public Sum(int value) { |
| 382 | ++ this.value = value; |
| 383 | ++ } |
| 384 | ++ |
| 385 | ++ void add(int element) { |
| 386 | ++ this.value += element; |
| 387 | ++ } |
| 388 | ++ |
| 389 | ++ void subtract(int element) { |
| 390 | ++ this.value -= element; |
| 391 | ++ } |
| 392 | ++ } |
| 393 | ++ |
| 394 | ++ private static TreeNode singlePathWith7AsGivenSum() { |
| 395 | ++ TreeNode six = new TreeNode(6, null, null); |
| 396 | ++ TreeNode five = new TreeNode(5, null, null); |
| 397 | ++ TreeNode four = new TreeNode(4, null, null); |
| 398 | ++ TreeNode three = new TreeNode(3, six, null); |
| 399 | ++ TreeNode two = new TreeNode(2, four, five); |
| 400 | ++ TreeNode root = new TreeNode(1, two, three); |
| 401 | ++ |
| 402 | ++ return root; |
| 403 | ++ } |
| 404 | ++ |
| 405 | ++ private static TreeNode dualPathWith7AsGivenSum() { |
| 406 | ++ TreeNode six = new TreeNode(3, null, null); |
| 407 | ++ TreeNode five = new TreeNode(5, null, null); |
| 408 | ++ TreeNode four = new TreeNode(4, null, null); |
| 409 | ++ TreeNode three = new TreeNode(3, six, null); |
| 410 | ++ TreeNode two = new TreeNode(2, four, five); |
| 411 | ++ TreeNode root = new TreeNode(1, two, three); |
| 412 | ++ |
| 413 | ++ return root; |
| 414 | ++ } |
| 415 | ++} |
| 416 | +diff --git a/src/main/java/com/pulkit/datastructures_algorithms/todo/DeleteANodeFromBinaryTree.java b/src/main/java/com/pulkit/datastructures_algorithms/todo/DeleteANodeFromBinaryTree.java |
| 417 | +new file mode 100644 |
| 418 | +index 0000000..33e74b5 |
| 419 | +--- /dev/null |
| 420 | ++++ b/src/main/java/com/pulkit/datastructures_algorithms/todo/DeleteANodeFromBinaryTree.java |
| 421 | +@@ -0,0 +1,7 @@ |
| 422 | ++package com.pulkit.datastructures_algorithms.todo; |
| 423 | ++ |
| 424 | ++public class DeleteANodeFromBinaryTree { |
| 425 | ++ public static void main(String[] args) { |
| 426 | ++ |
| 427 | ++ } |
| 428 | ++} |
| 429 | +diff --git a/src/main/java/com/pulkit/jyoti/GraphNode.java b/src/main/java/com/pulkit/jyoti/GraphNode.java |
| 430 | +new file mode 100644 |
| 431 | +index 0000000..79ae8ae |
| 432 | +--- /dev/null |
| 433 | ++++ b/src/main/java/com/pulkit/jyoti/GraphNode.java |
| 434 | +@@ -0,0 +1,137 @@ |
| 435 | ++package com.pulkit.jyoti; |
| 436 | ++ |
| 437 | ++import java.util.*; |
| 438 | ++ |
| 439 | ++public class GraphNode { |
| 440 | ++ int data; |
| 441 | ++ List<GraphNode> neighbours; |
| 442 | ++ |
| 443 | ++ public GraphNode(int data, List<GraphNode> neighbours) { |
| 444 | ++ this.data = data; |
| 445 | ++ this.neighbours = neighbours; |
| 446 | ++ } |
| 447 | ++ |
| 448 | ++ public void setNeighbours(List<GraphNode> neighbours) { |
| 449 | ++ this.neighbours = neighbours; |
| 450 | ++ } |
| 451 | ++ |
| 452 | ++ @Override |
| 453 | ++ public boolean equals(Object o) { |
| 454 | ++ if (this == o) return true; |
| 455 | ++ if (!(o instanceof GraphNode)) return false; |
| 456 | ++ GraphNode graphNode = (GraphNode) o; |
| 457 | ++ return data == graphNode.data && |
| 458 | ++ neighbours.equals(graphNode.neighbours); |
| 459 | ++ } |
| 460 | ++ |
| 461 | ++ @Override |
| 462 | ++ public int hashCode() { |
| 463 | ++ return Objects.hash(data, neighbours); |
| 464 | ++ } |
| 465 | ++ |
| 466 | ++ @Override |
| 467 | ++ public String toString() { |
| 468 | ++ return "" + data; |
| 469 | ++ } |
| 470 | ++ |
| 471 | ++ public static void main(String[] args) { |
| 472 | ++ GraphNode Node5 = new GraphNode(5, null); |
| 473 | ++ List<GraphNode> neighbours1 = new LinkedList<>(); |
| 474 | ++ neighbours1.add(Node5); |
| 475 | ++ |
| 476 | ++ GraphNode Node2 = new GraphNode(2, null); |
| 477 | ++ GraphNode Node3 = new GraphNode(3, null); |
| 478 | ++ GraphNode Node4 = new GraphNode(4, neighbours1); |
| 479 | ++ |
| 480 | ++ |
| 481 | ++ List<GraphNode> neighbours = new LinkedList<>(); |
| 482 | ++ neighbours.add(Node2); |
| 483 | ++ neighbours.add(Node3); |
| 484 | ++ neighbours.add(Node4); |
| 485 | ++ GraphNode initialNode = new GraphNode(1, neighbours); |
| 486 | ++ |
| 487 | ++ |
| 488 | ++ List<GraphNode> neighbours3 = new LinkedList<>(); |
| 489 | ++ neighbours3.add(initialNode); |
| 490 | ++ |
| 491 | ++ Node2.setNeighbours(neighbours3); |
| 492 | ++ Node3.setNeighbours(neighbours3); |
| 493 | ++ Node4.setNeighbours(neighbours3); |
| 494 | ++ |
| 495 | ++ DFS(initialNode, new HashSet<>()); |
| 496 | ++ } |
| 497 | ++ |
| 498 | ++ public static void DFS(GraphNode node, Set<GraphNode> visited) { |
| 499 | ++ //Stack Data structure or recursion |
| 500 | ++ if (node == null) |
| 501 | ++ return; |
| 502 | ++ |
| 503 | ++ if (visited.contains(node)) |
| 504 | ++ return; |
| 505 | ++ |
| 506 | ++ System.out.println(node); |
| 507 | ++ visited.add(node); |
| 508 | ++ |
| 509 | ++ List<GraphNode> neighbours = node.neighbours; |
| 510 | ++ |
| 511 | ++ if (neighbours != null) { |
| 512 | ++ for (GraphNode currentNode : neighbours) { |
| 513 | ++ DFS(currentNode, visited); |
| 514 | ++ } |
| 515 | ++ } |
| 516 | ++ } |
| 517 | ++ |
| 518 | ++ public static void findTopologicalSort(Stack<GraphNode> stack, List<GraphNode> graph, |
| 519 | ++ Set<GraphNode> visited) { |
| 520 | ++ if (graph == null) |
| 521 | ++ return; |
| 522 | ++ |
| 523 | ++ for (GraphNode node : graph) { |
| 524 | ++ |
| 525 | ++ |
| 526 | ++ } |
| 527 | ++ } |
| 528 | ++} |
| 529 | ++ |
| 530 | ++//C->B->A->D->E / C->B->D->A->E / C->B->E->D->E // 2 or 3 more |
| 531 | ++ |
| 532 | ++ |
| 533 | ++// B E C A D |
| 534 | ++ |
| 535 | ++ |
| 536 | ++//(JRE/JVM)java A |
| 537 | ++/* |
| 538 | ++ |
| 539 | ++file : A.java -> A.class |
| 540 | ++class A { |
| 541 | ++B b = new B(); |
| 542 | ++} |
| 543 | ++ |
| 544 | ++file : D.java -> D.class |
| 545 | ++class D { |
| 546 | ++B b = new B(); |
| 547 | ++} |
| 548 | ++ |
| 549 | ++file : E.java -> E.class |
| 550 | ++class E { |
| 551 | ++B b = new B(); |
| 552 | ++} |
| 553 | ++ |
| 554 | ++file : B.java -> B.class |
| 555 | ++class B { |
| 556 | ++C c = new C(); |
| 557 | ++} |
| 558 | ++ |
| 559 | ++file : C.java -> C.class |
| 560 | ++class C { |
| 561 | ++// |
| 562 | ++} |
| 563 | ++ |
| 564 | ++*/ |
| 565 | ++/* |
| 566 | ++Agra - mumbai |
| 567 | ++mumbai - delhi |
| 568 | ++delhi - kolkata |
| 569 | ++kolata - bangluru |
| 570 | ++Agra - banluru |
| 571 | ++ */ |
| 572 | +\ No newline at end of file |
| 573 | +diff --git a/to be committed/BoxStackingProblem.java b/to be committed/BoxStackingProblem.java |
| 574 | +new file mode 100644 |
| 575 | +index 0000000..d1a3888 |
| 576 | +--- /dev/null |
| 577 | ++++ b/to be committed/BoxStackingProblem.java |
| 578 | +@@ -0,0 +1,46 @@ |
| 579 | ++package com.mckinsey.engage.app.apis.mappings; |
| 580 | ++ |
| 581 | ++public class BoxStackingProblem { |
| 582 | ++ public static void main(String[] args) { |
| 583 | ++ int[] lengths = {4, 1, 4, 10}; |
| 584 | ++ int[] breadths = {6, 2, 5, 12}; |
| 585 | ++ int[] heights = {7, 3, 6, 32}; |
| 586 | ++ |
| 587 | ++ int maxHeight = findMaxHeight(Integer.MAX_VALUE, 0, lengths, breadths, heights, 0); |
| 588 | ++ System.out.println(maxHeight); |
| 589 | ++ } |
| 590 | ++ |
| 591 | ++ private static int findMaxHeight(int currentBaseArea, int index, int[] lengths, int[] breadths, |
| 592 | ++ int[] heights, int maxHeight) { |
| 593 | ++ if (lengths.length == 0 || breadths.length == 0 || heights.length == 0) |
| 594 | ++ return 0; |
| 595 | ++ |
| 596 | ++ if (index >= lengths.length) |
| 597 | ++ return maxHeight; |
| 598 | ++ |
| 599 | ++ int result = 0; |
| 600 | ++ |
| 601 | ++ int firstBasePossibility = lengths[index] * breadths[index]; |
| 602 | ++ if (firstBasePossibility < currentBaseArea) { |
| 603 | ++ result = Math.max(result, findMaxHeight(firstBasePossibility, index + 1, lengths, |
| 604 | ++ breadths, heights, maxHeight + heights[index])); |
| 605 | ++ } |
| 606 | ++ |
| 607 | ++ int secondBasePossibility = lengths[index] * heights[index]; |
| 608 | ++ if (secondBasePossibility < currentBaseArea) { |
| 609 | ++ result = Math.max(result, findMaxHeight(secondBasePossibility, index + 1, lengths, |
| 610 | ++ breadths, heights, maxHeight + breadths[index])); |
| 611 | ++ } |
| 612 | ++ |
| 613 | ++ int thirdBasePossibility = heights[index] * breadths[index]; |
| 614 | ++ if (thirdBasePossibility < currentBaseArea) { |
| 615 | ++ result = Math.max(result, findMaxHeight(thirdBasePossibility, index + 1, lengths, |
| 616 | ++ breadths, heights, maxHeight + heights[index])); |
| 617 | ++ } |
| 618 | ++ |
| 619 | ++ int anotherResult = findMaxHeight(currentBaseArea, index + 1, lengths, |
| 620 | ++ breadths, heights, maxHeight); |
| 621 | ++ |
| 622 | ++ return Math.max(result, anotherResult); |
| 623 | ++ } |
| 624 | ++} |
| 625 | +diff --git a/to be committed/ConnectNodesAtSameLevel.java b/to be committed/ConnectNodesAtSameLevel.java |
| 626 | +new file mode 100644 |
| 627 | +index 0000000..d1fcded |
| 628 | +--- /dev/null |
| 629 | ++++ b/to be committed/ConnectNodesAtSameLevel.java |
| 630 | +@@ -0,0 +1,181 @@ |
| 631 | ++//package com.mckinsey.engage.app.apis.mappings; |
| 632 | ++// |
| 633 | ++// /* |
| 634 | ++// 1 |
| 635 | ++// 2 3 |
| 636 | ++// 4 5 6 n |
| 637 | ++//n n n 8 9 10 n n |
| 638 | ++// 11 |
| 639 | ++//*/ |
| 640 | ++// |
| 641 | ++//public class ConnectNodesAtSameLevel { |
| 642 | ++// public static void main(String[] args) { |
| 643 | ++// |
| 644 | ++// } |
| 645 | ++// |
| 646 | ++// private static void connect(TreeNodeWithNext root) { |
| 647 | ++// if (root == null) |
| 648 | ++// return; |
| 649 | ++// if (areBothChildNull(root)) { |
| 650 | ++// return; |
| 651 | ++// } |
| 652 | ++// |
| 653 | ++// while (root != null) { |
| 654 | ++// TreeNodeWithNext temp = root; |
| 655 | ++// while (temp != null) { |
| 656 | ++// if (areBothChildNotNull(temp)) { |
| 657 | ++// temp.left.next = temp.right; |
| 658 | ++// } |
| 659 | ++// TreeNodeWithNext next = getNextSibling(temp); |
| 660 | ++// if (next != null) { |
| 661 | ++// if (temp.right != null) { |
| 662 | ++// temp.right = next; |
| 663 | ++// } else if (temp.left != null) { |
| 664 | ++// temp.left = next; |
| 665 | ++// } |
| 666 | ++// } |
| 667 | ++// temp = moveTempToItsNextTillOneOfChildIsNotNull(temp); |
| 668 | ++// } |
| 669 | ++// |
| 670 | ++// root = getNextChild(root); |
| 671 | ++// } |
| 672 | ++// } |
| 673 | ++// |
| 674 | ++// private static TreeNodeWithNext getNextSibling(TreeNodeWithNext temp) { |
| 675 | ++// if (temp == null) |
| 676 | ++// return null; |
| 677 | ++// |
| 678 | ++// if (temp.next == null) { |
| 679 | ++// return null; |
| 680 | ++// } |
| 681 | ++// |
| 682 | ++// return getNextChild(temp.next); |
| 683 | ++// } |
| 684 | ++// |
| 685 | ++// private static TreeNodeWithNext getNextChild(TreeNodeWithNext temp) { |
| 686 | ++// if (temp == null) |
| 687 | ++// return temp; |
| 688 | ++// |
| 689 | ++// while (areBothChildNull(temp)) { |
| 690 | ++// temp = temp.next; |
| 691 | ++// } |
| 692 | ++// |
| 693 | ++// if (temp.left != null) |
| 694 | ++// return temp.left; |
| 695 | ++// |
| 696 | ++// if (temp.right != null) |
| 697 | ++// return temp.right; |
| 698 | ++// |
| 699 | ++// return null; |
| 700 | ++// } |
| 701 | ++// |
| 702 | ++// private static TreeNodeWithNext moveTempToItsNextTillOneOfChildIsNotNull(TreeNodeWithNext temp) { |
| 703 | ++// if (temp.next == null) { |
| 704 | ++// return null; |
| 705 | ++// } |
| 706 | ++// |
| 707 | ++// while (areBothChildNull(temp.next)) { |
| 708 | ++// temp = temp.next; |
| 709 | ++// } |
| 710 | ++// |
| 711 | ++// return temp.next; |
| 712 | ++// } |
| 713 | ++// |
| 714 | ++// private static boolean areBothChildNull(TreeNodeWithNext temp) { |
| 715 | ++// if (temp == null) |
| 716 | ++// return false; |
| 717 | ++// |
| 718 | ++// return temp.left == null && temp.right == null; |
| 719 | ++// } |
| 720 | ++// |
| 721 | ++// private static boolean areBothChildNotNull(TreeNodeWithNext temp) { |
| 722 | ++// if (temp == null) |
| 723 | ++// return false; |
| 724 | ++// |
| 725 | ++// return temp.left != null && temp.right != null; |
| 726 | ++// } |
| 727 | ++//} |
| 728 | ++// |
| 729 | ++// |
| 730 | ++//class TreeNodeWithNext { |
| 731 | ++// int val; |
| 732 | ++// TreeNodeWithNext left; |
| 733 | ++// TreeNodeWithNext right; |
| 734 | ++// TreeNodeWithNext next; |
| 735 | ++// |
| 736 | ++// TreeNodeWithNext() { |
| 737 | ++// } |
| 738 | ++// |
| 739 | ++// TreeNodeWithNext(int val) { |
| 740 | ++// this.val = val; |
| 741 | ++// } |
| 742 | ++// |
| 743 | ++// TreeNodeWithNext(int val, TreeNodeWithNext left, TreeNodeWithNext right) { |
| 744 | ++// this.val = val; |
| 745 | ++// this.left = left; |
| 746 | ++// this.right = right; |
| 747 | ++// } |
| 748 | ++//} |
| 749 | ++///* |
| 750 | ++//while root: |
| 751 | ++// while root: |
| 752 | ++// if root.left: |
| 753 | ++// next_level_first_node=root.left |
| 754 | ++// break |
| 755 | ++// if root.right: |
| 756 | ++// next_level_first_node=root.right |
| 757 | ++// break |
| 758 | ++// root = root.next |
| 759 | ++// while root: |
| 760 | ++// if root.left and root.right: |
| 761 | ++// root.left.next = root.right |
| 762 | ++// while root.next: |
| 763 | ++// if root.next.left: |
| 764 | ++// root.right.next=root.next.left |
| 765 | ++// break |
| 766 | ++// if root.next.right: |
| 767 | ++// root.right.next=root.next.right |
| 768 | ++// break |
| 769 | ++// root = root.next |
| 770 | ++// elif root.left: |
| 771 | ++// while root.next: |
| 772 | ++// if root.next.left: |
| 773 | ++// root.left.next=root.next.left |
| 774 | ++// break |
| 775 | ++// if root.next.right: |
| 776 | ++// root.left.next=root.next.right |
| 777 | ++// break |
| 778 | ++// root = root.next |
| 779 | ++// elif root.right: |
| 780 | ++// while root.next: |
| 781 | ++// if root.next.left: |
| 782 | ++// root.right.next=root.next.left |
| 783 | ++// break |
| 784 | ++// if root.next.right: |
| 785 | ++// root.right.next=root.next.right |
| 786 | ++// break |
| 787 | ++// root = root.next |
| 788 | ++// |
| 789 | ++// root = next_level_first_node |
| 790 | ++// |
| 791 | ++///* |
| 792 | ++// 1 |
| 793 | ++// 2 3 |
| 794 | ++// 4 5 6 n |
| 795 | ++//n n n 8 9 10 n n |
| 796 | ++// 11 |
| 797 | ++//*/ |
| 798 | ++// |
| 799 | ++// |
| 800 | ++// |
| 801 | ++// |
| 802 | ++// |
| 803 | ++// |
| 804 | ++// |
| 805 | ++// |
| 806 | ++// |
| 807 | ++// |
| 808 | ++// |
| 809 | ++// |
| 810 | ++// |
| 811 | ++// */ |
| 812 | +diff --git a/to be committed/CuttingARod.java b/to be committed/CuttingARod.java |
| 813 | +new file mode 100644 |
| 814 | +index 0000000..7a675d0 |
| 815 | +--- /dev/null |
| 816 | ++++ b/to be committed/CuttingARod.java |
| 817 | +@@ -0,0 +1,65 @@ |
| 818 | ++package com.mckinsey.engage.app.apis.mappings; |
| 819 | ++ |
| 820 | ++public class CuttingARod { |
| 821 | ++ public static void main(String[] args) { |
| 822 | ++ int[] length = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 823 | ++ int[] profit = {1, 5, 8, 9, 10, 17, 17, 20}; |
| 824 | ++ int totalLength = 8; |
| 825 | ++ |
| 826 | ++// int[] length = {1, 2, 3, 4}; |
| 827 | ++// int[] profit = {2, 5, 7, 8}; |
| 828 | ++// int totalLength = 5; |
| 829 | ++ |
| 830 | ++// int[] length = {1, 2}; |
| 831 | ++// int[] profit = {2, 5}; |
| 832 | ++// int totalLength = 2; |
| 833 | ++ |
| 834 | ++// int[] length = {1, 2, 3}; |
| 835 | ++// int[] profit = {2, 5, 7}; |
| 836 | ++// int totalLength = 3; |
| 837 | ++ |
| 838 | ++ int currentProfit = 0; |
| 839 | ++ int index = 0; |
| 840 | ++ System.out.println(findMaxProfit(length, profit, currentProfit, totalLength, index)); |
| 841 | ++ } |
| 842 | ++ |
| 843 | ++ private static int findMaxProfit(int[] length, int[] profit, int currentProfit, |
| 844 | ++ int totalLength, int index) { |
| 845 | ++ |
| 846 | ++ if (index >= profit.length) { |
| 847 | ++ return currentProfit; |
| 848 | ++ } |
| 849 | ++ |
| 850 | ++ if (totalLength < 0) { |
| 851 | ++ if (index > 0) { |
| 852 | ++ return currentProfit - profit[index]; |
| 853 | ++ } |
| 854 | ++ return 0; |
| 855 | ++ } |
| 856 | ++ |
| 857 | ++ if (totalLength == 0) { |
| 858 | ++ return currentProfit; |
| 859 | ++ } |
| 860 | ++ |
| 861 | ++ int maxProfitWhenCutIsMadeAtCurrentLength = findMaxProfit(length, profit, |
| 862 | ++ currentProfit + profit[index], totalLength - length[index], |
| 863 | ++ index); |
| 864 | ++ |
| 865 | ++ int maxProfitWhenCutIsMadeAtNextLength = 0; |
| 866 | ++ |
| 867 | ++ if (index + 1 < profit.length) { |
| 868 | ++ maxProfitWhenCutIsMadeAtNextLength = findMaxProfit(length, profit, |
| 869 | ++ currentProfit + profit[index + 1], totalLength - length[index + 1], |
| 870 | ++ index + 1); |
| 871 | ++ } |
| 872 | ++ |
| 873 | ++ int maxProfitWhenCutIsNotMadeAtCurrentLength = findMaxProfit(length, profit, currentProfit, |
| 874 | ++ totalLength, index + 1); |
| 875 | ++ |
| 876 | ++ int result = Math.max(maxProfitWhenCutIsMadeAtCurrentLength, maxProfitWhenCutIsNotMadeAtCurrentLength); |
| 877 | ++ int finalResult = Math.max(maxProfitWhenCutIsMadeAtNextLength, result); |
| 878 | ++ |
| 879 | ++// int finalResult = Math.max(maxProfitWhenCutIsMadeAtNextLength, maxProfitWhenCutIsNotMadeAtCurrentLength); |
| 880 | ++ return finalResult; |
| 881 | ++ } |
| 882 | ++} |
| 883 | +\ No newline at end of file |
| 884 | +diff --git a/to be committed/FlattenBinaryTreeToSinglyLinkedList.java b/to be committed/FlattenBinaryTreeToSinglyLinkedList.java |
| 885 | +new file mode 100644 |
| 886 | +index 0000000..d47baf0 |
| 887 | +--- /dev/null |
| 888 | ++++ b/to be committed/FlattenBinaryTreeToSinglyLinkedList.java |
| 889 | +@@ -0,0 +1,86 @@ |
| 890 | ++package com.mckinsey.engage.app.apis.mappings; |
| 891 | ++ |
| 892 | ++import java.util.LinkedList; |
| 893 | ++import java.util.PriorityQueue; |
| 894 | ++import java.util.Queue; |
| 895 | ++ |
| 896 | ++public class FlattenBinaryTreeToSinglyLinkedList { |
| 897 | ++ public static void main(String[] args) { |
| 898 | ++ TreeNode six = new TreeNode(6, null, null); |
| 899 | ++ TreeNode four = new TreeNode(4, null, null); |
| 900 | ++ TreeNode three = new TreeNode(3, null, null); |
| 901 | ++ TreeNode five = new TreeNode(5, null, six); |
| 902 | ++ TreeNode two = new TreeNode(2, three, four); |
| 903 | ++ TreeNode root = new TreeNode(1, two, five); |
| 904 | ++ |
| 905 | ++ flatten(root); |
| 906 | ++ printLevelOrder(root); |
| 907 | ++ } |
| 908 | ++ |
| 909 | ++ static void printLevelOrder(TreeNode root) { |
| 910 | ++ PriorityQueue priorityQueue = new PriorityQueue(); |
| 911 | ++ |
| 912 | ++ |
| 913 | ++ Queue<TreeNode> queue = new LinkedList<TreeNode>(); |
| 914 | ++ queue.add(root); |
| 915 | ++ while (!queue.isEmpty()) { |
| 916 | ++ TreeNode tempNode = queue.poll(); |
| 917 | ++ System.out.print(tempNode.val + " "); |
| 918 | ++ |
| 919 | ++ if (tempNode.left != null) { |
| 920 | ++ queue.add(tempNode.left); |
| 921 | ++ } |
| 922 | ++ |
| 923 | ++ if (tempNode.right != null) { |
| 924 | ++ queue.add(tempNode.right); |
| 925 | ++ } |
| 926 | ++ } |
| 927 | ++ } |
| 928 | ++ |
| 929 | ++ private static void flatten(TreeNode root) { |
| 930 | ++ if (root == null) |
| 931 | ++ return; |
| 932 | ++ |
| 933 | ++ if (root.left == null && root.right == null) |
| 934 | ++ return; |
| 935 | ++ |
| 936 | ++ flatten(root.left); |
| 937 | ++ flatten(root.right); |
| 938 | ++ |
| 939 | ++ TreeNode temp = root.right; |
| 940 | ++ root.right = root.left; |
| 941 | ++ root.left = null; |
| 942 | ++ TreeNode anotherTemp = root.right; |
| 943 | ++ |
| 944 | ++ while (anotherTemp != null && anotherTemp.right != null) { |
| 945 | ++ anotherTemp = anotherTemp.right; |
| 946 | ++ } |
| 947 | ++ |
| 948 | ++ if (anotherTemp != null) { |
| 949 | ++ anotherTemp.right = temp; |
| 950 | ++ } |
| 951 | ++ |
| 952 | ++ if (anotherTemp == null) { |
| 953 | ++ root.right = temp; |
| 954 | ++ } |
| 955 | ++ } |
| 956 | ++} |
| 957 | ++ |
| 958 | ++class TreeNode { |
| 959 | ++ int val; |
| 960 | ++ TreeNode left; |
| 961 | ++ TreeNode right; |
| 962 | ++ |
| 963 | ++ TreeNode() { |
| 964 | ++ } |
| 965 | ++ |
| 966 | ++ TreeNode(int val) { |
| 967 | ++ this.val = val; |
| 968 | ++ } |
| 969 | ++ |
| 970 | ++ TreeNode(int val, TreeNode left, TreeNode right) { |
| 971 | ++ this.val = val; |
| 972 | ++ this.left = left; |
| 973 | ++ this.right = right; |
| 974 | ++ } |
| 975 | ++} |
| 976 | +\ No newline at end of file |
| 977 | +diff --git a/to be committed/GenerateParentheses.java b/to be committed/GenerateParentheses.java |
| 978 | +new file mode 100644 |
| 979 | +index 0000000..eb653c8 |
| 980 | +--- /dev/null |
| 981 | ++++ b/to be committed/GenerateParentheses.java |
| 982 | +@@ -0,0 +1,25 @@ |
| 983 | ++package com.mckinsey.engage.app.apis.mappings; |
| 984 | ++ |
| 985 | ++import java.util.LinkedList; |
| 986 | ++import java.util.List; |
| 987 | ++ |
| 988 | ++public class GenerateParentheses { |
| 989 | ++ public static void main(String[] args) { |
| 990 | ++ LinkedList<String> result = new LinkedList<>(); |
| 991 | ++ generate(result, "", 3, 0, 0); |
| 992 | ++ System.out.println(result); |
| 993 | ++ } |
| 994 | ++ |
| 995 | ++ private static void generate(List<String> result, String parentheses, int halfSize, int openingBraces, int closingBraces) { |
| 996 | ++ if (parentheses.length() == halfSize * 2) { |
| 997 | ++ result.add(parentheses); |
| 998 | ++ return; |
| 999 | ++ } |
| 1000 | ++ if (openingBraces < halfSize) { |
| 1001 | ++ generate(result, parentheses + "(", halfSize, openingBraces + 1, closingBraces); |
| 1002 | ++ } |
| 1003 | ++ if (closingBraces < openingBraces) { |
| 1004 | ++ generate(result, parentheses + ")", halfSize, openingBraces, closingBraces + 1); |
| 1005 | ++ } |
| 1006 | ++ } |
| 1007 | ++} |
| 1008 | +\ No newline at end of file |
| 1009 | +diff --git a/to be committed/HouseThief.java b/to be committed/HouseThief.java |
| 1010 | +new file mode 100644 |
| 1011 | +index 0000000..67ee158 |
| 1012 | +--- /dev/null |
| 1013 | ++++ b/to be committed/HouseThief.java |
| 1014 | +@@ -0,0 +1,25 @@ |
| 1015 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1016 | ++ |
| 1017 | ++public class HouseThief { |
| 1018 | ++ public static void main(String[] args) { |
| 1019 | ++ int[] profits = {3, 1, 2, 4, 5, 2}; |
| 1020 | ++ |
| 1021 | ++ int maxProfit = findMaxProfit(profits, 0, 0); |
| 1022 | ++ System.out.println(maxProfit); |
| 1023 | ++ } |
| 1024 | ++ |
| 1025 | ++ private static int findMaxProfit(int[] profit, int maxProfitTillNow, int index) { |
| 1026 | ++ if (profit.length <= 0) |
| 1027 | ++ return 0; |
| 1028 | ++ |
| 1029 | ++ if (index >= profit.length) |
| 1030 | ++ return maxProfitTillNow; |
| 1031 | ++ |
| 1032 | ++ int possibleMaxProfit = findMaxProfit(profit, maxProfitTillNow + profit[index], |
| 1033 | ++ index + 2); |
| 1034 | ++ |
| 1035 | ++ int anotherPossibleMaxProfit = findMaxProfit(profit, maxProfitTillNow, index + 1); |
| 1036 | ++ |
| 1037 | ++ return Math.max(possibleMaxProfit, anotherPossibleMaxProfit); |
| 1038 | ++ } |
| 1039 | ++} |
| 1040 | +diff --git a/to be committed/KeyPadProblem.java b/to be committed/KeyPadProblem.java |
| 1041 | +new file mode 100644 |
| 1042 | +index 0000000..a8f2961 |
| 1043 | +--- /dev/null |
| 1044 | ++++ b/to be committed/KeyPadProblem.java |
| 1045 | +@@ -0,0 +1,51 @@ |
| 1046 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1047 | ++ |
| 1048 | ++import java.util.HashMap; |
| 1049 | ++import java.util.LinkedList; |
| 1050 | ++import java.util.List; |
| 1051 | ++import java.util.Map; |
| 1052 | ++ |
| 1053 | ++public class KeyPadProblem { |
| 1054 | ++ private static Map<String, String> digitLetterMap = new HashMap(); |
| 1055 | ++ |
| 1056 | ++ public static void main(String[] args) { |
| 1057 | ++ digitLetterMap.put("2", "abc"); |
| 1058 | ++ digitLetterMap.put("3", "def"); |
| 1059 | ++ digitLetterMap.put("4", "ghi"); |
| 1060 | ++ digitLetterMap.put("5", "jkl"); |
| 1061 | ++ digitLetterMap.put("6", "mno"); |
| 1062 | ++ digitLetterMap.put("7", "pqrs"); |
| 1063 | ++ digitLetterMap.put("8", "tuv"); |
| 1064 | ++ digitLetterMap.put("9", "wxyz"); |
| 1065 | ++ |
| 1066 | ++ List<String> strings = generateAllLetterRepresentation("23", 0); |
| 1067 | ++ System.out.println(strings); |
| 1068 | ++ } |
| 1069 | ++ |
| 1070 | ++ private static List<String> generateAllLetterRepresentation(String number, int index) { |
| 1071 | ++ if (number == null) { |
| 1072 | ++ List<String> emptyStringList = new LinkedList<>(); |
| 1073 | ++ emptyStringList.add(""); |
| 1074 | ++ return emptyStringList; |
| 1075 | ++ } |
| 1076 | ++ |
| 1077 | ++ if (index >= number.length()) { |
| 1078 | ++ List<String> emptyStringList = new LinkedList<>(); |
| 1079 | ++ emptyStringList.add(""); |
| 1080 | ++ return emptyStringList; |
| 1081 | ++ } |
| 1082 | ++ |
| 1083 | ++ List<String> currentRepresentations = generateAllLetterRepresentation(number, index + 1); |
| 1084 | ++ List<String> newRepresentations = new LinkedList<>(); |
| 1085 | ++ |
| 1086 | ++ for (String representation : currentRepresentations) { |
| 1087 | ++ Character digit = number.charAt(index); |
| 1088 | ++ String letters = digitLetterMap.get(digit.toString()); |
| 1089 | ++ for (int localIndex = 0; localIndex < letters.length(); localIndex++) { |
| 1090 | ++ char localCharacter = letters.charAt(localIndex); |
| 1091 | ++ newRepresentations.add(localCharacter + representation); |
| 1092 | ++ } |
| 1093 | ++ } |
| 1094 | ++ return newRepresentations; |
| 1095 | ++ } |
| 1096 | ++} |
| 1097 | +\ No newline at end of file |
| 1098 | +diff --git a/to be committed/KeyPadProblemm.java b/to be committed/KeyPadProblemm.java |
| 1099 | +new file mode 100644 |
| 1100 | +index 0000000..3d0d991 |
| 1101 | +--- /dev/null |
| 1102 | ++++ b/to be committed/KeyPadProblemm.java |
| 1103 | +@@ -0,0 +1,59 @@ |
| 1104 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1105 | ++ |
| 1106 | ++import java.util.HashMap; |
| 1107 | ++import java.util.LinkedList; |
| 1108 | ++import java.util.List; |
| 1109 | ++import java.util.Map; |
| 1110 | ++ |
| 1111 | ++public class KeyPadProblemm { |
| 1112 | ++ private static Map<String, String> digitLetterMap = new HashMap(); |
| 1113 | ++ |
| 1114 | ++ public static void main(String[] args) { |
| 1115 | ++ digitLetterMap.put("2", "abc"); |
| 1116 | ++ digitLetterMap.put("3", "def"); |
| 1117 | ++ digitLetterMap.put("4", "ghi"); |
| 1118 | ++ digitLetterMap.put("5", "jkl"); |
| 1119 | ++ digitLetterMap.put("6", "mno"); |
| 1120 | ++ digitLetterMap.put("7", "pqrs"); |
| 1121 | ++ digitLetterMap.put("8", "tuv"); |
| 1122 | ++ digitLetterMap.put("9", "wxyz"); |
| 1123 | ++ |
| 1124 | ++ List<String> strings = generateAllCombinations("245", 0); |
| 1125 | ++ System.out.println(strings.size()); |
| 1126 | ++ System.out.println(strings); |
| 1127 | ++ } |
| 1128 | ++ |
| 1129 | ++ //null case (base case && ending case) |
| 1130 | ++ // 1 node or 1 size |
| 1131 | ++ // recurrence relation |
| 1132 | ++ |
| 1133 | ++ private static List<String> generateAllCombinations(String number, int index) { |
| 1134 | ++ if (number == null) |
| 1135 | ++ return new LinkedList<>(); |
| 1136 | ++ |
| 1137 | ++ if (number.length() == 0) |
| 1138 | ++ return new LinkedList<>(); |
| 1139 | ++ |
| 1140 | ++ if (index >= number.length()) { |
| 1141 | ++ |
| 1142 | ++ LinkedList<String> emptyList = new LinkedList<>(); |
| 1143 | ++ emptyList.add(""); |
| 1144 | ++ return emptyList; |
| 1145 | ++ } |
| 1146 | ++ |
| 1147 | ++ List<String> combinationsGeneratedTillNow = generateAllCombinations(number, index + 1); |
| 1148 | ++ List<String> result = new LinkedList<>(); |
| 1149 | ++ |
| 1150 | ++ Character currentDigit = number.charAt(index); |
| 1151 | ++ String letter = digitLetterMap.get(currentDigit.toString()); //"abc" |
| 1152 | ++ |
| 1153 | ++ for (String combination : combinationsGeneratedTillNow) { |
| 1154 | ++ for (int localIndex = 0; localIndex < letter.length(); localIndex++) { |
| 1155 | ++ Character currentAlphabet = letter.charAt(localIndex); |
| 1156 | ++ result.add(currentAlphabet + combination); |
| 1157 | ++ } |
| 1158 | ++ } |
| 1159 | ++ |
| 1160 | ++ return result; |
| 1161 | ++ } |
| 1162 | ++} |
| 1163 | +diff --git a/to be committed/KnapsackProblem.java b/to be committed/KnapsackProblem.java |
| 1164 | +new file mode 100644 |
| 1165 | +index 0000000..2f73ece |
| 1166 | +--- /dev/null |
| 1167 | ++++ b/to be committed/KnapsackProblem.java |
| 1168 | +@@ -0,0 +1,224 @@ |
| 1169 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1170 | ++ |
| 1171 | ++import java.util.HashMap; |
| 1172 | ++import java.util.Map; |
| 1173 | ++import java.util.Objects; |
| 1174 | ++ |
| 1175 | ++//class GFG { |
| 1176 | ++// public static void main(String[] args) { |
| 1177 | ++// Scanner scanner = new Scanner(System.in); |
| 1178 | ++// int numberOfTestCases = scanner.nextInt(); |
| 1179 | ++// |
| 1180 | ++// while (numberOfTestCases-- > 0) { |
| 1181 | ++// int numberOfItems = scanner.nextInt(); |
| 1182 | ++// int maximumCapacity = scanner.nextInt(); |
| 1183 | ++// |
| 1184 | ++// int[] profits = new int[numberOfItems]; |
| 1185 | ++// int[] weights = new int[numberOfItems]; |
| 1186 | ++// |
| 1187 | ++// for (int index = 0; index < numberOfItems; index++) { |
| 1188 | ++// profits[index] = scanner.nextInt(); |
| 1189 | ++// } |
| 1190 | ++// for (int index = 0; index < numberOfItems; index++) { |
| 1191 | ++// weights[index] = scanner.nextInt(); |
| 1192 | ++// } |
| 1193 | ++// |
| 1194 | ++// int maxProfit = 0; |
| 1195 | ++// int currentWeight = 0; |
| 1196 | ++// int index = 0; |
| 1197 | ++// |
| 1198 | ++// Map<MaxProfitCurrentWeightPair, Integer> cache = new HashMap<>(); |
| 1199 | ++// |
| 1200 | ++// GFG gfg = new GFG(); |
| 1201 | ++// Integer maxProfit1 = gfg.findMaxProfit(maxProfit, maximumCapacity, maximumCapacity, index, |
| 1202 | ++// profits, weights, cache); |
| 1203 | ++// System.out.println(maxProfit1); |
| 1204 | ++// } |
| 1205 | ++// } |
| 1206 | ++// |
| 1207 | ++// private Integer findMaxProfit(int maxProfitTillNow, int currentWeight, int totalWeightAllowed, |
| 1208 | ++// int index, int[] profits, int[] weights, |
| 1209 | ++// Map<MaxProfitCurrentWeightPair, Integer> cache) { |
| 1210 | ++// if (totalWeightAllowed < 0) { |
| 1211 | ++// return 0; |
| 1212 | ++// } |
| 1213 | ++// |
| 1214 | ++// if (currentWeight < 0) { |
| 1215 | ++// return maxProfitTillNow - profits[index - 1]; |
| 1216 | ++// } |
| 1217 | ++// |
| 1218 | ++// if (index >= profits.length) { |
| 1219 | ++// return maxProfitTillNow; |
| 1220 | ++// } |
| 1221 | ++// |
| 1222 | ++// if (totalWeightAllowed == currentWeight) { |
| 1223 | ++// return maxProfitTillNow; |
| 1224 | ++// } |
| 1225 | ++// |
| 1226 | ++// if (cache.containsKey(new MaxProfitCurrentWeightPair(maxProfitTillNow, currentWeight))) { |
| 1227 | ++// return cache.get(new MaxProfitCurrentWeightPair(maxProfitTillNow, currentWeight)); |
| 1228 | ++// } else { |
| 1229 | ++// int maxProfitWhenCurrentWeightAdded = findMaxProfit(maxProfitTillNow + profits[index], |
| 1230 | ++// currentWeight - weights[index], totalWeightAllowed, index + 1, |
| 1231 | ++// profits, weights, cache); |
| 1232 | ++// |
| 1233 | ++// |
| 1234 | ++// int maxProfitWhenCurrentWeightNotAdded = findMaxProfit(maxProfitTillNow, currentWeight, |
| 1235 | ++// totalWeightAllowed, index + 1, profits, weights, cache); |
| 1236 | ++// |
| 1237 | ++// int result = Math.max(maxProfitWhenCurrentWeightAdded, maxProfitWhenCurrentWeightNotAdded); |
| 1238 | ++// |
| 1239 | ++// MaxProfitCurrentWeightPair key = new MaxProfitCurrentWeightPair(maxProfitTillNow, currentWeight); |
| 1240 | ++// cache.put(key, result); |
| 1241 | ++// return result; |
| 1242 | ++// } |
| 1243 | ++// } |
| 1244 | ++//} |
| 1245 | ++// |
| 1246 | ++//class MaxProfitCurrentWeightPair { |
| 1247 | ++// int maxProfit; |
| 1248 | ++// int currentWeight; |
| 1249 | ++// |
| 1250 | ++// public MaxProfitCurrentWeightPair(int maxProfit, int currentWeight) { |
| 1251 | ++// this.maxProfit = maxProfit; |
| 1252 | ++// this.currentWeight = currentWeight; |
| 1253 | ++// } |
| 1254 | ++// |
| 1255 | ++// @Override |
| 1256 | ++// public boolean equals(Object o) { |
| 1257 | ++// if (this == o) return true; |
| 1258 | ++// if (o == null || getClass() != o.getClass()) return false; |
| 1259 | ++// MaxProfitCurrentWeightPair maxProfitCurrentWeightPair = (MaxProfitCurrentWeightPair) o; |
| 1260 | ++// return maxProfit == maxProfitCurrentWeightPair.maxProfit && |
| 1261 | ++// currentWeight == maxProfitCurrentWeightPair.currentWeight; |
| 1262 | ++// } |
| 1263 | ++// |
| 1264 | ++// @Override |
| 1265 | ++// public int hashCode() { |
| 1266 | ++// return Objects.hash(maxProfit, currentWeight); |
| 1267 | ++// } |
| 1268 | ++//} |
| 1269 | ++ |
| 1270 | ++public class KnapsackProblem { |
| 1271 | ++ private static Map<MaxProfitCurrentWeightPair, Integer> cache = new HashMap<>(); |
| 1272 | ++ |
| 1273 | ++ public static void main(String[] args) { |
| 1274 | ++// int totalWeightAllowed = 50; |
| 1275 | ++// int[] profits = {60, 100, 120}; |
| 1276 | ++// int[] weights = {10, 20, 30}; |
| 1277 | ++ |
| 1278 | ++// int totalWeightAllowed = 50; |
| 1279 | ++// int[] profits = {60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70}; |
| 1280 | ++// int[] weights = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 1281 | ++ |
| 1282 | ++ int totalWeightAllowed = 50; |
| 1283 | ++ int[] profits = {60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70, 60, 100, 120, 100, 20, 30, 40, 50, 60, 70}; |
| 1284 | ++ int[] weights = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50}; |
| 1285 | ++ |
| 1286 | ++ System.out.println("profits size " + profits.length); |
| 1287 | ++ System.out.println("weights size " + weights.length); |
| 1288 | ++ |
| 1289 | ++// int totalWeightAllowed = 4; |
| 1290 | ++// int[] profits = {1, 2, 3}; |
| 1291 | ++// int[] weights = {4, 5, 1}; |
| 1292 | ++ |
| 1293 | ++// int totalWeightAllowed = 3; |
| 1294 | ++// int[] profits = {1, 2, 3}; |
| 1295 | ++// int[] weights = {4, 5, 6}; |
| 1296 | ++ |
| 1297 | ++// int totalWeightAllowed = 5; |
| 1298 | ++// int[] profits = {4, 5, 3, 7}; |
| 1299 | ++// int[] weights = {2, 3, 1, 4}; |
| 1300 | ++ |
| 1301 | ++// //int totalWeightAllowed = 7; |
| 1302 | ++// int totalWeightAllowed = 6; |
| 1303 | ++// int[] profits = {1, 6, 10, 16}; |
| 1304 | ++// int[] weights = {1, 2, 3, 5}; |
| 1305 | ++ |
| 1306 | ++// int totalWeightAllowed = 10; |
| 1307 | ++// int[] profits = {10, 40, 30, 50}; |
| 1308 | ++// int[] weights = {5, 4, 6, 3}; |
| 1309 | ++ |
| 1310 | ++ int maxProfit = 0; |
| 1311 | ++ int currentWeight = 0; |
| 1312 | ++ int index = 0; |
| 1313 | ++ |
| 1314 | ++ Counter counter = new Counter(0L); |
| 1315 | ++ |
| 1316 | ++ System.out.println(findMaxProfit(maxProfit, currentWeight, totalWeightAllowed, index, |
| 1317 | ++ profits, weights, counter)); |
| 1318 | ++ System.out.println(counter.value); |
| 1319 | ++ } |
| 1320 | ++ |
| 1321 | ++ private static Integer findMaxProfit(int maxProfitTillNow, int currentWeight, int totalWeightAllowed, |
| 1322 | ++ int index, int[] profits, int[] weights, Counter counter) { |
| 1323 | ++ if (totalWeightAllowed < 0) { |
| 1324 | ++ return 0; |
| 1325 | ++ } |
| 1326 | ++ |
| 1327 | ++ if (currentWeight > totalWeightAllowed) { |
| 1328 | ++ return maxProfitTillNow - profits[index - 1]; |
| 1329 | ++ } |
| 1330 | ++ |
| 1331 | ++ if (index >= profits.length) { |
| 1332 | ++ return maxProfitTillNow; |
| 1333 | ++ } |
| 1334 | ++ |
| 1335 | ++ if (totalWeightAllowed == currentWeight) { |
| 1336 | ++ return maxProfitTillNow; |
| 1337 | ++ } |
| 1338 | ++ |
| 1339 | ++ if (cache.containsKey(new MaxProfitCurrentWeightPair(maxProfitTillNow, currentWeight))) { |
| 1340 | ++ //System.out.println("========saved call========="); |
| 1341 | ++ //counter.value++; |
| 1342 | ++ return cache.get(new MaxProfitCurrentWeightPair(maxProfitTillNow, currentWeight)); |
| 1343 | ++ } else { |
| 1344 | ++ counter.value++; |
| 1345 | ++ int maxProfitWhenCurrentWeightAdded = findMaxProfit(maxProfitTillNow + profits[index], |
| 1346 | ++ currentWeight + weights[index], totalWeightAllowed, index + 1, |
| 1347 | ++ profits, weights, counter); |
| 1348 | ++ |
| 1349 | ++ |
| 1350 | ++ int maxProfitWhenCurrentWeightNotAdded = findMaxProfit(maxProfitTillNow, currentWeight, |
| 1351 | ++ totalWeightAllowed, index + 1, profits, weights, counter); |
| 1352 | ++ |
| 1353 | ++ int result = Math.max(maxProfitWhenCurrentWeightAdded, maxProfitWhenCurrentWeightNotAdded); |
| 1354 | ++ |
| 1355 | ++ MaxProfitCurrentWeightPair key = new MaxProfitCurrentWeightPair(maxProfitTillNow, currentWeight); |
| 1356 | ++ cache.put(key, result); |
| 1357 | ++ return result; |
| 1358 | ++ } |
| 1359 | ++ } |
| 1360 | ++} |
| 1361 | ++ |
| 1362 | ++class MaxProfitCurrentWeightPair { |
| 1363 | ++ int maxProfit; |
| 1364 | ++ int currentWeight; |
| 1365 | ++ |
| 1366 | ++ public MaxProfitCurrentWeightPair(int maxProfit, int currentWeight) { |
| 1367 | ++ this.maxProfit = maxProfit; |
| 1368 | ++ this.currentWeight = currentWeight; |
| 1369 | ++ } |
| 1370 | ++ |
| 1371 | ++ @Override |
| 1372 | ++ public boolean equals(Object o) { |
| 1373 | ++ if (this == o) return true; |
| 1374 | ++ if (o == null || getClass() != o.getClass()) return false; |
| 1375 | ++ MaxProfitCurrentWeightPair maxProfitCurrentWeightPair = (MaxProfitCurrentWeightPair) o; |
| 1376 | ++ return maxProfit == maxProfitCurrentWeightPair.maxProfit && |
| 1377 | ++ currentWeight == maxProfitCurrentWeightPair.currentWeight; |
| 1378 | ++ } |
| 1379 | ++ |
| 1380 | ++ @Override |
| 1381 | ++ public int hashCode() { |
| 1382 | ++ return Objects.hash(maxProfit, currentWeight); |
| 1383 | ++ } |
| 1384 | ++} |
| 1385 | ++ |
| 1386 | ++class Counter { |
| 1387 | ++ Long value; |
| 1388 | ++ |
| 1389 | ++ public Counter(Long value) { |
| 1390 | ++ this.value = value; |
| 1391 | ++ } |
| 1392 | ++} |
| 1393 | +\ No newline at end of file |
| 1394 | +diff --git a/to be committed/Permutation.java b/to be committed/Permutation.java |
| 1395 | +new file mode 100644 |
| 1396 | +index 0000000..5f11275 |
| 1397 | +--- /dev/null |
| 1398 | ++++ b/to be committed/Permutation.java |
| 1399 | +@@ -0,0 +1,43 @@ |
| 1400 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1401 | ++ |
| 1402 | ++import java.util.LinkedList; |
| 1403 | ++import java.util.List; |
| 1404 | ++ |
| 1405 | ++import static java.lang.System.out; |
| 1406 | ++ |
| 1407 | ++public class Permutation { |
| 1408 | ++ public static void main(String[] args) { |
| 1409 | ++ List<String> permutations = getPermutations("abcd", 0); |
| 1410 | ++ out.println(permutations); |
| 1411 | ++ out.println(permutations.size()); |
| 1412 | ++ } |
| 1413 | ++ |
| 1414 | ++ static List<String> getPermutations(String string, int k) { |
| 1415 | ++ if (string.length() == 0 || k >= string.length()) { |
| 1416 | ++ List<String> tempList = new LinkedList<>(); |
| 1417 | ++ tempList.add(""); |
| 1418 | ++ |
| 1419 | ++ return tempList; |
| 1420 | ++ } |
| 1421 | ++ |
| 1422 | ++ List<String> currentPermutations = getPermutations(string, k + 1); |
| 1423 | ++ char firstCharacter = string.charAt(k); |
| 1424 | ++ |
| 1425 | ++ List<String> newPermutationList = new LinkedList<>(); |
| 1426 | ++ |
| 1427 | ++ for (String permutation : currentPermutations) { |
| 1428 | ++ for (int j = 0; j <= permutation.length(); j++) { |
| 1429 | ++ String temp = ""; |
| 1430 | ++ |
| 1431 | ++ if (permutation.equals("")) { |
| 1432 | ++ temp = temp + firstCharacter; |
| 1433 | ++ } else { |
| 1434 | ++ temp = permutation.substring(0, j) + firstCharacter + permutation.substring(j); |
| 1435 | ++ } |
| 1436 | ++ newPermutationList.add(temp); |
| 1437 | ++ } |
| 1438 | ++ } |
| 1439 | ++ |
| 1440 | ++ return newPermutationList; |
| 1441 | ++ } |
| 1442 | ++} |
| 1443 | +\ No newline at end of file |
| 1444 | +diff --git a/to be committed/PriorityQueueTrial.java b/to be committed/PriorityQueueTrial.java |
| 1445 | +new file mode 100644 |
| 1446 | +index 0000000..a736a3d |
| 1447 | +--- /dev/null |
| 1448 | ++++ b/to be committed/PriorityQueueTrial.java |
| 1449 | +@@ -0,0 +1,14 @@ |
| 1450 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1451 | ++ |
| 1452 | ++import java.util.PriorityQueue; |
| 1453 | ++ |
| 1454 | ++public class PriorityQueueTrial { |
| 1455 | ++ public static void main(String[] args) { |
| 1456 | ++// PriorityQueue pq = new PriorityQueue(); |
| 1457 | ++// pq.offer(); |
| 1458 | ++// pq.poll() |
| 1459 | ++ |
| 1460 | ++ } |
| 1461 | ++ |
| 1462 | ++ |
| 1463 | ++} |
| 1464 | +diff --git a/to be committed/RottenTomatoes.java b/to be committed/RottenTomatoes.java |
| 1465 | +new file mode 100644 |
| 1466 | +index 0000000..58f062f |
| 1467 | +--- /dev/null |
| 1468 | ++++ b/to be committed/RottenTomatoes.java |
| 1469 | +@@ -0,0 +1,27 @@ |
| 1470 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1471 | ++ |
| 1472 | ++public class RottenTomatoes { |
| 1473 | ++ public static void main(String[] args) { |
| 1474 | ++ |
| 1475 | ++ } |
| 1476 | ++ |
| 1477 | ++// private static int findRottenTomatoes(int[][] grid, int xCoordinate, int yCoordinate, int minutes) { |
| 1478 | ++// |
| 1479 | ++// if (grid[xCoordinate + 1][yCoordinate] == 2) { |
| 1480 | ++// findRottenTomatoes(grid, xCoordinate + 1, yCoordinate, minutes + 1); |
| 1481 | ++// } |
| 1482 | ++// |
| 1483 | ++// if (grid[xCoordinate][yCoordinate + 1] == 2) { |
| 1484 | ++// findRottenTomatoes(grid, xCoordinate, yCoordinate + 1, minutes + 1); |
| 1485 | ++// } |
| 1486 | ++// |
| 1487 | ++// if (grid[xCoordinate - 1][yCoordinate] == 2) { |
| 1488 | ++// findRottenTomatoes(grid, xCoordinate - 1, yCoordinate, minutes + 1); |
| 1489 | ++// } |
| 1490 | ++// |
| 1491 | ++// if (grid[xCoordinate][yCoordinate - 1] == 2) { |
| 1492 | ++// findRottenTomatoes(grid, xCoordinate, yCoordinate - 1, minutes + 1); |
| 1493 | ++// } |
| 1494 | ++// |
| 1495 | ++// } |
| 1496 | ++} |
| 1497 | +\ No newline at end of file |
| 1498 | +diff --git a/to be committed/SearchSystem.java b/to be committed/SearchSystem.java |
| 1499 | +new file mode 100644 |
| 1500 | +index 0000000..05d5c2e |
| 1501 | +--- /dev/null |
| 1502 | ++++ b/to be committed/SearchSystem.java |
| 1503 | +@@ -0,0 +1,88 @@ |
| 1504 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1505 | ++ |
| 1506 | ++import java.util.LinkedList; |
| 1507 | ++import java.util.List; |
| 1508 | ++ |
| 1509 | ++public class SearchSystem { |
| 1510 | ++ public static void main(String[] args) { |
| 1511 | ++ TrieNode root = new TrieNode('/'); |
| 1512 | ++ String text = "rajjorani"; |
| 1513 | ++ String anotherText = "rajjorand"; |
| 1514 | ++ insertInTrie(root, text.toLowerCase(), 0); |
| 1515 | ++ insertInTrie(root, anotherText.toLowerCase(), 0); |
| 1516 | ++ |
| 1517 | ++ StringBuilder builder = new StringBuilder(); |
| 1518 | ++ LinkedList<StringBuilder> result = new LinkedList<>(); |
| 1519 | ++ searchInTrie(root, "raj", 0, builder, result); |
| 1520 | ++ } |
| 1521 | ++ |
| 1522 | ++ private static void insertInTrie(TrieNode root, String text, int index) { |
| 1523 | ++ if (text == null || text.length() == 0) { |
| 1524 | ++ return; |
| 1525 | ++ } |
| 1526 | ++ |
| 1527 | ++ if (index >= text.length()) { |
| 1528 | ++ return; |
| 1529 | ++ } |
| 1530 | ++ |
| 1531 | ++ char character = text.charAt(index); |
| 1532 | ++ int indexOfCharacter = character - 'a'; |
| 1533 | ++ TrieNode[] currentNodeChildren = root.children; |
| 1534 | ++ |
| 1535 | ++ TrieNode trieNode = currentNodeChildren[indexOfCharacter]; |
| 1536 | ++ |
| 1537 | ++ if (trieNode == null) { |
| 1538 | ++ trieNode = new TrieNode(character); |
| 1539 | ++ currentNodeChildren[indexOfCharacter] = trieNode; |
| 1540 | ++ } |
| 1541 | ++ |
| 1542 | ++ if (index == text.length() - 1) { |
| 1543 | ++ trieNode.endOfWord = true; |
| 1544 | ++ return; |
| 1545 | ++ } |
| 1546 | ++ |
| 1547 | ++ insertInTrie(trieNode, text, index + 1); |
| 1548 | ++ } |
| 1549 | ++ |
| 1550 | ++ private static void searchInTrie(TrieNode node, String prefix, int index, StringBuilder builder, |
| 1551 | ++ List<StringBuilder> result) { |
| 1552 | ++ if (prefix == null && prefix.length() == 0) { |
| 1553 | ++ return; |
| 1554 | ++ } |
| 1555 | ++ |
| 1556 | ++ if (node.endOfWord == true) { |
| 1557 | ++ StringBuilder tempBuilder = new StringBuilder(builder); |
| 1558 | ++ result.add(tempBuilder); |
| 1559 | ++ } |
| 1560 | ++ |
| 1561 | ++ if (index < prefix.length()) { |
| 1562 | ++ TrieNode[] currentChildren = node.children; |
| 1563 | ++ char currentCharacterInPrefix = prefix.charAt(index); |
| 1564 | ++ int indexOfCharacter = currentCharacterInPrefix - 'a'; |
| 1565 | ++ TrieNode trieNode = currentChildren[indexOfCharacter]; |
| 1566 | ++ |
| 1567 | ++ builder.append(trieNode.value); |
| 1568 | ++ |
| 1569 | ++ searchInTrie(trieNode, prefix, index + 1, builder, result); |
| 1570 | ++ |
| 1571 | ++ } else { |
| 1572 | ++ for (TrieNode trieNode : node.children) { |
| 1573 | ++ if (trieNode != null) { |
| 1574 | ++ builder.append(trieNode.value); |
| 1575 | ++ searchInTrie(trieNode, prefix, index, builder, result); |
| 1576 | ++ builder.deleteCharAt(builder.length() - 1); |
| 1577 | ++ } |
| 1578 | ++ } |
| 1579 | ++ } |
| 1580 | ++ } |
| 1581 | ++} |
| 1582 | ++ |
| 1583 | ++class TrieNode { |
| 1584 | ++ Character value; |
| 1585 | ++ TrieNode[] children = new TrieNode[26]; |
| 1586 | ++ boolean endOfWord; |
| 1587 | ++ |
| 1588 | ++ public TrieNode(Character value) { |
| 1589 | ++ this.value = value; |
| 1590 | ++ } |
| 1591 | ++} |
| 1592 | +\ No newline at end of file |
| 1593 | +diff --git a/to be committed/SkyLineProblem.java b/to be committed/SkyLineProblem.java |
| 1594 | +new file mode 100644 |
| 1595 | +index 0000000..7062a62 |
| 1596 | +--- /dev/null |
| 1597 | ++++ b/to be committed/SkyLineProblem.java |
| 1598 | +@@ -0,0 +1,70 @@ |
| 1599 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1600 | ++ |
| 1601 | ++import java.util.LinkedList; |
| 1602 | ++import java.util.List; |
| 1603 | ++ |
| 1604 | ++public class SkyLineProblem { |
| 1605 | ++ public static void main(String[] args) { |
| 1606 | ++ |
| 1607 | ++ } |
| 1608 | ++ |
| 1609 | ++ private static List<Building> constructSkyline(List<Building> buildings) { |
| 1610 | ++ if (buildings == null) { |
| 1611 | ++ return new LinkedList<>(); |
| 1612 | ++ } |
| 1613 | ++ |
| 1614 | ++ if (buildings.size() == 0) { |
| 1615 | ++ return new LinkedList<>(); |
| 1616 | ++ } |
| 1617 | ++ |
| 1618 | ++ List<Building> skyline = new LinkedList<>(); |
| 1619 | ++ Building firstBuilding = buildings.get(0); |
| 1620 | ++ int currentLeftXCoordinate = firstBuilding.leftXCoordinate; |
| 1621 | ++ int currentRightXCoordinate = firstBuilding.rightXCoordinate; |
| 1622 | ++ int currentHeight = firstBuilding.height; |
| 1623 | ++ |
| 1624 | ++ for (int index = 1; index < buildings.size(); index++) { |
| 1625 | ++ Building building = buildings.get(index); |
| 1626 | ++ int nextLeftXCoordinate = building.leftXCoordinate; |
| 1627 | ++ int nextRightXCoordinate = building.rightXCoordinate; |
| 1628 | ++ int nextHeight = building.height; |
| 1629 | ++ |
| 1630 | ++ if (currentRightXCoordinate > nextLeftXCoordinate) { |
| 1631 | ++ if (currentRightXCoordinate > nextRightXCoordinate) { |
| 1632 | ++ if (currentHeight > nextHeight) { |
| 1633 | ++ skyline.add(new Building(currentLeftXCoordinate, currentRightXCoordinate, currentHeight)); |
| 1634 | ++ } else { |
| 1635 | ++ skyline.add(new Building(currentLeftXCoordinate, nextLeftXCoordinate, currentHeight)); |
| 1636 | ++ skyline.add(new Building(nextLeftXCoordinate, nextRightXCoordinate, nextHeight)); |
| 1637 | ++ skyline.add(new Building(nextRightXCoordinate, currentRightXCoordinate, currentHeight)); |
| 1638 | ++ } |
| 1639 | ++ } else { |
| 1640 | ++ if (currentHeight > nextHeight) { |
| 1641 | ++ skyline.add(new Building(currentLeftXCoordinate, currentRightXCoordinate, currentHeight)); |
| 1642 | ++ skyline.add(new Building(currentRightXCoordinate, nextRightXCoordinate, nextHeight)); |
| 1643 | ++ } else { |
| 1644 | ++ skyline.add(new Building(currentLeftXCoordinate, currentRightXCoordinate, currentHeight)); |
| 1645 | ++ skyline.add(new Building(currentRightXCoordinate, nextRightXCoordinate, nextHeight)); |
| 1646 | ++ } |
| 1647 | ++ } |
| 1648 | ++ } else { |
| 1649 | ++ skyline.add(new Building(currentLeftXCoordinate, currentRightXCoordinate, currentHeight)); |
| 1650 | ++ skyline.add(new Building(currentRightXCoordinate, nextLeftXCoordinate, 0)); |
| 1651 | ++ skyline.add(new Building(nextLeftXCoordinate, nextRightXCoordinate, nextHeight)); |
| 1652 | ++ } |
| 1653 | ++ } |
| 1654 | ++ return skyline; |
| 1655 | ++ } |
| 1656 | ++} |
| 1657 | ++ |
| 1658 | ++class Building { |
| 1659 | ++ int leftXCoordinate; |
| 1660 | ++ int rightXCoordinate; |
| 1661 | ++ int height; |
| 1662 | ++ |
| 1663 | ++ public Building(int leftXCoordinate, int rightXCoordinate, int height) { |
| 1664 | ++ this.leftXCoordinate = leftXCoordinate; |
| 1665 | ++ this.rightXCoordinate = rightXCoordinate; |
| 1666 | ++ this.height = height; |
| 1667 | ++ } |
| 1668 | ++} |
| 1669 | +\ No newline at end of file |
| 1670 | +diff --git a/to be committed/SubSetSumProblem.java b/to be committed/SubSetSumProblem.java |
| 1671 | +new file mode 100644 |
| 1672 | +index 0000000..597f892 |
| 1673 | +--- /dev/null |
| 1674 | ++++ b/to be committed/SubSetSumProblem.java |
| 1675 | +@@ -0,0 +1,129 @@ |
| 1676 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1677 | ++ |
| 1678 | ++import java.util.HashMap; |
| 1679 | ++import java.util.Map; |
| 1680 | ++import java.util.Objects; |
| 1681 | ++ |
| 1682 | ++public class SubSetSumProblem { |
| 1683 | ++ |
| 1684 | ++ public static void main(String[] args) { |
| 1685 | ++ //int array[] = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}; |
| 1686 | ++ //int target = 30; |
| 1687 | ++ |
| 1688 | ++ int array[] = {3, 34, 4, 12, 5, 2}; |
| 1689 | ++ int target = 9; |
| 1690 | ++ |
| 1691 | ++ //int numberOfSubsets = findSubSet(target, array, 0, new HashMap<>(), 0); |
| 1692 | ++ //int numberOfSubsets = findSubSet(target, array, 0, new HashMap<>(), 0); |
| 1693 | ++ |
| 1694 | ++ boolean result = findSubSetIfPresent(target, array, 0, new HashMap<>()); |
| 1695 | ++ System.out.println(result); |
| 1696 | ++ } |
| 1697 | ++ |
| 1698 | ++ private static Boolean findSubSetIfPresent(int target, int array[], int index, |
| 1699 | ++ Map<Pair, Boolean> cache) { |
| 1700 | ++ System.out.println("Target-> " + target + " " + "index-> " + index); |
| 1701 | ++ |
| 1702 | ++ if (target < 0) |
| 1703 | ++ return false; |
| 1704 | ++ |
| 1705 | ++ if (target == 0) { |
| 1706 | ++ return true; |
| 1707 | ++ } |
| 1708 | ++ |
| 1709 | ++ Boolean value = false; |
| 1710 | ++ |
| 1711 | ++ for (int tempIndex = index; tempIndex < array.length; tempIndex++) { |
| 1712 | ++ Pair key = new Pair(target, tempIndex); |
| 1713 | ++ |
| 1714 | ++ if (!cache.containsKey(key)) { |
| 1715 | ++ value = value || findSubSetIfPresent(target - array[tempIndex], |
| 1716 | ++ array, tempIndex + 1, cache); |
| 1717 | ++ //|| findSubSetIfPresent(target, array, tempIndex + 1, cache); |
| 1718 | ++ |
| 1719 | ++ cache.put(key, value); |
| 1720 | ++ //return value; |
| 1721 | ++ } else { |
| 1722 | ++ value = cache.get(key); |
| 1723 | ++ } |
| 1724 | ++ } |
| 1725 | ++ |
| 1726 | ++ return value; |
| 1727 | ++ //return false; |
| 1728 | ++ } |
| 1729 | ++ |
| 1730 | ++ private static Boolean findSubSetIfPresentByRajat(int target, int array[], int index, |
| 1731 | ++ Map<Pair, Boolean> cache) { |
| 1732 | ++ System.out.println("Target-> " + target + " " + "index-> " + index); |
| 1733 | ++ |
| 1734 | ++ if (target < 0) |
| 1735 | ++ return false; |
| 1736 | ++ |
| 1737 | ++ if (target == 0) { |
| 1738 | ++ return true; |
| 1739 | ++ } |
| 1740 | ++ |
| 1741 | ++ for (int tempIndex = index; tempIndex < array.length; tempIndex++) { |
| 1742 | ++ Pair key = new Pair(target, index); |
| 1743 | ++ |
| 1744 | ++ if (!cache.containsKey(key)) { |
| 1745 | ++ boolean value = findSubSetIfPresentByRajat(target - array[tempIndex], array, tempIndex + 1, cache) |
| 1746 | ++ || findSubSetIfPresentByRajat(target, array, tempIndex + 1, cache); |
| 1747 | ++ |
| 1748 | ++ cache.put(key, value); |
| 1749 | ++ return value; |
| 1750 | ++ } else { |
| 1751 | ++ return cache.get(key); |
| 1752 | ++ } |
| 1753 | ++ } |
| 1754 | ++ return false; |
| 1755 | ++ } |
| 1756 | ++ |
| 1757 | ++// private static int findSubSet(int target, int array[], int index, |
| 1758 | ++// Map<Pair, Integer> cache, int result) { |
| 1759 | ++// if (target < 0) |
| 1760 | ++// return 0; |
| 1761 | ++// |
| 1762 | ++// if (target == 0) { |
| 1763 | ++// return 1; |
| 1764 | ++// } |
| 1765 | ++// |
| 1766 | ++// for (int tempIndex = index; tempIndex < array.length; tempIndex++) { |
| 1767 | ++// Pair key = new Pair(target, index); |
| 1768 | ++// int value; |
| 1769 | ++// |
| 1770 | ++// if (!cache.containsKey(key)) { |
| 1771 | ++// value = findSubSet(target - array[tempIndex], array, tempIndex + 1, cache, result); |
| 1772 | ++// cache.put(key, value); |
| 1773 | ++// } else { |
| 1774 | ++// value = cache.get(key); |
| 1775 | ++// } |
| 1776 | ++// result += value; |
| 1777 | ++// } |
| 1778 | ++// return result; |
| 1779 | ++// } |
| 1780 | ++} |
| 1781 | ++ |
| 1782 | ++class Pair { |
| 1783 | ++ int target; |
| 1784 | ++ int index; |
| 1785 | ++ |
| 1786 | ++ public Pair(int target, int index) { |
| 1787 | ++ this.target = target; |
| 1788 | ++ this.index = index; |
| 1789 | ++ } |
| 1790 | ++ |
| 1791 | ++ @Override |
| 1792 | ++ public boolean equals(Object o) { |
| 1793 | ++ if (this == o) return true; |
| 1794 | ++ if (o == null || getClass() != o.getClass()) return false; |
| 1795 | ++ Pair pair = (Pair) o; |
| 1796 | ++ return target == pair.target && |
| 1797 | ++ index == pair.index; |
| 1798 | ++ } |
| 1799 | ++ |
| 1800 | ++ @Override |
| 1801 | ++ public int hashCode() { |
| 1802 | ++ return Objects.hash(target, index); |
| 1803 | ++ } |
| 1804 | ++} |
| 1805 | +\ No newline at end of file |
| 1806 | +diff --git a/to be committed/Subset.java b/to be committed/Subset.java |
| 1807 | +new file mode 100644 |
| 1808 | +index 0000000..b5a22fc |
| 1809 | +--- /dev/null |
| 1810 | ++++ b/to be committed/Subset.java |
| 1811 | +@@ -0,0 +1,39 @@ |
| 1812 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1813 | ++ |
| 1814 | ++import java.util.LinkedList; |
| 1815 | ++import java.util.List; |
| 1816 | ++ |
| 1817 | ++public class Subset { |
| 1818 | ++ public static void main(String[] args) { |
| 1819 | ++ List<StringBuilder> result = permute(new StringBuilder("abc"), 0); |
| 1820 | ++ System.out.println(result); |
| 1821 | ++ } |
| 1822 | ++ |
| 1823 | ++ static List<StringBuilder> permute(StringBuilder string, int index) { |
| 1824 | ++ if (string.length() == 0) { |
| 1825 | ++ List<StringBuilder> baseCaseList = new LinkedList<>(); |
| 1826 | ++ baseCaseList.add(new StringBuilder("")); |
| 1827 | ++ return baseCaseList; |
| 1828 | ++ } |
| 1829 | ++ |
| 1830 | ++ if (index >= string.length()) { |
| 1831 | ++ List<StringBuilder> baseCaseList = new LinkedList<>(); |
| 1832 | ++ baseCaseList.add(new StringBuilder("")); |
| 1833 | ++ return baseCaseList; |
| 1834 | ++ } |
| 1835 | ++ |
| 1836 | ++ List<StringBuilder> permutationsTillNow = permute(string, index + 1); |
| 1837 | ++ List<StringBuilder> temp = new LinkedList<>(permutationsTillNow); |
| 1838 | ++ |
| 1839 | ++ Character currentCharacter = string.charAt(index); |
| 1840 | ++ |
| 1841 | ++ for (StringBuilder permutation : temp) { |
| 1842 | ++ permutationsTillNow.add(new StringBuilder() |
| 1843 | ++ .append(currentCharacter) |
| 1844 | ++ .append(permutation)); |
| 1845 | ++ } |
| 1846 | ++ temp.add(new StringBuilder().append(currentCharacter)); |
| 1847 | ++ |
| 1848 | ++ return permutationsTillNow; |
| 1849 | ++ } |
| 1850 | ++} |
| 1851 | +\ No newline at end of file |
| 1852 | +diff --git a/to be committed/Subtree.java b/to be committed/Subtree.java |
| 1853 | +new file mode 100644 |
| 1854 | +index 0000000..d93ec81 |
| 1855 | +--- /dev/null |
| 1856 | ++++ b/to be committed/Subtree.java |
| 1857 | +@@ -0,0 +1,49 @@ |
| 1858 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1859 | ++ |
| 1860 | ++public class Subtree { |
| 1861 | ++ public static void main(String[] args) { |
| 1862 | ++ |
| 1863 | ++ } |
| 1864 | ++ /* root root1 |
| 1865 | ++ |
| 1866 | ++ 1 1 |
| 1867 | ++ 2 3 2 3 |
| 1868 | ++ 4 5 6 7 5 4 6 7 |
| 1869 | ++ |
| 1870 | ++ */ |
| 1871 | ++ // find is root1 is subtree of root |
| 1872 | ++ |
| 1873 | ++ private static boolean isSubtree(TreeNode root, TreeNode root1, TreeNode root1fixed) { |
| 1874 | ++ if (root == null && root1 == null) { |
| 1875 | ++ return true; |
| 1876 | ++ } |
| 1877 | ++ |
| 1878 | ++ if (root == null || root1 == null) { |
| 1879 | ++ return false; |
| 1880 | ++ } |
| 1881 | ++ |
| 1882 | ++// if (isSame(root, root1)) { |
| 1883 | ++// return true; |
| 1884 | ++// } |
| 1885 | ++ |
| 1886 | ++ if (root.val == root1.val) { |
| 1887 | ++ return isSubtree(root.left, root1.left, root1fixed) && isSubtree(root.right, root1.right, root1fixed); |
| 1888 | ++ } else { |
| 1889 | ++ return isSubtree(root.left, root1fixed, root1fixed) || isSubtree(root.right, root1fixed, root1fixed); |
| 1890 | ++ } |
| 1891 | ++ } |
| 1892 | ++ |
| 1893 | ++ private static boolean isSame(TreeNode root, TreeNode root1) { |
| 1894 | ++ if (root == null && root1 == null) { |
| 1895 | ++ return true; |
| 1896 | ++ } |
| 1897 | ++ |
| 1898 | ++ if (root == null || root1 == null) { |
| 1899 | ++ return false; |
| 1900 | ++ } |
| 1901 | ++ |
| 1902 | ++ return root.val == root1.val |
| 1903 | ++ && isSame(root.left, root1.left) |
| 1904 | ++ && isSame(root.right, root1.right); |
| 1905 | ++ } |
| 1906 | ++} |
| 1907 | +diff --git a/to be committed/WordLadderII.java b/to be committed/WordLadderII.java |
| 1908 | +new file mode 100644 |
| 1909 | +index 0000000..42c78cf |
| 1910 | +--- /dev/null |
| 1911 | ++++ b/to be committed/WordLadderII.java |
| 1912 | +@@ -0,0 +1,106 @@ |
| 1913 | ++package com.mckinsey.engage.app.apis.mappings; |
| 1914 | ++ |
| 1915 | ++import java.util.HashSet; |
| 1916 | ++import java.util.LinkedList; |
| 1917 | ++import java.util.List; |
| 1918 | ++import java.util.Set; |
| 1919 | ++ |
| 1920 | ++public class WordLadderII { |
| 1921 | ++ public static void main(String[] args) { |
| 1922 | ++ Set dictionary = new HashSet<>(); |
| 1923 | ++ |
| 1924 | ++ String beginWord = "hit"; |
| 1925 | ++ String endWord = "cog"; |
| 1926 | ++ |
| 1927 | ++// beginWord = "hit"; |
| 1928 | ++// endWord = "cog"; |
| 1929 | ++ |
| 1930 | ++ dictionary.add("hot"); |
| 1931 | ++ dictionary.add("dot"); |
| 1932 | ++ dictionary.add("dog"); |
| 1933 | ++ dictionary.add("lot"); |
| 1934 | ++ dictionary.add("log"); |
| 1935 | ++ dictionary.add(endWord); |
| 1936 | ++ |
| 1937 | ++ Set<List<String>> result = new HashSet<>(); |
| 1938 | ++ List<String> tempResult = new LinkedList<>(); |
| 1939 | ++ Set<String> visited = new HashSet<>(); |
| 1940 | ++ List<List<String>> finalResult = new LinkedList<>(); |
| 1941 | ++ |
| 1942 | ++ tempResult.add(beginWord); |
| 1943 | ++ |
| 1944 | ++ allShortestTransformationSequences(beginWord, endWord, dictionary, result, tempResult, visited); |
| 1945 | ++ removeAllListFromResultWithDepthGreaterThanMinimum(result, finalResult); |
| 1946 | ++ |
| 1947 | ++ //return finalResult; |
| 1948 | ++ System.out.println(finalResult); |
| 1949 | ++ } |
| 1950 | ++ |
| 1951 | ++ private static void allShortestTransformationSequences(String start, String end, Set<String> dictionary, |
| 1952 | ++ Set<List<String>> result, List<String> tempResult, |
| 1953 | ++ Set<String> visited) { |
| 1954 | ++ if (start.equals(end)) { |
| 1955 | ++ List<String> tempList = new LinkedList<>(); |
| 1956 | ++ |
| 1957 | ++ for (String word : tempResult) { |
| 1958 | ++ tempList.add(word); |
| 1959 | ++ } |
| 1960 | ++ |
| 1961 | ++ if (!result.contains(tempList)) { |
| 1962 | ++ result.add(tempList); |
| 1963 | ++ } |
| 1964 | ++ } |
| 1965 | ++ |
| 1966 | ++ List<String> neighbours = getNeighbours(start, dictionary); |
| 1967 | ++ |
| 1968 | ++ if (neighbours.size() != 0) { |
| 1969 | ++ for (String neighbour : neighbours) { |
| 1970 | ++ if (!visited.contains(neighbour)) { |
| 1971 | ++ tempResult.add(neighbour); |
| 1972 | ++ visited.add(neighbour); |
| 1973 | ++ allShortestTransformationSequences(neighbour, end, dictionary, result, tempResult, visited); |
| 1974 | ++ tempResult.remove(neighbour); |
| 1975 | ++ visited.remove(neighbour); |
| 1976 | ++ } |
| 1977 | ++ } |
| 1978 | ++ } |
| 1979 | ++ } |
| 1980 | ++ |
| 1981 | ++ private static void removeAllListFromResultWithDepthGreaterThanMinimum(Set<List<String>> result, List<List<String>> finalResult) { |
| 1982 | ++ int minimum = Integer.MAX_VALUE; |
| 1983 | ++ |
| 1984 | ++ for (List<? extends String> list : result) { |
| 1985 | ++ if (list.size() < minimum) { |
| 1986 | ++ minimum = list.size(); |
| 1987 | ++ } |
| 1988 | ++ } |
| 1989 | ++ |
| 1990 | ++ for (List<String> list : result) { |
| 1991 | ++ if (list.size() == minimum) { |
| 1992 | ++ finalResult.add(list); |
| 1993 | ++ } |
| 1994 | ++ } |
| 1995 | ++ } |
| 1996 | ++ |
| 1997 | ++ private static List<String> getNeighbours(String start, Set<String> dictionary) { |
| 1998 | ++ char[] chars = start.toCharArray(); |
| 1999 | ++ List<String> result = new LinkedList<>(); |
| 2000 | ++ |
| 2001 | ++ for (char ch = 'a'; ch <= 'z'; ch++) { |
| 2002 | ++ for (int index = 0; index < chars.length; index++) { |
| 2003 | ++ if (ch != chars[index]) { |
| 2004 | ++ char oldCh = chars[index]; |
| 2005 | ++ chars[index] = ch; |
| 2006 | ++ |
| 2007 | ++ String stringValueOfNewChars = String.valueOf(chars); |
| 2008 | ++ |
| 2009 | ++ if (dictionary.contains(stringValueOfNewChars)) { |
| 2010 | ++ result.add(stringValueOfNewChars); |
| 2011 | ++ } |
| 2012 | ++ chars[index] = oldCh; |
| 2013 | ++ } |
| 2014 | ++ } |
| 2015 | ++ } |
| 2016 | ++ return result; |
| 2017 | ++ } |
| 2018 | ++} |
| 2019 | +\ No newline at end of file |
| 2020 | +-- |
| 2021 | +2.25.0 |
| 2022 | + |
0 commit comments