Skip to content

Commit a301c61

Browse files
committed
Update
1 parent ddddcc1 commit a301c61

10 files changed

+396
-0
lines changed

src/array/FindTheHighestAltitude.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/find-the-highest-altitude
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class FindTheHighestAltitude {
10+
11+
public int largestAltitude(int[] gains) {
12+
int largest = 0;
13+
int altitude = 0;
14+
15+
for (int gain : gains) {
16+
altitude += gain;
17+
largest = Math.max(largest, altitude);
18+
}
19+
20+
return largest;
21+
}
22+
}

src/array/MaximumAverageSubarray.java

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/maximum-average-subarray-i
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class MaximumAverageSubarray {
10+
11+
public double findMaxAverage(int[] nums, int k) {
12+
double maxAvg = Double.NEGATIVE_INFINITY;
13+
int windowSum = 0;
14+
for (int i = 0; i < nums.length; i++) {
15+
windowSum += nums[i];
16+
if (i >= k) windowSum -= nums[i - k];
17+
18+
if (i >= k - 1) {
19+
double windowAvg = (double) windowSum / k;
20+
maxAvg = Math.max(maxAvg, windowAvg);
21+
}
22+
}
23+
24+
return maxAvg;
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package binary_search;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/guess-number-higher-or-lower
5+
* Difficulty: Easy
6+
* Time complexity: O(log n)
7+
* Space complexity: O(1)
8+
*/
9+
public class GuessNumberHigherOrLower {
10+
11+
public int guessNumber(int n) {
12+
int left = 1;
13+
int right = n;
14+
15+
while (left <= right) {
16+
int mid = left + (right - left) / 2;
17+
int guess = guess(mid);
18+
19+
if (guess > 0) {
20+
left = mid + 1;
21+
} else if (guess < 0) {
22+
right = mid - 1;
23+
} else {
24+
return mid;
25+
}
26+
}
27+
28+
return -1;
29+
}
30+
31+
// API call
32+
private int guess(int num) {
33+
return 0;
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package binary_search_tree;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/minimum-absolute-difference-in-bst
8+
* Difficulty: Easy
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class MinimumAbsoluteDifferenceInBST {
13+
14+
public int getMinimumDifference(TreeNode root) {
15+
Deque<TreeNode> stack = new LinkedList<>();
16+
int minDiff = Integer.MAX_VALUE;
17+
18+
TreeNode prev = null;
19+
while (!stack.isEmpty() || root != null) {
20+
if (root != null) {
21+
stack.push(root);
22+
root = root.left;
23+
} else {
24+
root = stack.pop();
25+
if (prev != null) minDiff = Math.min(minDiff, Math.abs(root.val - prev.val));
26+
prev = root;
27+
root = root.right;
28+
}
29+
}
30+
31+
return minDiff;
32+
}
33+
34+
private static class TreeNode {
35+
int val;
36+
TreeNode left;
37+
TreeNode right;
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package binary_search_tree;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/minimum-distance-between-bst-nodes
8+
* Difficulty: Easy
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class MinimumDistanceBetweenBSTNodes {
13+
14+
public int minDiffInBST(TreeNode root) {
15+
Deque<TreeNode> stack = new LinkedList<>();
16+
int minDiff = Integer.MAX_VALUE;
17+
18+
TreeNode prev = null;
19+
while (!stack.isEmpty() || root != null) {
20+
if (root != null) {
21+
stack.push(root);
22+
root = root.left;
23+
} else {
24+
root = stack.pop();
25+
if (prev != null) minDiff = Math.min(minDiff, Math.abs(root.val - prev.val));
26+
prev = root;
27+
root = root.right;
28+
}
29+
}
30+
31+
return minDiff;
32+
}
33+
34+
private static class TreeNode {
35+
int val;
36+
TreeNode left;
37+
TreeNode right;
38+
}
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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/average-of-levels-in-binary-tree
10+
* Difficulty: Easy
11+
* Time complexity: O(n)
12+
* Space complexity: O(n)
13+
*/
14+
public class AverageOfLevelsInBinaryTree {
15+
16+
public List<Double> averageOfLevels(TreeNode root) {
17+
Queue<TreeNode> planned = new LinkedList<>();
18+
planned.offer(root);
19+
20+
List<Double> averages = new ArrayList<>();
21+
while (!planned.isEmpty()) {
22+
int levelSize = planned.size();
23+
long sum = 0L;
24+
int count = 0;
25+
26+
for (int i = 0; i < levelSize; i++) {
27+
TreeNode current = planned.poll();
28+
sum += current.val;
29+
count++;
30+
31+
if (current.left != null) planned.offer(current.left);
32+
if (current.right != null) planned.offer(current.right);
33+
}
34+
35+
double avg = (double) sum / count;
36+
averages.add(avg);
37+
}
38+
39+
return averages;
40+
}
41+
42+
private static class TreeNode {
43+
int val;
44+
TreeNode left;
45+
TreeNode right;
46+
}
47+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package binary_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/count-complete-tree-nodes
5+
* Difficulty: Medium
6+
*/
7+
public class CountCompleteTreeNodes {
8+
9+
/**
10+
* Time complexity: O(log n * log n)
11+
* Space complexity: O(1)
12+
*/
13+
public int countNodesViaBinarySearch(TreeNode root) {
14+
if (root == null) return 0;
15+
16+
int depth = findDepth(root);
17+
int lastLevelNodesNumber = findLastLevelNodesNumber(root, depth);
18+
19+
return (int) Math.pow(2, depth) - 1 + lastLevelNodesNumber;
20+
}
21+
22+
private int findLastLevelNodesNumber(TreeNode root, int depth) {
23+
// last level has from 1 to 2^depth nodes
24+
int left = 0;
25+
int right = (int) Math.pow(2, depth) - 1;
26+
27+
while (left <= right) {
28+
int mid = left + (right - left) / 2;
29+
if (isExist(root, depth, mid)) {
30+
left = mid + 1;
31+
} else {
32+
right = mid - 1;
33+
}
34+
}
35+
36+
return left;
37+
}
38+
39+
private boolean isExist(TreeNode root, int depth, int idx) {
40+
// use BS to reconstruct the sequence of moves from root to idx node
41+
int left = 0;
42+
int right = (int) Math.pow(2, depth) - 1;
43+
44+
for (int i = 0; i < depth; i++) {
45+
int mid = left + (right - left) / 2;
46+
if (idx <= mid) {
47+
root = root.left;
48+
right = mid;
49+
} else {
50+
root = root.right;
51+
left = mid + 1;
52+
}
53+
}
54+
55+
return root != null;
56+
}
57+
58+
private int findDepth(TreeNode root) {
59+
int depth = 0;
60+
while (root.left != null) {
61+
root = root.left;
62+
depth++;
63+
}
64+
65+
return depth;
66+
}
67+
68+
/**
69+
* Time complexity: O(n)
70+
* Space complexity: O(h)
71+
*/
72+
public int countNodesNaiveApproach(TreeNode root) {
73+
if (root == null) return 0;
74+
return 1 + countNodesNaiveApproach(root.left) + countNodesNaiveApproach(root.right);
75+
}
76+
77+
private static class TreeNode {
78+
int val;
79+
TreeNode left;
80+
TreeNode right;
81+
}
82+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dynamic_programming;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/minimum-path-sum
5+
* Difficulty: Medium
6+
* Time complexity: O(n * m)
7+
* Space complexity: O(n * m)
8+
*/
9+
public class MinimumPathSum {
10+
11+
public int minPathSum(int[][] grid) {
12+
int[][] dp = new int[grid.length][grid[0].length];
13+
for (int i = 0; i < grid.length; i++) {
14+
for (int j = 0; j < grid[0].length; j++) {
15+
if (i == 0 && j == 0) {
16+
dp[i][j] = grid[i][j];
17+
continue;
18+
}
19+
20+
int fromTop = i > 0 ? dp[i - 1][j] : Integer.MAX_VALUE;
21+
int fromLeft = j > 0 ? dp[i][j - 1] : Integer.MAX_VALUE;
22+
23+
dp[i][j] = Math.min(fromTop, fromLeft) + grid[i][j];
24+
}
25+
}
26+
27+
return dp[grid.length - 1][grid[0].length - 1];
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dynamic_programming;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/n-th-tribonacci-number
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class NthTribonacciNumber {
10+
11+
public int tribonacci(int n) {
12+
if (n <= 1) return n;
13+
if (n == 2) return 1;
14+
15+
int prev1 = 0;
16+
int prev2 = 1;
17+
int prev3 = 1;
18+
19+
for (int i = 3; i <= n; i++) {
20+
int current = prev1 + prev2 + prev3;
21+
prev1 = prev2;
22+
prev2 = prev3;
23+
prev3 = current;
24+
}
25+
26+
return prev3;
27+
}
28+
}

0 commit comments

Comments
 (0)