Skip to content

Commit 250af45

Browse files
committed
Update
1 parent b2d7cc2 commit 250af45

38 files changed

+1656
-11
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class AverageSalaryExcludingTheMinimumAndMaximumSalary {
10+
11+
public double average(int[] salaries) {
12+
int min = Integer.MAX_VALUE;
13+
int max = Integer.MIN_VALUE;
14+
int sum = 0;
15+
16+
for (int salary : salaries) {
17+
min = Math.min(min, salary);
18+
max = Math.max(max, salary);
19+
sum += salary;
20+
}
21+
22+
return (double) (sum - min - max) / (salaries.length - 2);
23+
}
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
/**
9+
* Description: https://leetcode.com/problems/find-the-difference-of-two-arrays
10+
* Difficulty: Easy
11+
* Time complexity: O(m + n)
12+
* Space complexity: O(m + n)
13+
*/
14+
public class FindTheDifferenceOfTwoArrays {
15+
16+
public static void main(String[] args) {
17+
int n = 5;
18+
int times = n / 4 + (n % 4 == 0 ? 0 : 1);
19+
System.out.println(times);
20+
}
21+
22+
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
23+
Set<Integer> set2 = new HashSet<>();
24+
for (int num : nums2) {
25+
set2.add(num);
26+
}
27+
28+
Set<Integer> set1 = new HashSet<>();
29+
List<Integer> result1 = new ArrayList<>();
30+
for (int num : nums1) {
31+
set1.add(num);
32+
if (set2.add(num)) result1.add(num);
33+
}
34+
35+
List<Integer> result2 = new ArrayList<>();
36+
for (int num : nums2) {
37+
if (set1.add(num)) result2.add(num);
38+
}
39+
40+
return List.of(result1, result2);
41+
}
42+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package array;
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/find-the-winner-of-the-circular-game
10+
* Difficulty: Medium
11+
*/
12+
public class FindWinnerOfTheCircularGame {
13+
14+
/**
15+
* Time complexity: O(n * k)
16+
* Space complexity: O(n)
17+
*/
18+
public int findTheWinnerViaQueue(int n, int k) {
19+
Queue<Integer> players = new LinkedList<>();
20+
for (int i = 0; i < n; i++) {
21+
players.offer(i);
22+
}
23+
24+
while (players.size() > 1) {
25+
int looser = (k - 1) % players.size();
26+
for (int i = 0; i < looser; i++) {
27+
players.offer(players.poll()); // "rotate" players
28+
}
29+
30+
players.poll(); // remove looser
31+
}
32+
33+
return players.poll() + 1;
34+
}
35+
36+
/**
37+
* Time complexity: O(n^2)
38+
* Space complexity: O(n)
39+
*/
40+
public int findTheWinner(int n, int k) {
41+
List<Integer> players = new ArrayList<>();
42+
for (int player = 0; player < n; player++) {
43+
players.add(player);
44+
}
45+
46+
int current = 0;
47+
while (players.size() > 1) {
48+
int looser = (current + k - 1) % players.size();
49+
players.remove(looser); // takes O(n) time
50+
current = looser;
51+
}
52+
53+
return players.get(0) + 1;
54+
}
55+
}

src/array/FourSum.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/4sum
7+
* Difficulty: Medium
8+
* Time complexity: O(n^3)
9+
* Space complexity: O(n)
10+
*/
11+
public class FourSum {
12+
13+
public List<List<Integer>> fourSum(int[] nums, int target) {
14+
Arrays.sort(nums);
15+
16+
Set<List<Integer>> result = new HashSet<>();
17+
for (int i = 0; i < nums.length; i++) {
18+
for (int j = i + 1; j < nums.length - 2; j++) {
19+
int left = j + 1;
20+
int right = nums.length - 1;
21+
long localTarget = (long) target - nums[i] - nums[j];
22+
23+
while (left < right) {
24+
long sum = nums[left] + nums[right];
25+
if (sum == localTarget) {
26+
result.add(List.of(nums[i], nums[j], nums[left], nums[right]));
27+
left++;
28+
right--;
29+
} else if (sum > localTarget) {
30+
right--;
31+
} else {
32+
left++;
33+
}
34+
}
35+
}
36+
}
37+
38+
return new ArrayList<>(result);
39+
}
40+
}

src/array/FruitIntoBaskets.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package array;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/fruit-into-baskets
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(1)
11+
*/
12+
public class FruitIntoBaskets {
13+
14+
public int totalFruit(int[] fruits) {
15+
int left = 0;
16+
int right = 0;
17+
18+
Map<Integer, Integer> basket = new HashMap<>();
19+
int max = 0;
20+
while (right < fruits.length) {
21+
basket.merge(fruits[right], 1, Integer::sum);
22+
23+
// shrink the window from the left
24+
while (basket.size() > 2) {
25+
int count = basket.merge(fruits[left], -1, Integer::sum);
26+
if (count == 0) basket.remove(fruits[left]);
27+
left++;
28+
}
29+
30+
max = Math.max(max, right - left + 1);
31+
right++;
32+
}
33+
34+
return max;
35+
}
36+
}

src/array/MaximumSizeSubarraySumEqualsK.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ public int maxSubArrayLen(int[] nums, int k) {
2424
maxLength = Math.max(maxLength, i - prefixSum.get(sum - k));
2525
}
2626

27-
if (!prefixSum.containsKey(sum)) {
28-
prefixSum.put(sum, i);
29-
}
27+
prefixSum.putIfAbsent(sum, i);
3028
}
3129

3230
return maxLength;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition
7+
* Difficulty: Medium
8+
* Time complexity: O(nlog n)
9+
* Space complexity: O(n)
10+
*/
11+
public class NumberOfSubsequencesThatSatisfyTheGivenSumCondition {
12+
13+
// since the answer may be too large, return it modulo 10^9 + 7
14+
private static final int MOD = 1_000_000_007;
15+
16+
public int numSubseq(int[] nums, int target) {
17+
Arrays.sort(nums);
18+
int[] powersOfTwo = precomputePowers(nums);
19+
int result = 0;
20+
21+
int left = 0;
22+
int right = nums.length - 1;
23+
while (left <= right) {
24+
if (nums[left] + nums[right] <= target) {
25+
// all possible subsequences in [left + 1; right]
26+
result += powersOfTwo[right - left];
27+
result %= MOD;
28+
left++;
29+
} else {
30+
right--;
31+
}
32+
}
33+
34+
return result;
35+
}
36+
37+
private int[] precomputePowers(int[] nums) {
38+
int[] powers = new int[nums.length];
39+
powers[0] = 1;
40+
for (int i = 1; i < nums.length; ++i) {
41+
powers[i] = (powers[i - 1] * 2) % MOD;
42+
}
43+
44+
return powers;
45+
}
46+
}

src/array/PancakeSorting.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/pancake-sorting
8+
* Difficulty: Medium
9+
* Time complexity: O(n^2)
10+
* Space complexity: O(n)
11+
*/
12+
public class PancakeSorting {
13+
14+
public List<Integer> pancakeSort(int[] arr) {
15+
List<Integer> flips = new ArrayList<>();
16+
for (int current = arr.length; current > 0; current--) {
17+
int index = find(arr, current);
18+
if (index == current - 1) continue; // already in place
19+
20+
if (index != 0) {
21+
// flip to the head
22+
flips.add(index + 1);
23+
flip(arr, index + 1);
24+
}
25+
26+
// flip to the tail
27+
flips.add(current);
28+
flip(arr, current);
29+
}
30+
31+
return flips;
32+
}
33+
34+
private void flip(int[] arr, int index) {
35+
for (int i = 0; i < index / 2; i++) {
36+
swap(arr, i, index - i - 1);
37+
}
38+
}
39+
40+
private void swap(int[] arr, int i, int j) {
41+
int tmp = arr[i];
42+
arr[i] = arr[j];
43+
arr[j] = tmp;
44+
}
45+
46+
private int find(int[] arr, int val) {
47+
for (int i = 0; i < arr.length; i++) {
48+
if (arr[i] == val) return i;
49+
}
50+
51+
return -1;
52+
}
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package array;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/prison-cells-after-n-days
8+
* Difficulty: Medium
9+
* Time complexity: O(cells * min(days, 2^cells))
10+
* Space complexity: O(min(days, 2^cells))
11+
*/
12+
public class PrisonCellAfterNDays {
13+
14+
public int[] prisonAfterNDays(int[] cells, int days) {
15+
Map<Integer, Integer> stateToDayCache = new HashMap<>();
16+
boolean isFastForwarded = false;
17+
while (days > 0) {
18+
if (!isFastForwarded) {
19+
int currentState = buildState(cells);
20+
if (stateToDayCache.containsKey(currentState)) {
21+
// pattern (cycle) was found -> fast forward
22+
days = days % (stateToDayCache.get(currentState) - days);
23+
isFastForwarded = true;
24+
} else {
25+
stateToDayCache.put(currentState, days);
26+
}
27+
}
28+
29+
if (days > 0) {
30+
cells = nextDay(cells);
31+
days--;
32+
}
33+
}
34+
35+
return cells;
36+
}
37+
38+
private int[] nextDay(int[] prevDay) {
39+
int[] nextDay = new int[prevDay.length];
40+
nextDay[0] = 0;
41+
nextDay[nextDay.length - 1] = 0;
42+
43+
for (int cell = 1; cell < nextDay.length - 1; cell++) {
44+
nextDay[cell] = prevDay[cell - 1] == prevDay[cell + 1] ? 1 : 0;
45+
}
46+
47+
return nextDay;
48+
}
49+
50+
private int buildState(int[] cells) {
51+
int state = 0;
52+
for (int cell : cells) {
53+
state = state << 1;
54+
state = state | cell;
55+
}
56+
57+
return state;
58+
}
59+
}

0 commit comments

Comments
 (0)