Skip to content

Commit 46adbba

Browse files
committed
Update
1 parent 4d96d32 commit 46adbba

13 files changed

+446
-1
lines changed

src/array/FindAnagramMappings.java

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.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/find-anagram-mappings
8+
* Difficulty: Easy
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class FindAnagramMappings {
13+
14+
public int[] anagramMappings(int[] nums1, int[] nums2) {
15+
Map<Integer, Integer> numToIndex = new HashMap<>();
16+
for (int i = 0; i < nums2.length; i++) {
17+
numToIndex.put(nums2[i], i);
18+
}
19+
20+
int[] mappings = new int[nums1.length];
21+
for (int i = 0; i < nums1.length; i++) {
22+
mappings[i] = numToIndex.get(nums1[i]);
23+
}
24+
25+
return mappings;
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/how-many-apples-can-you-put-into-the-basket
7+
* Difficulty: Easy
8+
* Time complexity: O(nlog n)
9+
* Space complexity: O(log n)
10+
*/
11+
public class HowManyApplesCanYouPutIntoTheBasket {
12+
13+
public int maxNumberOfApples(int[] weight) {
14+
Arrays.sort(weight);
15+
16+
int count = 0;
17+
int total = 0;
18+
for (int apple : weight) {
19+
total += apple;
20+
if (total > 5000) return count;
21+
count++;
22+
}
23+
24+
return count;
25+
}
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/k-radius-subarray-averages
7+
* Difficulty: Medium
8+
* Time complexity: O(n)
9+
* Space complexity: O(1)
10+
*/
11+
public class KRadiusSubarrayAverages {
12+
13+
public int[] getAverages(int[] nums, int k) {
14+
int[] averages = new int[nums.length];
15+
Arrays.fill(averages, -1);
16+
if (2 * k + 1 > nums.length) return averages;
17+
18+
long sum = 0L;
19+
int left = 0;
20+
for (int right = 0; right < nums.length; right++) {
21+
sum += nums[right];
22+
23+
if (right > k * 2) {
24+
sum -= nums[left];
25+
left++;
26+
}
27+
28+
if (right >= k * 2) {
29+
averages[right - k] = (int) (sum / (k * 2 + 1));
30+
}
31+
}
32+
33+
return averages;
34+
}
35+
}

src/array/MajorityElement2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public List<Integer> majorityElementViaMooreAlgo(int[] nums) {
4444
return checkCandidatesFrequency(nums, candidate1, candidate2);
4545
}
4646

47-
private static List<Integer> checkCandidatesFrequency(int[] nums, int candidate1, int candidate2) {
47+
private List<Integer> checkCandidatesFrequency(int[] nums, int candidate1, int candidate2) {
4848
int count1 = 0;
4949
int count2 = 0;
5050

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package binary;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class MinimumFlipsToMakeAOrBEqualToC {
10+
11+
public int minFlips(int a, int b, int c) {
12+
int flips = 0;
13+
while (a != 0 || b != 0 || c != 0) {
14+
int aBit = (a & 1);
15+
int bBit = (b & 1);
16+
int cBit = (c & 1);
17+
18+
if ((aBit | bBit) != cBit) {
19+
if (cBit == 1) {
20+
flips += 1;
21+
} else {
22+
flips += (aBit == 1) ? 1 : 0;
23+
flips += (bBit == 1) ? 1 : 0;
24+
}
25+
}
26+
27+
a >>= 1;
28+
b >>= 1;
29+
c >>= 1;
30+
}
31+
32+
return flips;
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package binary_search;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array
5+
* Difficulty: Easy
6+
* Time complexity: O(log n)
7+
* Space complexity: O(1)
8+
*/
9+
public class CheckIfNumberIsMajorityElementInSortedArray {
10+
11+
public boolean isMajorityElement(int[] nums, int target) {
12+
int firstOccurrence = findFirstOccurrence(nums, target);
13+
return firstOccurrence + nums.length / 2 < nums.length
14+
&& nums[firstOccurrence + nums.length / 2] == target;
15+
}
16+
17+
private int findFirstOccurrence(int[] nums, int target) {
18+
int left = 0;
19+
int right = nums.length - 1;
20+
21+
int min = 0;
22+
while (left <= right) {
23+
int mid = left + (right - left) / 2;
24+
if (nums[mid] < target) {
25+
left = mid + 1;
26+
} else if (nums[mid] > target) {
27+
right = mid - 1;
28+
} else {
29+
min = mid;
30+
right = mid - 1;
31+
}
32+
}
33+
34+
return min;
35+
}
36+
}

src/binary_search/FixedPoint.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package binary_search;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/fixed-point
5+
* Difficulty: Easy
6+
* Time complexity: O(log n)
7+
* Space complexity: O(1)
8+
*/
9+
public class FixedPoint {
10+
11+
public int fixedPoint(int[] arr) {
12+
int left = 0;
13+
int right = arr.length - 1;
14+
15+
int min = -1;
16+
while (left <= right) {
17+
int mid = left + (right - left) / 2;
18+
if (arr[mid] > mid) {
19+
right = mid - 1;
20+
} else if (arr[mid] < mid) {
21+
left = mid + 1;
22+
} else {
23+
min = mid;
24+
right = mid - 1;
25+
}
26+
}
27+
28+
return min;
29+
}
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package binary_search_tree;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/closest-binary-search-tree-value
8+
* Difficulty: Easy
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class ClosestBinarySearchTreeValue {
13+
14+
public int closestValueViaInorderTraversal(TreeNode root, double target) {
15+
int predecessor = Integer.MIN_VALUE;
16+
Deque<TreeNode> stack = new LinkedList<>();
17+
while (root != null || !stack.isEmpty()) {
18+
if (root != null) {
19+
stack.push(root);
20+
root = root.left;
21+
} else {
22+
root = stack.pop();
23+
if (predecessor <= target && target <= root.val) {
24+
return Math.abs(predecessor - target) <= Math.abs(root.val - target) ? predecessor : root.val;
25+
}
26+
27+
predecessor = root.val;
28+
root = root.right;
29+
}
30+
}
31+
32+
return predecessor;
33+
}
34+
35+
private static class TreeNode {
36+
int val;
37+
TreeNode left;
38+
TreeNode right;
39+
}
40+
}

src/design/LoggerRateLimiter.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package design;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/logger-rate-limiter
8+
* Difficulty: Easy
9+
* Time complexity: O(1)
10+
* Space complexity: O(n)
11+
*/
12+
public class LoggerRateLimiter {
13+
14+
private static final int RATE_LIMIT_INTERVAL_SEC = 10;
15+
16+
private static class Logger {
17+
18+
private final Map<String, Integer> lastPrinted;
19+
20+
public Logger() {
21+
this.lastPrinted = new HashMap<>();
22+
}
23+
24+
public boolean shouldPrintMessage(int timestamp, String message) {
25+
if (lastPrinted.getOrDefault(message, -RATE_LIMIT_INTERVAL_SEC) + RATE_LIMIT_INTERVAL_SEC <= timestamp) {
26+
lastPrinted.put(message, timestamp);
27+
return true;
28+
}
29+
30+
return false;
31+
}
32+
}
33+
}

src/greedy/Candy.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package greedy;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.PriorityQueue;
6+
import java.util.Queue;
7+
8+
/**
9+
* Description: https://leetcode.com/problems/candy
10+
* Difficulty: Hard
11+
*/
12+
public class Candy {
13+
14+
/**
15+
* Time complexity: O(n)
16+
* Space complexity: O(n)
17+
*/
18+
public int candyViaTwoPasses(int[] ratings) {
19+
int[] candies = new int[ratings.length];
20+
Arrays.fill(candies, 1);
21+
22+
// go right and make sure each child with higher rating has more candies than his left neighbor
23+
for (int i = 1; i < ratings.length; i++) {
24+
if (ratings[i] > ratings[i - 1]) candies[i] = candies[i - 1] + 1;
25+
}
26+
27+
// go left and make sure each child with higher rating has more candies than his right neighbor
28+
for (int i = ratings.length - 2; i >= 0; i--) {
29+
if (ratings[i] > ratings[i + 1]) candies[i] = Math.max(candies[i], candies[i + 1] + 1);
30+
}
31+
32+
// count the total number of candies
33+
int total = 0;
34+
for (int candy : candies) {
35+
total += candy;
36+
}
37+
38+
return total;
39+
}
40+
41+
/**
42+
* Time complexity: O(nlog n)
43+
* Space complexity: O(n)
44+
*/
45+
public int candyViaMinHeap(int[] ratings) {
46+
Queue<Child> minHeap = new PriorityQueue<>(Comparator.comparingInt(c -> c.rating));
47+
for (int i = 0; i < ratings.length; i++) {
48+
minHeap.offer(new Child(ratings[i], i));
49+
}
50+
51+
int[] candies = new int[ratings.length];
52+
Arrays.fill(candies, 1);
53+
54+
int total = 0;
55+
while (!minHeap.isEmpty()) {
56+
int current = minHeap.poll().index;
57+
58+
if (current > 0 && ratings[current - 1] < ratings[current]) {
59+
candies[current] = candies[current - 1] + 1;
60+
}
61+
62+
if (current < ratings.length - 1 && ratings[current + 1] < ratings[current]) {
63+
candies[current] = Math.max(candies[current], candies[current + 1] + 1);
64+
}
65+
66+
total += candies[current];
67+
}
68+
69+
return total;
70+
}
71+
72+
private record Child(int rating, int index) {
73+
}
74+
}

src/greedy/Dota2Senate.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package greedy;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/dota2-senate
8+
* Difficulty: Medium
9+
* Time complexity: O(nlog n)
10+
* Space complexity: O(n)
11+
*/
12+
public class Dota2Senate {
13+
14+
public String predictPartyVictory(String senate) {
15+
Queue<Integer> radiant = new LinkedList<>();
16+
Queue<Integer> dire = new LinkedList<>();
17+
for (int i = 0; i < senate.length(); i++) {
18+
if (senate.charAt(i) == 'R') {
19+
radiant.offer(i);
20+
} else {
21+
dire.offer(i);
22+
}
23+
}
24+
25+
while (!radiant.isEmpty() && !dire.isEmpty()) {
26+
int radiantSenator = radiant.poll();
27+
int direSenator = dire.poll();
28+
29+
// the one with the larger index is BANNED by the one with the smaller one
30+
if (radiantSenator > direSenator) {
31+
// make sure this senator won't vote again in the same round
32+
dire.offer(direSenator + senate.length());
33+
} else {
34+
radiant.offer(radiantSenator + senate.length());
35+
}
36+
}
37+
38+
return radiant.isEmpty() ? "Dire" : "Radiant";
39+
}
40+
}

0 commit comments

Comments
 (0)