Skip to content

Commit 0e351cc

Browse files
committed
Add 4 problems
1 parent 0d3d2bd commit 0e351cc

File tree

6 files changed

+173
-4
lines changed

6 files changed

+173
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
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 593 DSA problems from the [LeetCode](https://leetcode.com/)
5+
This repository contains solutions for 597 DSA problems from the [LeetCode](https://leetcode.com/)
66
website.
77

88
Problems are separated into 20 topics:
99

1010
| | Topic | Package | Problems |
1111
|---:|:--------------------|:-----------------------------------------------|---------:|
12-
| 1 | Array | [array](src/array) | 84 |
12+
| 1 | Array | [array](src/array) | 85 |
1313
| 2 | Binary | [binary](src/binary) | 13 |
1414
| 3 | Binary Search | [binary_search](src/binary_search) | 19 |
1515
| 4 | Binary Search Tree | [binary_search_tree](src/binary_search_tree) | 16 |
16-
| 5 | Binary Tree | [binary_tree](src/binary_tree) | 47 |
16+
| 5 | Binary Tree | [binary_tree](src/binary_tree) | 50 |
1717
| 6 | Design | [design](src/design) | 18 |
1818
| 7 | Dynamic Programming | [dynamic_programming](src/dynamic_programming) | 49 |
1919
| 8 | Graph | [graph](src/graph) | 68 |
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package array;
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/group-the-people-given-the-group-size-they-belong-to
10+
* Difficulty: Medium
11+
* Time complexity: O(n)
12+
* Space complexity: O(n)
13+
*/
14+
public class GroupPeopleGivenGroupSizeTheyBelongTo {
15+
16+
public List<List<Integer>> groupThePeople(int[] groupSizes) {
17+
Map<Integer, List<Integer>> groupToPeople = new HashMap<>();
18+
19+
List<List<Integer>> groups = new ArrayList<>();
20+
for (int person = 0; person < groupSizes.length; person++) {
21+
int groupSize = groupSizes[person];
22+
List<Integer> group = groupToPeople.computeIfAbsent(groupSize, __ -> new ArrayList<>());
23+
group.add(person);
24+
25+
if (group.size() == groupSize) {
26+
groups.add(group);
27+
groupToPeople.remove(groupSize);
28+
}
29+
30+
}
31+
32+
return groups;
33+
}
34+
}

src/array/RearrangeArrayElementsBySign.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Description: https://leetcode.com/problems/rearrange-array-elements-by-sign
55
* Difficulty: Medium
6-
* Time complexity: O(n(
6+
* Time complexity: O(n)
77
* Space complexity: O(n)
88
*/
99
public class RearrangeArrayElementsBySign {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package binary_tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
/**
9+
* Description: https://leetcode.com/problems/n-ary-tree-level-order-traversal
10+
* Difficulty: Medium
11+
* Time complexity: O(n)
12+
* Space complexity: O(n)
13+
*/
14+
public class NaryTreeLevelOrderTraversal {
15+
16+
public List<List<Integer>> levelOrder(Node root) {
17+
List<List<Integer>> traversal = new ArrayList<>();
18+
if (root == null) return traversal;
19+
20+
Queue<Node> planned = new LinkedList<>();
21+
planned.offer(root);
22+
23+
while (!planned.isEmpty()) {
24+
int levelSize = planned.size();
25+
List<Integer> level = new ArrayList<>();
26+
traversal.add(level);
27+
28+
for (int i = 0; i < levelSize; i++) {
29+
Node current = planned.poll();
30+
level.add(current.val);
31+
32+
for (Node child : current.children) {
33+
planned.offer(child);
34+
}
35+
}
36+
}
37+
38+
return traversal;
39+
}
40+
41+
private static class Node {
42+
int val;
43+
List<Node> children;
44+
}
45+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package binary_tree;
2+
3+
import java.util.LinkedList;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/reverse-odd-levels-of-binary-tree
7+
* Difficulty: Medium
8+
* Time complexity: O(n)
9+
* Space complexity: O(n)
10+
*/
11+
public class ReverseOddLevelsOfBinaryTree {
12+
13+
public TreeNode reverseOddLevels(TreeNode root) {
14+
LinkedList<TreeNode> planned = new LinkedList<>();
15+
planned.offer(root);
16+
17+
int level = 0;
18+
while (!planned.isEmpty()) {
19+
int levelSize = planned.size();
20+
21+
for (int i = 0; i < levelSize; i++) {
22+
TreeNode current = planned.poll();
23+
24+
if (current.left != null) planned.offer(current.left);
25+
if (current.right != null) planned.offer(current.right);
26+
}
27+
28+
level++;
29+
30+
if (level % 2 != 0) {
31+
reverseLevel(planned);
32+
}
33+
}
34+
35+
return root;
36+
}
37+
38+
private void reverseLevel(LinkedList<TreeNode> planned) {
39+
int left = 0;
40+
int right = planned.size() - 1;
41+
42+
while (left < right) {
43+
swap(planned.get(left), planned.get(right));
44+
45+
left++;
46+
right--;
47+
}
48+
}
49+
50+
private void swap(TreeNode first, TreeNode second) {
51+
int tmp = first.val;
52+
first.val = second.val;
53+
second.val = tmp;
54+
}
55+
56+
private static class TreeNode {
57+
int val;
58+
TreeNode left;
59+
TreeNode right;
60+
}
61+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package binary_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(h)
8+
*/
9+
public class SumOfNodesWithEvenValuedGrandparent {
10+
11+
public int sumEvenGrandparent(TreeNode root) {
12+
return sum(null, null, root);
13+
}
14+
15+
private int sum(TreeNode grandParent, TreeNode parent, TreeNode current) {
16+
if (current == null) return 0;
17+
18+
int sum = (grandParent != null && grandParent.val % 2 == 0) ? current.val : 0;
19+
return sum
20+
+ sum(parent, current, current.left)
21+
+ sum(parent, current, current.right);
22+
}
23+
24+
private static class TreeNode {
25+
int val;
26+
TreeNode left;
27+
TreeNode right;
28+
}
29+
}

0 commit comments

Comments
 (0)