Skip to content

Commit 74a000f

Browse files
committed
Update
1 parent 80d576c commit 74a000f

File tree

5 files changed

+131
-4
lines changed

5 files changed

+131
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![DSA Learning](https://img.shields.io/badge/DSA-Learning-blue?style=for-the-badge&logo=leetcode)
44

5-
This repository contains solutions for 534 DSA problems from the [LeetCode](https://leetcode.com/)
5+
This repository contains solutions for 537 DSA problems from the [LeetCode](https://leetcode.com/)
66
website.
77

88
Problems are separated into 19 topics:
@@ -13,14 +13,14 @@ Problems are separated into 19 topics:
1313
| 2 | Binary | [binary](src/binary) | 13 |
1414
| 3 | Binary Search | [binary_search](src/binary_search) | 18 |
1515
| 4 | Binary Search Tree | [binary_search_tree](src/binary_search_tree) | 16 |
16-
| 5 | Binary Tree | [binary_tree](src/binary_tree) | 46 |
16+
| 5 | Binary Tree | [binary_tree](src/binary_tree) | 47 |
1717
| 6 | Design | [design](src/design) | 18 |
1818
| 7 | Dynamic Programming | [dynamic_programming](src/dynamic_programming) | 48 |
19-
| 8 | Graph | [graph](src/graph) | 65 |
19+
| 8 | Graph | [graph](src/graph) | 66 |
2020
| 9 | Greedy Algorithms | [greedy](src/greedy) | 21 |
2121
| 10 | Heap | [heap](src/heap) | 21 |
2222
| 11 | Interval | [interval](src/interval) | 11 |
23-
| 12 | Linked List | [linked_list](src/linked_list) | 32 |
23+
| 12 | Linked List | [linked_list](src/linked_list) | 33 |
2424
| 13 | Math | [math](src/math) | 20 |
2525
| 14 | Matrix | [matrix](src/matrix) | 18 |
2626
| 15 | Quad Tree | [quad_tree](src/quad_tree) | 1 |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package binary_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/binary-tree-longest-consecutive-sequence
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(n)
8+
*/
9+
public class BinaryTreeLongestConsecutiveSequence {
10+
11+
public int longestConsecutive(TreeNode root) {
12+
return findLongestConsecutivePath(root, Integer.MIN_VALUE, 0);
13+
}
14+
15+
private int findLongestConsecutivePath(TreeNode root, int prev, int path) {
16+
if (root == null) return path;
17+
18+
path = (root.val == prev + 1) ? path + 1 : 1;
19+
20+
int left = findLongestConsecutivePath(root.left, root.val, path);
21+
int right = findLongestConsecutivePath(root.right, root.val, path);
22+
23+
return Math.max(path, Math.max(left, right));
24+
}
25+
26+
private static class TreeNode {
27+
int val;
28+
TreeNode left;
29+
TreeNode right;
30+
}
31+
}

src/graph/KillProcess.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* Description: https://leetcode.com/problems/kill-process
10+
* Difficulty: Medium
11+
* Time complexity: O(n)
12+
* Space complexity: O(n)
13+
*/
14+
public class KillProcess {
15+
16+
public List<Integer> killProcess(List<Integer> pid, List<Integer> ppid, int process) {
17+
Map<Integer, List<Integer>> adjList = buildAdjList(pid, ppid);
18+
19+
List<Integer> killed = new ArrayList<>();
20+
kill(process, adjList, killed);
21+
22+
return killed;
23+
}
24+
25+
private Map<Integer, List<Integer>> buildAdjList(List<Integer> pid, List<Integer> ppid) {
26+
Map<Integer, List<Integer>> adjList = new HashMap<>();
27+
for (int i = 0; i < pid.size(); i++) {
28+
adjList.computeIfAbsent(ppid.get(i), __ -> new ArrayList<>()).add(pid.get(i));
29+
}
30+
31+
return adjList;
32+
}
33+
34+
private void kill(int process, Map<Integer, List<Integer>> adjList, List<Integer> killed) {
35+
killed.add(process);
36+
37+
for (int child : adjList.getOrDefault(process, List.of())) {
38+
kill(child, adjList, killed);
39+
}
40+
}
41+
}

src/heap/LeastNumberOfUniqueIntegersAfterKRemovals.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ private Queue<Map.Entry<Integer, Integer>> buildHeap(Map<Integer, Integer> freqM
2727
for (Map.Entry<Integer, Integer> e : freqMap.entrySet()) {
2828
minHeap.offer(e);
2929
}
30+
3031
return minHeap;
3132
}
3233

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package linked_list;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class InsertIntoSortedCircularLinkedList {
10+
11+
public Node insert(Node head, int insertVal) {
12+
if (head == null) return createCircularList(insertVal);
13+
14+
Node current = findInsertionPosition(head, insertVal);
15+
Node next = current.next;
16+
Node inserted = new Node(insertVal);
17+
current.next = inserted;
18+
inserted.next = next;
19+
20+
return head;
21+
}
22+
23+
private Node findInsertionPosition(Node head, int insertVal) {
24+
Node current = head;
25+
26+
while (current.next != head) {
27+
if (current.val <= current.next.val) {
28+
if (current.val <= insertVal && current.next.val >= insertVal) break;
29+
} else {
30+
if (current.val <= insertVal || current.next.val >= insertVal) break;
31+
}
32+
33+
current = current.next;
34+
}
35+
36+
return current;
37+
}
38+
39+
private Node createCircularList(int insertVal) {
40+
Node head = new Node(insertVal);
41+
head.next = head;
42+
43+
return head;
44+
}
45+
46+
private static class Node {
47+
int val;
48+
Node next;
49+
50+
public Node(int val) {
51+
this.val = val;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)