Skip to content

Commit

Permalink
Added More task.
Browse files Browse the repository at this point in the history
  • Loading branch information
neerazz committed Apr 4, 2024
1 parent 60419a3 commit 729b3b5
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Algorithms/greedy-algorithum/TaskScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,59 @@

public class TaskScheduler {
public static void main(String[] args) {
System.out.println("------------------------------------------------------------");

System.out.println(leastInterval(new char[]{'A', 'A', 'A', 'B', 'B', 'B'}, 2) + " should be [8]");
System.out.println(leastInterval(new char[]{'A', 'A', 'A', 'B', 'B', 'B'}, 0) + " should be [6]");
System.out.println(leastInterval(new char[]{'A', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G'}, 2) + " should be [16]");

System.out.println("------------------------------------------------------------");

System.out.println(leastInterval_rev1(new char[]{'A', 'A', 'A', 'B', 'B', 'B'}, 2) + " should be [8]");
System.out.println(leastInterval_rev1(new char[]{'A', 'A', 'A', 'B', 'B', 'B'}, 0) + " should be [6]");
System.out.println(leastInterval_rev1(new char[]{'A', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G'}, 2) + " should be [16]");
System.out.println(leastInterval_rev1(new char[]{'F', 'J', 'J', 'A', 'J', 'F', 'C', 'H', 'J', 'B', 'E', 'G', 'G', 'F', 'A', 'C', 'I', 'F', 'J', 'C', 'J', 'C', 'H', 'C', 'A', 'D', 'G', 'H', 'B', 'F', 'G', 'C', 'C', 'A', 'E', 'B', 'H', 'J', 'E', 'I', 'F', 'D', 'E', 'A', 'C', 'D', 'B', 'D', 'J', 'J', 'C', 'F', 'D', 'D', 'J', 'H', 'A', 'E', 'C', 'D', 'J', 'D', 'G', 'G', 'B', 'C', 'E', 'G', 'H', 'I', 'D', 'H', 'F', 'E', 'I', 'B', 'D', 'E', 'I', 'E', 'C', 'J', 'G', 'I', 'D', 'E', 'D', 'J', 'C', 'A', 'C', 'C', 'D', 'I', 'J', 'B', 'D', 'H', 'H', 'J', 'G', 'B', 'G', 'A', 'H', 'E', 'H', 'E', 'D', 'E', 'J', 'E', 'J', 'C', 'F', 'C', 'J', 'G', 'B', 'C', 'I', 'I', 'H', 'F', 'A', 'D', 'G', 'F', 'C', 'C', 'F', 'G', 'C', 'J', 'B', 'B', 'I', 'C', 'J', 'J', 'E', 'G', 'H', 'C', 'I', 'G', 'J', 'I', 'G', 'G', 'J', 'G', 'G', 'E', 'G', 'B', 'I', 'J', 'B', 'H', 'D', 'H', 'G', 'F', 'C', 'H', 'C', 'D', 'A', 'G', 'B', 'H', 'H', 'B', 'J', 'C', 'A', 'F', 'J', 'G', 'F', 'E', 'B', 'F', 'E', 'B', 'B', 'A', 'E', 'F', 'E', 'H', 'I', 'I', 'C', 'G', 'J', 'D', 'H', 'E', 'F', 'G', 'G', 'D', 'E', 'B', 'F', 'J', 'J', 'J', 'D', 'H', 'E', 'B', 'D', 'J', 'I', 'F', 'C', 'I', 'E', 'H', 'F', 'E', 'G', 'D', 'E', 'C', 'F', 'E', 'D', 'E', 'A', 'I', 'E', 'A', 'D', 'H', 'G', 'C', 'I', 'E', 'G', 'A', 'H', 'I', 'G', 'G', 'A', 'G', 'F', 'H', 'J', 'D', 'F', 'A', 'G', 'H', 'B', 'J', 'A', 'H', 'B', 'H', 'C', 'G', 'F', 'A', 'C', 'C', 'B', 'I', 'G', 'G', 'B', 'C', 'J', 'J', 'I', 'E', 'G', 'D', 'I', 'J', 'I', 'C', 'G', 'A', 'J', 'G', 'F', 'J', 'F', 'C', 'F', 'G', 'J', 'I', 'E', 'B', 'G', 'F', 'A', 'D', 'A', 'I', 'A', 'E', 'H', 'F', 'D', 'D', 'C', 'B', 'J', 'I', 'J', 'H', 'I', 'C', 'D', 'A', 'G', 'F', 'I', 'B', 'E', 'D', 'C', 'J', 'G', 'I', 'H', 'E', 'C', 'E', 'I', 'I', 'B', 'B', 'H', 'J', 'C', 'F', 'I', 'D', 'B', 'F', 'H', 'F', 'A', 'C', 'A', 'A', 'B', 'D', 'C', 'A', 'G', 'B', 'G', 'F', 'E', 'G', 'A', 'A', 'A', 'C', 'J', 'H', 'H', 'G', 'C', 'C', 'B', 'C', 'E', 'B', 'E', 'F', 'I', 'E', 'E', 'D', 'I', 'H', 'G', 'F', 'A', 'H', 'B', 'J', 'B', 'G', 'H', 'C', 'C', 'B', 'G', 'C', 'B', 'A', 'E', 'G', 'A', 'J', 'G', 'D', 'C', 'I', 'G', 'F', 'G', 'G', 'A', 'J', 'E', 'I', 'D', 'E', 'A', 'F', 'A', 'H', 'C', 'E', 'D', 'D', 'D', 'H', 'I', 'F', 'F', 'A', 'F', 'A', 'A', 'C', 'J', 'D', 'J', 'H', 'I', 'F', 'A', 'C', 'B', 'C', 'A', 'C', 'C', 'H', 'A', 'J', 'I', 'B', 'A', 'I', 'F', 'J', 'C', 'I', 'B', 'C', 'E', 'E', 'E', 'J', 'G', 'F', 'E', 'I', 'A', 'A', 'E', 'B', 'J', 'H', 'H', 'H', 'A', 'H', 'J', 'E', 'F', 'E', 'F', 'G', 'J', 'D', 'I', 'D', 'I', 'F', 'B', 'J', 'D', 'A', 'A', 'D', 'F', 'G', 'B', 'J', 'H', 'F', 'A', 'D', 'H', 'C', 'B', 'A', 'J', 'H', 'I', 'F', 'H', 'E', 'G', 'B', 'A', 'F', 'F', 'A', 'C', 'D', 'G', 'I', 'I', 'J', 'H', 'H', 'C', 'J', 'G', 'B', 'A', 'D', 'B', 'F', 'J', 'D', 'I', 'A', 'F', 'F', 'F', 'F', 'A', 'E', 'B', 'C', 'G', 'H', 'E', 'B', 'B', 'A', 'G', 'D', 'C', 'C', 'E', 'A', 'C', 'F', 'G', 'A', 'I', 'F', 'B', 'H', 'J', 'G', 'C', 'B', 'H', 'D', 'A', 'H', 'B', 'H', 'H', 'C', 'A', 'F', 'I', 'C', 'F', 'A', 'C', 'J', 'I', 'H', 'H', 'F', 'B', 'B', 'D', 'E', 'C', 'J', 'F', 'C', 'E', 'A', 'J', 'E', 'C', 'A', 'E', 'B', 'A', 'J', 'F', 'J', 'J', 'J', 'H', 'H', 'C', 'I', 'E', 'G', 'G', 'H', 'J', 'J', 'H', 'H', 'H', 'J', 'H', 'A', 'G', 'I', 'C', 'E', 'C', 'D', 'G', 'G', 'F', 'H', 'D', 'G', 'H', 'A', 'E', 'I', 'D', 'A', 'H', 'G', 'E', 'A', 'B', 'F', 'I', 'C', 'A', 'F', 'B', 'A', 'I', 'F', 'G', 'I', 'F', 'D', 'A', 'B', 'J', 'B', 'D', 'F', 'G', 'J', 'J', 'A', 'A', 'C', 'H', 'G', 'F', 'B', 'I', 'I', 'J', 'A', 'H', 'D', 'F', 'E', 'F', 'J', 'B', 'F', 'C', 'G', 'E', 'A', 'G', 'H', 'E', 'H', 'H', 'F', 'I', 'G', 'C', 'C', 'G', 'J', 'B', 'H', 'F', 'H', 'D', 'I', 'B', 'D', 'I', 'F', 'H', 'I', 'D', 'F', 'G', 'G', 'E', 'A', 'C', 'A', 'G', 'H', 'G', 'H', 'J', 'F', 'D', 'F', 'G', 'D', 'D', 'C', 'J', 'C', 'J', 'G', 'G', 'G', 'G', 'H', 'H', 'G', 'D', 'E', 'H', 'G', 'C', 'B', 'F', 'I', 'F', 'C', 'H', 'J', 'I', 'A', 'F', 'D', 'C', 'F', 'C', 'E', 'E', 'D', 'D', 'C', 'G', 'B', 'F', 'E', 'J', 'C', 'I', 'E', 'D', 'B', 'B', 'I', 'I', 'I', 'H', 'C', 'E', 'C', 'J', 'F', 'G', 'A', 'I', 'J', 'D', 'I', 'C', 'G', 'F', 'I', 'E', 'I', 'E', 'F', 'A', 'G', 'E', 'J', 'A', 'I', 'A', 'D', 'A', 'G', 'J', 'F', 'E', 'D', 'I', 'A', 'E', 'J', 'I', 'C', 'J', 'B', 'F', 'B', 'E', 'C', 'E', 'F', 'G', 'E', 'J', 'J', 'I', 'E', 'D', 'F', 'C', 'H', 'H', 'B', 'G', 'D', 'I', 'I', 'F', 'B', 'G', 'C', 'F', 'J', 'B', 'G', 'J', 'H', 'D', 'G', 'C', 'C', 'I', 'I', 'E', 'I', 'B', 'H', 'B', 'I', 'G', 'F', 'H', 'G', 'C', 'J', 'D', 'C', 'E', 'G', 'F', 'C', 'H', 'D', 'A', 'C', 'D', 'H', 'B', 'C', 'H', 'I', 'B', 'A', 'J', 'C', 'B', 'D', 'J', 'D', 'H', 'F', 'B', 'A', 'G', 'G', 'J', 'I', 'E', 'F', 'A', 'D', 'H', 'D', 'B', 'C', 'A', 'H', 'F', 'G', 'B', 'F', 'H', 'B', 'H', 'I', 'J', 'D', 'H', 'I', 'B', 'C', 'D', 'G', 'A', 'E', 'A', 'A', 'I', 'F', 'I', 'F', 'B', 'B', 'I', 'F', 'A', 'E', 'I', 'A', 'B', 'G', 'C', 'F', 'I', 'A', 'F', 'I', 'D', 'H', 'B', 'I', 'I', 'B', 'J', 'F', 'E', 'B', 'B', 'B', 'D', 'C', 'J', 'E', 'J', 'J', 'G', 'D', 'F', 'F', 'F', 'G', 'I', 'H', 'J', 'J', 'G', 'D', 'G', 'F'}
, 8) + " should be [1000]");

System.out.println("------------------------------------------------------------");

System.out.println(leastInterval_rev3(new char[]{'A', 'A', 'A', 'B', 'B', 'B'}, 2) + " should be [8]");
System.out.println(leastInterval_rev3(new char[]{'A', 'A', 'A', 'B', 'B', 'B'}, 0) + " should be [6]");
System.out.println(leastInterval_rev3(new char[]{'A','A','A','A','A','A','B','C','D','E','F','G'}, 1) + " should be [12]");
System.out.println(leastInterval_rev3(new char[]{'A', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G'}, 2) + " should be [16]");
System.out.println(leastInterval_rev3(new char[]{'F', 'J', 'J', 'A', 'J', 'F', 'C', 'H', 'J', 'B', 'E', 'G', 'G', 'F', 'A', 'C', 'I', 'F', 'J', 'C', 'J', 'C', 'H', 'C', 'A', 'D', 'G', 'H', 'B', 'F', 'G', 'C', 'C', 'A', 'E', 'B', 'H', 'J', 'E', 'I', 'F', 'D', 'E', 'A', 'C', 'D', 'B', 'D', 'J', 'J', 'C', 'F', 'D', 'D', 'J', 'H', 'A', 'E', 'C', 'D', 'J', 'D', 'G', 'G', 'B', 'C', 'E', 'G', 'H', 'I', 'D', 'H', 'F', 'E', 'I', 'B', 'D', 'E', 'I', 'E', 'C', 'J', 'G', 'I', 'D', 'E', 'D', 'J', 'C', 'A', 'C', 'C', 'D', 'I', 'J', 'B', 'D', 'H', 'H', 'J', 'G', 'B', 'G', 'A', 'H', 'E', 'H', 'E', 'D', 'E', 'J', 'E', 'J', 'C', 'F', 'C', 'J', 'G', 'B', 'C', 'I', 'I', 'H', 'F', 'A', 'D', 'G', 'F', 'C', 'C', 'F', 'G', 'C', 'J', 'B', 'B', 'I', 'C', 'J', 'J', 'E', 'G', 'H', 'C', 'I', 'G', 'J', 'I', 'G', 'G', 'J', 'G', 'G', 'E', 'G', 'B', 'I', 'J', 'B', 'H', 'D', 'H', 'G', 'F', 'C', 'H', 'C', 'D', 'A', 'G', 'B', 'H', 'H', 'B', 'J', 'C', 'A', 'F', 'J', 'G', 'F', 'E', 'B', 'F', 'E', 'B', 'B', 'A', 'E', 'F', 'E', 'H', 'I', 'I', 'C', 'G', 'J', 'D', 'H', 'E', 'F', 'G', 'G', 'D', 'E', 'B', 'F', 'J', 'J', 'J', 'D', 'H', 'E', 'B', 'D', 'J', 'I', 'F', 'C', 'I', 'E', 'H', 'F', 'E', 'G', 'D', 'E', 'C', 'F', 'E', 'D', 'E', 'A', 'I', 'E', 'A', 'D', 'H', 'G', 'C', 'I', 'E', 'G', 'A', 'H', 'I', 'G', 'G', 'A', 'G', 'F', 'H', 'J', 'D', 'F', 'A', 'G', 'H', 'B', 'J', 'A', 'H', 'B', 'H', 'C', 'G', 'F', 'A', 'C', 'C', 'B', 'I', 'G', 'G', 'B', 'C', 'J', 'J', 'I', 'E', 'G', 'D', 'I', 'J', 'I', 'C', 'G', 'A', 'J', 'G', 'F', 'J', 'F', 'C', 'F', 'G', 'J', 'I', 'E', 'B', 'G', 'F', 'A', 'D', 'A', 'I', 'A', 'E', 'H', 'F', 'D', 'D', 'C', 'B', 'J', 'I', 'J', 'H', 'I', 'C', 'D', 'A', 'G', 'F', 'I', 'B', 'E', 'D', 'C', 'J', 'G', 'I', 'H', 'E', 'C', 'E', 'I', 'I', 'B', 'B', 'H', 'J', 'C', 'F', 'I', 'D', 'B', 'F', 'H', 'F', 'A', 'C', 'A', 'A', 'B', 'D', 'C', 'A', 'G', 'B', 'G', 'F', 'E', 'G', 'A', 'A', 'A', 'C', 'J', 'H', 'H', 'G', 'C', 'C', 'B', 'C', 'E', 'B', 'E', 'F', 'I', 'E', 'E', 'D', 'I', 'H', 'G', 'F', 'A', 'H', 'B', 'J', 'B', 'G', 'H', 'C', 'C', 'B', 'G', 'C', 'B', 'A', 'E', 'G', 'A', 'J', 'G', 'D', 'C', 'I', 'G', 'F', 'G', 'G', 'A', 'J', 'E', 'I', 'D', 'E', 'A', 'F', 'A', 'H', 'C', 'E', 'D', 'D', 'D', 'H', 'I', 'F', 'F', 'A', 'F', 'A', 'A', 'C', 'J', 'D', 'J', 'H', 'I', 'F', 'A', 'C', 'B', 'C', 'A', 'C', 'C', 'H', 'A', 'J', 'I', 'B', 'A', 'I', 'F', 'J', 'C', 'I', 'B', 'C', 'E', 'E', 'E', 'J', 'G', 'F', 'E', 'I', 'A', 'A', 'E', 'B', 'J', 'H', 'H', 'H', 'A', 'H', 'J', 'E', 'F', 'E', 'F', 'G', 'J', 'D', 'I', 'D', 'I', 'F', 'B', 'J', 'D', 'A', 'A', 'D', 'F', 'G', 'B', 'J', 'H', 'F', 'A', 'D', 'H', 'C', 'B', 'A', 'J', 'H', 'I', 'F', 'H', 'E', 'G', 'B', 'A', 'F', 'F', 'A', 'C', 'D', 'G', 'I', 'I', 'J', 'H', 'H', 'C', 'J', 'G', 'B', 'A', 'D', 'B', 'F', 'J', 'D', 'I', 'A', 'F', 'F', 'F', 'F', 'A', 'E', 'B', 'C', 'G', 'H', 'E', 'B', 'B', 'A', 'G', 'D', 'C', 'C', 'E', 'A', 'C', 'F', 'G', 'A', 'I', 'F', 'B', 'H', 'J', 'G', 'C', 'B', 'H', 'D', 'A', 'H', 'B', 'H', 'H', 'C', 'A', 'F', 'I', 'C', 'F', 'A', 'C', 'J', 'I', 'H', 'H', 'F', 'B', 'B', 'D', 'E', 'C', 'J', 'F', 'C', 'E', 'A', 'J', 'E', 'C', 'A', 'E', 'B', 'A', 'J', 'F', 'J', 'J', 'J', 'H', 'H', 'C', 'I', 'E', 'G', 'G', 'H', 'J', 'J', 'H', 'H', 'H', 'J', 'H', 'A', 'G', 'I', 'C', 'E', 'C', 'D', 'G', 'G', 'F', 'H', 'D', 'G', 'H', 'A', 'E', 'I', 'D', 'A', 'H', 'G', 'E', 'A', 'B', 'F', 'I', 'C', 'A', 'F', 'B', 'A', 'I', 'F', 'G', 'I', 'F', 'D', 'A', 'B', 'J', 'B', 'D', 'F', 'G', 'J', 'J', 'A', 'A', 'C', 'H', 'G', 'F', 'B', 'I', 'I', 'J', 'A', 'H', 'D', 'F', 'E', 'F', 'J', 'B', 'F', 'C', 'G', 'E', 'A', 'G', 'H', 'E', 'H', 'H', 'F', 'I', 'G', 'C', 'C', 'G', 'J', 'B', 'H', 'F', 'H', 'D', 'I', 'B', 'D', 'I', 'F', 'H', 'I', 'D', 'F', 'G', 'G', 'E', 'A', 'C', 'A', 'G', 'H', 'G', 'H', 'J', 'F', 'D', 'F', 'G', 'D', 'D', 'C', 'J', 'C', 'J', 'G', 'G', 'G', 'G', 'H', 'H', 'G', 'D', 'E', 'H', 'G', 'C', 'B', 'F', 'I', 'F', 'C', 'H', 'J', 'I', 'A', 'F', 'D', 'C', 'F', 'C', 'E', 'E', 'D', 'D', 'C', 'G', 'B', 'F', 'E', 'J', 'C', 'I', 'E', 'D', 'B', 'B', 'I', 'I', 'I', 'H', 'C', 'E', 'C', 'J', 'F', 'G', 'A', 'I', 'J', 'D', 'I', 'C', 'G', 'F', 'I', 'E', 'I', 'E', 'F', 'A', 'G', 'E', 'J', 'A', 'I', 'A', 'D', 'A', 'G', 'J', 'F', 'E', 'D', 'I', 'A', 'E', 'J', 'I', 'C', 'J', 'B', 'F', 'B', 'E', 'C', 'E', 'F', 'G', 'E', 'J', 'J', 'I', 'E', 'D', 'F', 'C', 'H', 'H', 'B', 'G', 'D', 'I', 'I', 'F', 'B', 'G', 'C', 'F', 'J', 'B', 'G', 'J', 'H', 'D', 'G', 'C', 'C', 'I', 'I', 'E', 'I', 'B', 'H', 'B', 'I', 'G', 'F', 'H', 'G', 'C', 'J', 'D', 'C', 'E', 'G', 'F', 'C', 'H', 'D', 'A', 'C', 'D', 'H', 'B', 'C', 'H', 'I', 'B', 'A', 'J', 'C', 'B', 'D', 'J', 'D', 'H', 'F', 'B', 'A', 'G', 'G', 'J', 'I', 'E', 'F', 'A', 'D', 'H', 'D', 'B', 'C', 'A', 'H', 'F', 'G', 'B', 'F', 'H', 'B', 'H', 'I', 'J', 'D', 'H', 'I', 'B', 'C', 'D', 'G', 'A', 'E', 'A', 'A', 'I', 'F', 'I', 'F', 'B', 'B', 'I', 'F', 'A', 'E', 'I', 'A', 'B', 'G', 'C', 'F', 'I', 'A', 'F', 'I', 'D', 'H', 'B', 'I', 'I', 'B', 'J', 'F', 'E', 'B', 'B', 'B', 'D', 'C', 'J', 'E', 'J', 'J', 'G', 'D', 'F', 'F', 'F', 'G', 'I', 'H', 'J', 'J', 'G', 'D', 'G', 'F'}
, 8) + " should be [1000]");

System.out.println("------------------------------------------------------------");
}

public static int leastInterval_rev3(char[] tasks, int n) {
int interval = 1;
Map<Character, Integer> map = new HashMap<>();
for (char t : tasks) {
map.put(t, map.getOrDefault(t, 0) + 1);
}

// task, count, time
PriorityQueue<int[]> pq = new PriorityQueue<>((t1, t2) -> t1[1] == t2[1] ? t1[2] - t2[2] : t2[1] - t1[1]);
PriorityQueue<int[]> pending = new PriorityQueue<>((t1, t2) -> t1[2] - t2[2]);
map.entrySet().forEach(entry -> pq.add(new int[]{entry.getKey() - 'A', entry.getValue(), 0}));

while (!pq.isEmpty() || !pending.isEmpty()) {
while (!pending.isEmpty() && pending.peek()[2] < interval) {
pq.add(pending.poll());
}
if (pq.isEmpty()) {
interval = Math.max(interval+1, pending.peek()[2]);
continue;
}
int[] poll = pq.poll();
if (poll[1] > 1) {
pending.add(new int[]{poll[0], poll[1] - 1, interval + n});
}
interval++;
}
return interval - 1;
}

public static int leastInterval_rev2(char[] tasks, int n) {
Expand Down
6 changes: 6 additions & 0 deletions dataStructures/binarytree/BinaryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public TreeNode(int val) {
this.val = val;
}

public TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}

@Override
public String toString() {
return val +
Expand Down
57 changes: 57 additions & 0 deletions dataStructures/binarytree/CousinsInBinaryTreeII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.*;

public class CousinsInBinaryTreeII {

public static void main(String[] args) {
TreeNode treeNode = BinaryTree.createTreeNode(33, 37, 42, null, null, null, 46);
TreeNode result = replaceValueInTree(treeNode);
System.out.println("result = " + result);
}

public static TreeNode replaceValueInTree(TreeNode root) {
Map<Integer, Integer> map = new HashMap<>();
int level = 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
int sum = 0;
while (size-- > 0) {
TreeNode poll = queue.poll();
sum += poll.val;
if (poll.left != null) queue.add(poll.left);
if (poll.right != null) queue.add(poll.right);
}
map.put(level++, sum);
}

queue.add(root);
level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
TreeNode poll = queue.poll();
// At each node you set the nextLevelSum value of the child nodes, by reducing the nextLevelSum of values
int nextLevelSum = 0;
if (level <= 1) {
poll.val = 0;
}
if (poll.left != null) {
queue.add(poll.left);
nextLevelSum += poll.left.val;
}
if (poll.right != null) {
queue.add(poll.right);
nextLevelSum += poll.right.val;
}

if (poll.left != null) poll.left.val = map.get(level + 1) - nextLevelSum;
if (poll.right != null) poll.right.val = map.get(level + 1) - nextLevelSum;
}
level++;
}

return root;
}

}
30 changes: 30 additions & 0 deletions dataStructures/graph/CheapestFlightsWithinKStops.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ public static void main(String[] args) {
13, 4, 13) + " should be [47]");
}

public static int findCheapestPrice_rev2(int n, int[][] flights, int src, int dst, int k) {
Map<Integer, List<int[]>> cons = new HashMap<>();
for (int[] flight : flights) {
int from = flight[0], to = flight[1], cost = flight[2];
cons.computeIfAbsent(from, key -> new ArrayList<>()).add(new int[]{to, cost});
}
int[] distances = new int[n];
Arrays.fill(distances, Integer.MAX_VALUE);
distances[src] = 0;
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{src, 0});
int stops = 0;
while (!q.isEmpty() && stops <= k) {
int size = q.size();
for (int i = 0; i < size; i++) {
int[] poll = q.poll();
int cur = poll[0], curCost = poll[1];
for (int[] con : cons.getOrDefault(cur, new ArrayList<>())) {
int nextCost = curCost + con[1];
if (nextCost < distances[con[0]]) {
distances[con[0]] = nextCost;
q.add(new int[]{con[0], nextCost});
}
}
}
stops++;
}
return distances[dst] == Integer.MAX_VALUE ? -1 : distances[dst];
}

public static int findCheapestPrice_rev1(int n, int[][] flights, int src, int dst, int K) {
map = new HashMap<>();
price = new HashMap<>();
Expand Down
46 changes: 46 additions & 0 deletions dataStructures/stack/MaximumNestingDepthOfTheParentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.LinkedList;
import java.util.Queue;

// https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/description/?envType=daily-question&envId=2024-04-04
public class MaximumNestingDepthOfTheParentheses {

public static void main(String[] args) {
System.out.println(maxDepth("(1+(2*3)+((8)/4))+1"));
System.out.println(maxDepth("(1)+((2))+(((3)))"));
}

public static int maxDepth(String s) {
int depth = 0;
if (s.isEmpty()) return depth;
Queue<String> queue = new LinkedList<>();
queue.add(s);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
String poll = queue.poll();
int start = -1, cal = 0;
for (int j = 0; j < poll.length(); j++) {
char c = poll.charAt(j);
if (c == '(') {
cal++;
if (start == -1) {
start = j;
}
} else if (c == ')') {
cal--;
if (cal == 0) {
queue.add(poll.substring(start + 1, j));
start = -1;
}
}
}
if (start != -1) {
queue.add(poll.substring(start + 1));
}
}
depth++;
// System.out.println("Depth " + depth + " Queue = " + queue);
}
return depth - 1;
}
}

0 comments on commit 729b3b5

Please sign in to comment.