Skip to content

Commit be1fde5

Browse files
authored
Added tasks 2028, 2029, 2030, 2032, 2033.
1 parent e8a5b4f commit be1fde5

File tree

15 files changed

+604
-0
lines changed

15 files changed

+604
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2001_2100.s2028_find_missing_observations;
2+
3+
// #Medium #Array #Math #Simulation #2022_05_25_Time_10_ms_(31.40%)_Space_165.4_MB_(26.74%)
4+
5+
public class Solution {
6+
public int[] missingRolls(int[] rolls, int mean, int n) {
7+
int m = rolls.length;
8+
int msum = 0;
9+
int[] res = new int[n];
10+
for (int roll : rolls) {
11+
msum += roll;
12+
}
13+
int totalmn = mean * (m + n) - msum;
14+
if (totalmn < n || totalmn > n * 6) {
15+
return new int[0];
16+
}
17+
int j = 0;
18+
while (totalmn > 0) {
19+
int dice = Math.min(6, totalmn - n + 1);
20+
res[j] = dice;
21+
totalmn = totalmn - dice;
22+
n--;
23+
j++;
24+
}
25+
return res;
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2028\. Find Missing Observations
2+
3+
Medium
4+
5+
You have observations of `n + m` **6-sided** dice rolls with each face numbered from `1` to `6`. `n` of the observations went missing, and you only have the observations of `m` rolls. Fortunately, you have also calculated the **average value** of the `n + m` rolls.
6+
7+
You are given an integer array `rolls` of length `m` where `rolls[i]` is the value of the <code>i<sup>th</sup></code> observation. You are also given the two integers `mean` and `n`.
8+
9+
Return _an array of length_ `n` _containing the missing observations such that the **average value** of the_ `n + m` _rolls is **exactly**_ `mean`. If there are multiple valid answers, return _any of them_. If no such array exists, return _an empty array_.
10+
11+
The **average value** of a set of `k` numbers is the sum of the numbers divided by `k`.
12+
13+
Note that `mean` is an integer, so the sum of the `n + m` rolls should be divisible by `n + m`.
14+
15+
**Example 1:**
16+
17+
**Input:** rolls = [3,2,4,3], mean = 4, n = 2
18+
19+
**Output:** [6,6]
20+
21+
**Explanation:** The mean of all n + m rolls is (3 + 2 + 4 + 3 + 6 + 6) / 6 = 4.
22+
23+
**Example 2:**
24+
25+
**Input:** rolls = [1,5,6], mean = 3, n = 4
26+
27+
**Output:** [2,3,2,2]
28+
29+
**Explanation:** The mean of all n + m rolls is (1 + 5 + 6 + 2 + 3 + 2 + 2) / 7 = 3.
30+
31+
**Example 3:**
32+
33+
**Input:** rolls = [1,2,3,4], mean = 6, n = 4
34+
35+
**Output:** []
36+
37+
**Explanation:** It is impossible for the mean to be 6 no matter what the 4 missing rolls are.
38+
39+
**Constraints:**
40+
41+
* `m == rolls.length`
42+
* <code>1 <= n, m <= 10<sup>5</sup></code>
43+
* `1 <= rolls[i], mean <= 6`
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package g2001_2100.s2029_stone_game_ix;
2+
3+
// #Medium #Array #Math #Greedy #Counting #Game_Theory
4+
// #2022_05_25_Time_31_ms_(10.87%)_Space_114.5_MB_(65.22%)
5+
6+
public class Solution {
7+
private int[] stones;
8+
9+
public boolean stoneGameIX(int[] stones) {
10+
this.stones = stones;
11+
int[] freq = new int[3];
12+
for (int i : stones) {
13+
if (i % 3 == 0) {
14+
freq[0]++;
15+
} else if (i % 3 == 1) {
16+
freq[1]++;
17+
} else {
18+
freq[2]++;
19+
}
20+
}
21+
boolean b1 = false;
22+
boolean b2 = false;
23+
int[] a = freq.clone();
24+
int[] b = freq.clone();
25+
if (a[1] > 0) {
26+
a[1]--;
27+
b1 = fun(a, 1);
28+
}
29+
if (b[2] > 0) {
30+
b[2]--;
31+
b2 = fun(b, 2);
32+
}
33+
return b1 || b2;
34+
}
35+
36+
private boolean fun(int[] freq, int sum) {
37+
int n = stones.length;
38+
int i = 1;
39+
while (i < n) {
40+
if (i % 2 == 0) {
41+
if (sum % 3 == 1) {
42+
if (freq[0] > 0) {
43+
freq[0]--;
44+
} else if (freq[1] > 0) {
45+
freq[1]--;
46+
sum += 1;
47+
} else {
48+
return false;
49+
}
50+
} else if (sum % 3 == 2) {
51+
if (freq[0] > 0) {
52+
freq[0]--;
53+
} else if (freq[2] > 0) {
54+
freq[2]--;
55+
sum += 2;
56+
} else {
57+
return false;
58+
}
59+
}
60+
} else {
61+
if (sum % 3 == 2) {
62+
if (freq[0] > 0) {
63+
freq[0]--;
64+
} else if (freq[2] > 0) {
65+
freq[2]--;
66+
sum += 2;
67+
} else {
68+
return true;
69+
}
70+
} else if (sum % 3 == 1) {
71+
if (freq[0] > 0) {
72+
freq[0]--;
73+
} else if (freq[1] > 0) {
74+
freq[1]--;
75+
sum += 1;
76+
} else {
77+
return true;
78+
}
79+
}
80+
}
81+
i++;
82+
}
83+
return false;
84+
}
85+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2029\. Stone Game IX
2+
3+
Medium
4+
5+
Alice and Bob continue their games with stones. There is a row of n stones, and each stone has an associated value. You are given an integer array `stones`, where `stones[i]` is the **value** of the <code>i<sup>th</sup></code> stone.
6+
7+
Alice and Bob take turns, with **Alice** starting first. On each turn, the player may remove any stone from `stones`. The player who removes a stone **loses** if the **sum** of the values of **all removed stones** is divisible by `3`. Bob will win automatically if there are no remaining stones (even if it is Alice's turn).
8+
9+
Assuming both players play **optimally**, return `true` _if Alice wins and_ `false` _if Bob wins_.
10+
11+
**Example 1:**
12+
13+
**Input:** stones = [2,1]
14+
15+
**Output:** true
16+
17+
**Explanation:** The game will be played as follows:
18+
19+
- Turn 1: Alice can remove either stone.
20+
21+
- Turn 2: Bob removes the remaining stone.
22+
23+
The sum of the removed stones is 1 + 2 = 3 and is divisible by 3. Therefore, Bob loses and Alice wins the game.
24+
25+
**Example 2:**
26+
27+
**Input:** stones = [2]
28+
29+
**Output:** false
30+
31+
**Explanation:** Alice will remove the only stone, and the sum of the values on the removed stones is 2.
32+
33+
Since all the stones are removed and the sum of values is not divisible by 3, Bob wins the game.
34+
35+
**Example 3:**
36+
37+
**Input:** stones = [5,1,2,4,3]
38+
39+
**Output:** false
40+
41+
**Explanation:** Bob will always win. One possible way for Bob to win is shown below:
42+
43+
- Turn 1: Alice can remove the second stone with value 1. Sum of removed stones = 1.
44+
45+
- Turn 2: Bob removes the fifth stone with value 3. Sum of removed stones = 1 + 3 = 4.
46+
47+
- Turn 3: Alices removes the fourth stone with value 4. Sum of removed stones = 1 + 3 + 4 = 8.
48+
49+
- Turn 4: Bob removes the third stone with value 2. Sum of removed stones = 1 + 3 + 4 + 2 = 10.
50+
51+
- Turn 5: Alice removes the first stone with value 5. Sum of removed stones = 1 + 3 + 4 + 2 + 5 = 15.
52+
53+
Alice loses the game because the sum of the removed stones (15) is divisible by 3. Bob wins the game.
54+
55+
**Constraints:**
56+
57+
* <code>1 <= stones.length <= 10<sup>5</sup></code>
58+
* <code>1 <= stones[i] <= 10<sup>4</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2001_2100.s2030_smallest_k_length_subsequence_with_occurrences_of_a_letter;
2+
3+
// #Hard #String #Greedy #Stack #Monotonic_Stack
4+
// #2022_05_25_Time_131_ms_(64.46%)_Space_72.3_MB_(46.99%)
5+
6+
public class Solution {
7+
public String smallestSubsequence(String s, int k, char letter, int repetition) {
8+
int count = 0;
9+
for (char c : s.toCharArray()) {
10+
count += c == letter ? 1 : 0;
11+
}
12+
StringBuilder sb = new StringBuilder();
13+
for (int i = 0; i < s.length(); count -= s.charAt(i++) == letter ? 1 : 0) {
14+
while (sb.length() + s.length() > i + k
15+
&& sb.length() > 0
16+
&& s.charAt(i) < sb.charAt(sb.length() - 1)
17+
&& (sb.charAt(sb.length() - 1) != letter || count != repetition)) {
18+
repetition += sb.charAt(sb.length() - 1) == letter ? 1 : 0;
19+
sb.setLength(sb.length() - 1);
20+
}
21+
if (k - sb.length() > Math.max(0, s.charAt(i) == letter ? 0 : repetition)) {
22+
sb.append(s.charAt(i));
23+
repetition -= s.charAt(i) == letter ? 1 : 0;
24+
}
25+
}
26+
return sb.toString();
27+
}
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2030\. Smallest K-Length Subsequence With Occurrences of a Letter
2+
3+
Hard
4+
5+
You are given a string `s`, an integer `k`, a letter `letter`, and an integer `repetition`.
6+
7+
Return _the **lexicographically smallest** subsequence of_ `s` _of length_ `k` _that has the letter_ `letter` _appear **at least**_ `repetition` _times_. The test cases are generated so that the `letter` appears in `s` **at least** `repetition` times.
8+
9+
A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
10+
11+
A string `a` is **lexicographically smaller** than a string `b` if in the first position where `a` and `b` differ, string `a` has a letter that appears earlier in the alphabet than the corresponding letter in `b`.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "leet", k = 3, letter = "e", repetition = 1
16+
17+
**Output:** "eet"
18+
19+
**Explanation:** There are four subsequences of length 3 that have the letter 'e' appear at least 1 time:
20+
21+
- "lee" (from "**lee**t")
22+
23+
- "let" (from "**le**e**t**")
24+
25+
- "let" (from "**l**e**et**")
26+
27+
- "eet" (from "l**eet**")
28+
29+
The lexicographically smallest subsequence among them is "eet".
30+
31+
**Example 2:**
32+
33+
![example-2](https://assets.leetcode.com/uploads/2021/09/13/smallest-k-length-subsequence.png)
34+
35+
**Input:** s = "leetcode", k = 4, letter = "e", repetition = 2
36+
37+
**Output:** "ecde"
38+
39+
**Explanation:** "ecde" is the lexicographically smallest subsequence of length 4 that has the letter "e" appear at least 2 times.
40+
41+
**Example 3:**
42+
43+
**Input:** s = "bb", k = 2, letter = "b", repetition = 2
44+
45+
**Output:** "bb"
46+
47+
**Explanation:** "bb" is the only subsequence of length 2 that has the letter "b" appear at least 2 times.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= repetition <= k <= s.length <= 5 * 10<sup>4</sup></code>
52+
* `s` consists of lowercase English letters.
53+
* `letter` is a lowercase English letter, and appears in `s` at least `repetition` times.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g2001_2100.s2032_two_out_of_three;
2+
3+
// #Easy #Array #Hash_Table #2022_05_25_Time_9_ms_(45.56%)_Space_47.4_MB_(38.52%)
4+
5+
import java.util.ArrayList;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Set;
9+
10+
public class Solution {
11+
public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
12+
Set<Integer> ans = new HashSet<>();
13+
Set<Integer> set1 = new HashSet<>();
14+
for (int i : nums1) {
15+
set1.add(i);
16+
}
17+
Set<Integer> set2 = new HashSet<>();
18+
for (int i : nums2) {
19+
set2.add(i);
20+
}
21+
Set<Integer> set3 = new HashSet<>();
22+
for (int i : nums3) {
23+
set3.add(i);
24+
}
25+
for (int j : nums1) {
26+
if (set2.contains(j) || set3.contains(j)) {
27+
ans.add(j);
28+
}
29+
}
30+
for (int j : nums2) {
31+
if (set1.contains(j) || set3.contains(j)) {
32+
ans.add(j);
33+
}
34+
}
35+
for (int j : nums3) {
36+
if (set1.contains(j) || set2.contains(j)) {
37+
ans.add(j);
38+
}
39+
}
40+
List<Integer> result = new ArrayList<>();
41+
result.addAll(ans);
42+
return result;
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2032\. Two Out of Three
2+
3+
Easy
4+
5+
Given three integer arrays `nums1`, `nums2`, and `nums3`, return _a **distinct** array containing all the values that are present in **at least two** out of the three arrays. You may return the values in **any** order_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]
10+
11+
**Output:** [3,2]
12+
13+
**Explanation:** The values that are present in at least two arrays are:
14+
15+
- 3, in all three arrays.
16+
17+
- 2, in nums1 and nums2.
18+
19+
**Example 2:**
20+
21+
**Input:** nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]
22+
23+
**Output:** [2,3,1]
24+
25+
**Explanation:** The values that are present in at least two arrays are:
26+
27+
- 2, in nums2 and nums3.
28+
29+
- 3, in nums1 and nums2.
30+
31+
- 1, in nums1 and nums3.
32+
33+
**Example 3:**
34+
35+
**Input:** nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]
36+
37+
**Output:** []
38+
39+
**Explanation:** No value is present in at least two arrays.
40+
41+
**Constraints:**
42+
43+
* `1 <= nums1.length, nums2.length, nums3.length <= 100`
44+
* `1 <= nums1[i], nums2[j], nums3[k] <= 100`

0 commit comments

Comments
 (0)