Skip to content

Commit 574d381

Browse files
committed
Update
1 parent 83133b5 commit 574d381

14 files changed

+707
-2
lines changed

src/array/DietPlanPerformance.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/diet-plan-performance
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class DietPlanPerformance {
10+
11+
public int dietPlanPerformance(int[] calories, int k, int lower, int upper) {
12+
int points = 0;
13+
int eaten = 0;
14+
15+
int left = 0;
16+
for (int right = 0; right < calories.length; right++) {
17+
eaten += calories[right];
18+
19+
if (right - left + 1 > k) {
20+
eaten -= calories[left];
21+
left++;
22+
}
23+
24+
if (right >= k - 1) {
25+
points += assessPerformance(eaten, lower, upper);
26+
}
27+
}
28+
29+
return points;
30+
}
31+
32+
private int assessPerformance(int eaten, int lower, int upper) {
33+
if (eaten < lower) return -1;
34+
if (eaten > upper) return 1;
35+
return 0;
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/minimum-size-subarray-sum
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class MinimumSizeSubarraySum {
10+
11+
public int minSubArrayLen(int target, int[] nums) {
12+
int left = 0;
13+
int sum = 0;
14+
int min = Integer.MAX_VALUE;
15+
for (int right = 0; right < nums.length; right++) {
16+
sum += nums[right];
17+
while (sum >= target) {
18+
min = Math.min(min, right - left + 1);
19+
sum -= nums[left];
20+
left++;
21+
}
22+
}
23+
24+
return min == Integer.MAX_VALUE ? 0 : min;
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/subarray-product-less-than-k
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class SubarrayProductLessThanK {
10+
11+
public int numSubarrayProductLessThanK(int[] nums, int k) {
12+
if (k <= 1) return 0;
13+
14+
int left = 0;
15+
int product = 1;
16+
int count = 0;
17+
for (int right = 0; right < nums.length; right++) {
18+
product *= nums[right];
19+
while (product >= k) {
20+
product /= nums[left];
21+
left++;
22+
}
23+
24+
count += right - left + 1;
25+
}
26+
27+
return count;
28+
}
29+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package binary_tree;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii
8+
* Difficulty: Medium
9+
*/
10+
public class PopulatingNextRightPointersInEachNode2 {
11+
12+
/**
13+
* Time complexity: O(n)
14+
* Space complexity: O(n)
15+
*/
16+
public Node connectViaLevelOrderTraversal(Node root) {
17+
if (root == null) return null;
18+
19+
Queue<Node> planned = new LinkedList<>();
20+
planned.offer(root);
21+
22+
while (!planned.isEmpty()) {
23+
int levelSize = planned.size();
24+
for (int i = 0; i < levelSize; i++) {
25+
Node current = planned.poll();
26+
if (i < levelSize - 1) current.next = planned.peek();
27+
28+
if (current.left != null) planned.offer(current.left);
29+
if (current.right != null) planned.offer(current.right);
30+
}
31+
}
32+
33+
return root;
34+
}
35+
36+
/**
37+
* Time complexity: O(n)
38+
* Space complexity: O(1)
39+
*/
40+
public Node connect(Node root) {
41+
if (root == null) return null;
42+
43+
Node dummyLeftmostOnTheNextLevel = new Node();
44+
Node prevOnTheNextLevel = dummyLeftmostOnTheNextLevel;
45+
Node current = root;
46+
47+
while (current != null) {
48+
if (current.left != null) {
49+
prevOnTheNextLevel.next = current.left;
50+
prevOnTheNextLevel = current.left;
51+
}
52+
53+
if (current.right != null) {
54+
prevOnTheNextLevel.next = current.right;
55+
prevOnTheNextLevel = current.right;
56+
}
57+
58+
// move to the next node on the current level
59+
current = current.next;
60+
61+
// go one level down
62+
if (current == null) {
63+
current = dummyLeftmostOnTheNextLevel.next;
64+
prevOnTheNextLevel = dummyLeftmostOnTheNextLevel;
65+
prevOnTheNextLevel.next = null;
66+
}
67+
}
68+
69+
return root;
70+
}
71+
72+
private static class Node {
73+
Node left;
74+
Node right;
75+
Node next;
76+
}
77+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package binary_tree;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another
7+
* Difficulty: Medium
8+
*/
9+
public class StepByStepDirectionsFromBinaryTreeNodeToAnother {
10+
11+
/**
12+
* Time complexity: O(n)
13+
* Space complexity: O(n)
14+
*/
15+
public String getDirectionsViaBFS(TreeNode root, int startValue, int destValue) {
16+
Map<TreeNode, TreeNode> rootToParentMap = new HashMap<>();
17+
findParents(root, rootToParentMap);
18+
19+
// can find it while building parents map not to traverse the graph one more time
20+
TreeNode start = find(root, startValue);
21+
22+
return findShortestPath(start, destValue, rootToParentMap);
23+
}
24+
25+
private String findShortestPath(TreeNode start, int target, Map<TreeNode, TreeNode> rootToParentMap) {
26+
Queue<Pair> planned = new LinkedList<>();
27+
Set<TreeNode> visited = new HashSet<>();
28+
planned.offer(new Pair(start, ""));
29+
30+
while (!planned.isEmpty()) {
31+
int levelSize = planned.size();
32+
for (int i = 0; i < levelSize; i++) {
33+
Pair current = planned.poll();
34+
TreeNode currentNode = current.node;
35+
if (currentNode.val == target) return current.path;
36+
37+
if (currentNode.left != null && visited.add(currentNode.left)) {
38+
planned.offer(new Pair(currentNode.left, current.path + "L"));
39+
}
40+
41+
if (currentNode.right != null && visited.add(currentNode.right)) {
42+
planned.offer(new Pair(currentNode.right, current.path + "R"));
43+
}
44+
45+
TreeNode parent = rootToParentMap.get(currentNode);
46+
if (parent != null && visited.add(parent)) {
47+
planned.offer(new Pair(parent, current.path + "U"));
48+
}
49+
}
50+
}
51+
52+
return "";
53+
}
54+
55+
private TreeNode find(TreeNode root, int val) {
56+
if (root == null) return null;
57+
if (root.val == val) return root;
58+
59+
TreeNode left = find(root.left, val);
60+
TreeNode right = find(root.right, val);
61+
62+
return left != null ? left : right;
63+
}
64+
65+
private void findParents(TreeNode root, Map<TreeNode, TreeNode> parents) {
66+
if (root == null) return;
67+
68+
if (root.left != null) {
69+
parents.put(root.left, root);
70+
findParents(root.left, parents);
71+
}
72+
73+
if (root.right != null) {
74+
parents.put(root.right, root);
75+
findParents(root.right, parents);
76+
}
77+
}
78+
79+
private record Pair(TreeNode node, String path) {
80+
}
81+
82+
/**
83+
* Time complexity: O(n)
84+
* Space complexity: O(n)
85+
*/
86+
public String getDirectionsViaLCA(TreeNode root, int startValue, int destValue) {
87+
TreeNode lca = findLCA(root, startValue, destValue);
88+
String fromStart = buildPathFromStart(lca, startValue, new StringBuilder()); // start -> lca
89+
String toDest = buildPathToDest(lca, destValue, new StringBuilder()); // lca -> dest
90+
91+
return fromStart + toDest;
92+
}
93+
94+
private String buildPathFromStart(TreeNode root, int start, StringBuilder path) {
95+
if (root == null) return null;
96+
if (root.val == start) return path.toString();
97+
98+
path.append("U");
99+
String left = buildPathFromStart(root.left, start, path);
100+
String right = buildPathFromStart(root.right, start, path);
101+
path.deleteCharAt(path.length() - 1);
102+
103+
return left != null ? left : right;
104+
}
105+
106+
private String buildPathToDest(TreeNode root, int dest, StringBuilder path) {
107+
if (root == null) return null;
108+
if (root.val == dest) return path.toString();
109+
110+
path.append("L");
111+
String left = buildPathToDest(root.left, dest, path);
112+
path.deleteCharAt(path.length() - 1);
113+
114+
path.append("R");
115+
String right = buildPathToDest(root.right, dest, path);
116+
path.deleteCharAt(path.length() - 1);
117+
118+
return left != null ? left : right;
119+
}
120+
121+
private TreeNode findLCA(TreeNode root, int first, int second) {
122+
if (root == null) return null;
123+
if (root.val == first || root.val == second) return root;
124+
125+
TreeNode left = findLCA(root.left, first, second);
126+
TreeNode right = findLCA(root.right, first, second);
127+
if (left == null) return right;
128+
if (right == null) return left;
129+
130+
return root;
131+
}
132+
133+
/**
134+
* Time complexity: O(n)
135+
* Space complexity: O(n)
136+
*/
137+
public String getDirectionsViaTwoPaths(TreeNode root, int startValue, int destValue) {
138+
String pathToStart = buildPath(root, startValue, new StringBuilder());
139+
String pathToDest = buildPath(root, destValue, new StringBuilder());
140+
141+
// skip the common part of two paths
142+
int skip = 0;
143+
while (skip < pathToStart.length()
144+
&& skip < pathToDest.length()
145+
&& pathToStart.charAt(skip) == pathToDest.charAt(skip)) {
146+
skip++;
147+
}
148+
149+
// replace all the pathToStart directions with 'U'
150+
StringBuilder directions = new StringBuilder();
151+
for (int j = skip; j < pathToStart.length(); j++) {
152+
directions.append("U");
153+
}
154+
155+
return directions.append(pathToDest.substring(skip)).toString();
156+
}
157+
158+
private String buildPath(TreeNode root, int target, StringBuilder path) {
159+
if (root == null) return null;
160+
if (root.val == target) return path.toString();
161+
162+
path.append("L");
163+
String left = buildPath(root.left, target, path);
164+
path.deleteCharAt(path.length() - 1);
165+
166+
path.append("R");
167+
String right = buildPath(root.right, target, path);
168+
path.deleteCharAt(path.length() - 1);
169+
170+
return left != null ? left : right;
171+
}
172+
173+
private static class TreeNode {
174+
int val;
175+
TreeNode left;
176+
TreeNode right;
177+
}
178+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package hash_table;
1+
package design;
22

33
import java.util.*;
44

src/hash_table/InsertDeleteGetRandomDuplicatesAllowed.java renamed to src/design/InsertDeleteGetRandomDuplicatesAllowed.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package hash_table;
1+
package design;
22

33
import java.util.*;
44

0 commit comments

Comments
 (0)