Skip to content

Commit b2d7cc2

Browse files
committed
Update
1 parent ce8b51d commit b2d7cc2

23 files changed

+1060
-8
lines changed

src/array/DistributeCandies.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
* Description: https://leetcode.com/problems/distribute-candies
9+
* Difficulty: Easy
10+
*/
11+
public class DistributeCandies {
12+
13+
/**
14+
* Time complexity: O(n)
15+
* Space complexity: O(n)
16+
*/
17+
public int distributeCandiesViaSet(int[] candyType) {
18+
Set<Integer> eaten = new HashSet<>();
19+
int candiesLeft = candyType.length / 2;
20+
21+
for (int candy : candyType) {
22+
if (eaten.add(candy)) {
23+
if (--candiesLeft == 0) break;
24+
}
25+
}
26+
27+
return eaten.size();
28+
}
29+
30+
/**
31+
* Time complexity: O(nlog n)
32+
* Space complexity: O(log n)
33+
*/
34+
public int distributeCandiesViaSorting(int[] candyType) {
35+
Arrays.sort(candyType);
36+
37+
int candiesLeft = candyType.length / 2;
38+
int eaten = 0;
39+
for (int i = 0; i < candyType.length; i++) {
40+
if (i > 0 && candyType[i - 1] == candyType[i]) continue;
41+
eaten++;
42+
if (--candiesLeft == 0) break;
43+
}
44+
45+
return eaten;
46+
}
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/kids-with-the-greatest-number-of-candies
8+
* Difficulty: Easy
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class KidsWithTheGreatestNumberOfCandies {
13+
14+
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
15+
int max = 0;
16+
for (int candy : candies) {
17+
max = Math.max(max, candy);
18+
}
19+
20+
List<Boolean> result = new ArrayList<>();
21+
for (int candy : candies) {
22+
result.add(candy + extraCandies >= max);
23+
}
24+
25+
return result;
26+
}
27+
}

src/array/MajorityElement.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ public int majorityElementViaSorting(int[] nums) {
2323
*/
2424
public int majorityElementViaMooreAlgo(int[] nums) {
2525
int count = 0;
26-
int majorityElement = nums[0];
26+
int candidate = nums[0];
2727

2828
for (int num : nums) {
2929
if (count == 0) {
30-
majorityElement = num;
31-
}
32-
33-
if (majorityElement == num) {
30+
candidate = num;
31+
count++;
32+
} else if (candidate == num) {
3433
count++;
3534
} else {
3635
count--;
3736
}
3837
}
3938

40-
return majorityElement;
39+
// it is guaranteed that there's a majority element, so no need for second pass
40+
return candidate;
4141
}
4242
}

src/array/MajorityElement2.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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/majority-element-ii
10+
* Difficulty: Medium
11+
*/
12+
public class MajorityElement2 {
13+
14+
/**
15+
* Time complexity: O(n)
16+
* Space complexity: O(1)
17+
*/
18+
public List<Integer> majorityElementViaMooreAlgo(int[] nums) {
19+
int count1 = 0;
20+
int count2 = 0;
21+
22+
// there can only be 2 elements that appear more than n/3 times
23+
int candidate1 = nums[0];
24+
int candidate2 = nums[0];
25+
26+
for (int num : nums) {
27+
if (candidate1 == num) {
28+
count1++;
29+
} else if (candidate2 == num) {
30+
count2++;
31+
} else if (count1 == 0) {
32+
candidate1 = num;
33+
count1++;
34+
} else if (count2 == 0) {
35+
candidate2 = num;
36+
count2++;
37+
} else {
38+
count1--;
39+
count2--;
40+
}
41+
}
42+
43+
// second pass to check, if candidates really appear more than n/3 times
44+
return checkCandidatesFrequency(nums, candidate1, candidate2);
45+
}
46+
47+
private static List<Integer> checkCandidatesFrequency(int[] nums, int candidate1, int candidate2) {
48+
int count1 = 0;
49+
int count2 = 0;
50+
51+
for (int num : nums) {
52+
if (num == candidate1) {
53+
count1++;
54+
} else if (num == candidate2) {
55+
count2++;
56+
}
57+
}
58+
59+
int n = nums.length / 3;
60+
List<Integer> result = new ArrayList<>();
61+
62+
if (count1 > n) result.add(candidate1);
63+
if (count2 > n) result.add(candidate2);
64+
65+
return result;
66+
}
67+
68+
/**
69+
* Time complexity: O(n)
70+
* Space complexity: O(m)
71+
*/
72+
public List<Integer> majorityElementViaMap(int[] nums) {
73+
Map<Integer, Integer> freqMap = new HashMap<>();
74+
for (int num : nums) {
75+
freqMap.merge(num, 1, Integer::sum);
76+
}
77+
78+
List<Integer> result = new ArrayList<>();
79+
int n = nums.length / 3;
80+
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
81+
if (entry.getValue() > n) result.add(entry.getKey());
82+
}
83+
84+
return result;
85+
}
86+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/minimize-product-sum-of-two-arrays
7+
* Difficulty: Medium
8+
*/
9+
public class MinimizeProductSumOfTwoArrays {
10+
11+
private static final int LOWER_DATA_BOUND = 0;
12+
private static final int UPPER_DATA_BOUND = 100;
13+
14+
/**
15+
* Time complexity: O(n)
16+
* Space complexity: O(1)
17+
*/
18+
public int minProductSumViaCountingSort(int[] nums1, int[] nums2) {
19+
int[] counter1 = new int[UPPER_DATA_BOUND + 1];
20+
int[] counter2 = new int[UPPER_DATA_BOUND + 1];
21+
22+
for (int i = 0; i < nums1.length; i++) {
23+
counter1[nums1[i]]++;
24+
counter2[nums2[i]]++;
25+
}
26+
27+
int p1 = LOWER_DATA_BOUND;
28+
int p2 = UPPER_DATA_BOUND;
29+
int dotProduct = 0;
30+
31+
while (p1 <= UPPER_DATA_BOUND && p2 > LOWER_DATA_BOUND) {
32+
while (p1 <= UPPER_DATA_BOUND && counter1[p1] == 0) {
33+
p1++;
34+
}
35+
36+
while (p2 > LOWER_DATA_BOUND && counter2[p2] == 0) {
37+
p2--;
38+
}
39+
40+
if (p1 > UPPER_DATA_BOUND || p2 == LOWER_DATA_BOUND) break;
41+
42+
int occurrences = Math.min(counter1[p1], counter2[p2]);
43+
dotProduct += occurrences * p1 * p2;
44+
counter1[p1] -= occurrences;
45+
counter2[p2] -= occurrences;
46+
}
47+
48+
return dotProduct;
49+
}
50+
51+
/**
52+
* Time complexity: O(nlog n)
53+
* Space complexity: O(log n)
54+
*/
55+
public int minProductSumViaSorting(int[] nums1, int[] nums2) {
56+
Arrays.sort(nums1);
57+
Arrays.sort(nums2);
58+
59+
int dotProduct = 0;
60+
for (int i = 0; i < nums1.length; i++) {
61+
dotProduct += nums1[i] * nums2[nums1.length - 1 - i];
62+
}
63+
64+
return dotProduct;
65+
}
66+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package binary_search_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/trim-a-binary-search-tree
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(h)
8+
*/
9+
public class TrimBinarySearchTree {
10+
11+
public TreeNode trimBST(TreeNode root, int low, int high) {
12+
if (root == null) return null;
13+
14+
root.left = trimBST(root.left, low, high);
15+
root.right = trimBST(root.right, low, high);
16+
17+
if (root.val < low) return trimBST(root.right, low, high);
18+
if (root.val > high) return trimBST(root.left, low, high);
19+
20+
return root;
21+
}
22+
23+
private static class TreeNode {
24+
int val;
25+
TreeNode left;
26+
TreeNode right;
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package binary_tree;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/deepest-leaves-sum
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(h)
11+
*/
12+
public class DeepestLeavesSum {
13+
14+
public int deepestLeavesSum(TreeNode root) {
15+
Queue<TreeNode> planned = new LinkedList<>();
16+
planned.offer(root);
17+
18+
int levelSum = 0;
19+
while (!planned.isEmpty()) {
20+
int levelSize = planned.size();
21+
levelSum = 0;
22+
for (int i = 0; i < levelSize; i++) {
23+
TreeNode current = planned.poll();
24+
levelSum += current.val;
25+
26+
if (current.left != null) planned.offer(current.left);
27+
if (current.right != null) planned.offer(current.right);
28+
}
29+
}
30+
31+
return levelSum;
32+
}
33+
34+
private static class TreeNode {
35+
int val;
36+
TreeNode left;
37+
TreeNode right;
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package binary_tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Objects;
6+
7+
/**
8+
* Description: https://leetcode.com/problems/flipping-an-image
9+
* Difficulty: Easy
10+
* Time complexity: O(n + m)
11+
* Space complexity: O(n + m)
12+
*/
13+
public class LeafSimilarTrees {
14+
15+
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
16+
List<Integer> firstLeaves = new ArrayList<>();
17+
findLeaves(root1, firstLeaves);
18+
List<Integer> secondLeaves = new ArrayList<>();
19+
findLeaves(root2, secondLeaves);
20+
21+
return Objects.equals(firstLeaves, secondLeaves);
22+
}
23+
24+
private void findLeaves(TreeNode root, List<Integer> leaves) {
25+
if (root == null) return;
26+
if (root.left == null && root.right == null) {
27+
leaves.add(root.val);
28+
return;
29+
}
30+
31+
findLeaves(root.left, leaves);
32+
findLeaves(root.right, leaves);
33+
}
34+
35+
private static class TreeNode {
36+
int val;
37+
TreeNode left;
38+
TreeNode right;
39+
}
40+
}

0 commit comments

Comments
 (0)