Skip to content

Commit b96887e

Browse files
authored
Added tasks 3096-3100
1 parent f1434a7 commit b96887e

File tree

15 files changed

+462
-0
lines changed

15 files changed

+462
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3001_3100.s3096_minimum_levels_to_gain_more_points;
2+
3+
// #Medium #Array #Prefix_Sum #2024_04_19_Time_3_ms_(99.97%)_Space_59.6_MB_(49.99%)
4+
5+
public class Solution {
6+
public int minimumLevels(int[] possible) {
7+
int n = possible.length;
8+
int sum = 0;
9+
for (int p : possible) {
10+
sum += p;
11+
}
12+
if (sum == 0 && n == 2) {
13+
return -1;
14+
}
15+
if (sum == 0 && n > 2) {
16+
return 1;
17+
}
18+
int sumLeft = 0;
19+
for (int i = 0; i < n - 1; i++) {
20+
sumLeft += possible[i];
21+
int sumRight = sum - sumLeft;
22+
int danScore = sumLeft - ((i + 1) - sumLeft);
23+
int bobScore = sumRight - ((n - i - 1) - sumRight);
24+
if (danScore > bobScore) {
25+
return i + 1;
26+
}
27+
}
28+
return -1;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
3096\. Minimum Levels to Gain More Points
2+
3+
Medium
4+
5+
You are given a binary array `possible` of length `n`.
6+
7+
Alice and Bob are playing a game that consists of `n` levels. Some of the levels in the game are **impossible** to clear while others can **always** be cleared. In particular, if `possible[i] == 0`, then the <code>i<sup>th</sup></code> level is **impossible** to clear for **both** the players. A player gains `1` point on clearing a level and loses `1` point if the player fails to clear it.
8+
9+
At the start of the game, Alice will play some levels in the **given order** starting from the <code>0<sup>th</sup></code> level, after which Bob will play for the rest of the levels.
10+
11+
Alice wants to know the **minimum** number of levels she should play to gain more points than Bob, if both players play optimally to **maximize** their points.
12+
13+
Return _the **minimum** number of levels Alice should play to gain more points_. _If this is **not** possible, return_ `-1`.
14+
15+
**Note** that each player must play at least `1` level.
16+
17+
**Example 1:**
18+
19+
**Input:** possible = [1,0,1,0]
20+
21+
**Output:** 1
22+
23+
**Explanation:**
24+
25+
Let's look at all the levels that Alice can play up to:
26+
27+
* If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has -1 + 1 - 1 = -1 point.
28+
* If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 1 - 1 = 0 points, while Bob has 1 - 1 = 0 points.
29+
* If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 1 - 1 + 1 = 1 point, while Bob has -1 point.
30+
31+
Alice must play a minimum of 1 level to gain more points.
32+
33+
**Example 2:**
34+
35+
**Input:** possible = [1,1,1,1,1]
36+
37+
**Output:** 3
38+
39+
**Explanation:**
40+
41+
Let's look at all the levels that Alice can play up to:
42+
43+
* If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has 4 points.
44+
* If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 2 points, while Bob has 3 points.
45+
* If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 3 points, while Bob has 2 points.
46+
* If Alice plays till level 3 and Bob plays the rest of the levels, Alice has 4 points, while Bob has 1 point.
47+
48+
Alice must play a minimum of 3 levels to gain more points.
49+
50+
**Example 3:**
51+
52+
**Input:** possible = [0,0]
53+
54+
**Output:** \-1
55+
56+
**Explanation:**
57+
58+
The only possible way is for both players to play 1 level each. Alice plays level 0 and loses 1 point. Bob plays level 1 and loses 1 point. As both players have equal points, Alice can't gain more points than Bob.
59+
60+
**Constraints:**
61+
62+
* <code>2 <= n == possible.length <= 10<sup>5</sup></code>
63+
* `possible[i]` is either `0` or `1`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3001_3100.s3097_shortest_subarray_with_or_at_least_k_ii;
2+
3+
// #Medium #Array #Bit_Manipulation #Sliding_Window
4+
// #2024_04_19_Time_7_ms_(98.43%)_Space_70.2_MB_(74.25%)
5+
6+
public class Solution {
7+
public int minimumSubarrayLength(int[] nums, int k) {
8+
int n = nums.length;
9+
if (nums[0] >= k) {
10+
return 1;
11+
}
12+
int res = Integer.MAX_VALUE;
13+
for (int i = 1; i < n; i++) {
14+
if (nums[i] >= k) {
15+
return 1;
16+
}
17+
for (int j = i - 1; j >= 0 && (nums[i] | nums[j]) != nums[j]; j--) {
18+
nums[j] |= nums[i];
19+
if (nums[j] >= k) {
20+
res = Math.min(res, i - j + 1);
21+
}
22+
}
23+
}
24+
if (res == Integer.MAX_VALUE) {
25+
return -1;
26+
}
27+
return res;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3097\. Shortest Subarray With OR at Least K II
2+
3+
Medium
4+
5+
You are given an array `nums` of **non-negative** integers and an integer `k`.
6+
7+
An array is called **special** if the bitwise `OR` of all of its elements is **at least** `k`.
8+
9+
Return _the length of the **shortest** **special** **non-empty** subarray of_ `nums`, _or return_ `-1` _if no special subarray exists_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3], k = 2
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
The subarray `[3]` has `OR` value of `3`. Hence, we return `1`.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [2,1,8], k = 10
24+
25+
**Output:** 3
26+
27+
**Explanation:**
28+
29+
The subarray `[2,1,8]` has `OR` value of `11`. Hence, we return `3`.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [1,2], k = 0
34+
35+
**Output:** 1
36+
37+
**Explanation:**
38+
39+
The subarray `[1]` has `OR` value of `1`. Hence, we return `1`.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= nums.length <= 2 * 10<sup>5</sup></code>
44+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
45+
* <code>0 <= k <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3001_3100.s3098_find_the_sum_of_subsequence_powers;
2+
3+
// #Hard #Array #Dynamic_Programming #Sorting #2024_04_19_Time_34_ms_(91.54%)_Space_47.9_MB_(65.64%)
4+
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class Solution {
10+
private static final int MOD = 1_000_000_007;
11+
private int len;
12+
13+
private int dfs(int lastIdx, int k, int minDiff, Map<Long, Integer> dp, int[] nums) {
14+
if (k == 0) {
15+
return minDiff;
16+
}
17+
long key = (((long) minDiff) << 12) + ((long) lastIdx << 6) + k;
18+
if (dp.containsKey(key)) {
19+
return dp.get(key);
20+
}
21+
int res = 0;
22+
for (int i = lastIdx + 1; i <= len - k; i++) {
23+
res = (res + dfs(i, k - 1, Math.min(minDiff, nums[i] - nums[lastIdx]), dp, nums)) % MOD;
24+
}
25+
dp.put(key, res);
26+
return res;
27+
}
28+
29+
public int sumOfPowers(int[] nums, int k) {
30+
len = nums.length;
31+
Arrays.sort(nums);
32+
Map<Long, Integer> dp = new HashMap<>();
33+
int res = 0;
34+
for (int i = 0; i <= len - k; i++) {
35+
res = (res + dfs(i, k - 1, nums[len - 1] - nums[0], dp, nums)) % MOD;
36+
}
37+
return res;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3098\. Find the Sum of Subsequence Powers
2+
3+
Hard
4+
5+
You are given an integer array `nums` of length `n`, and a **positive** integer `k`.
6+
7+
The **power** of a subsequence is defined as the **minimum** absolute difference between **any** two elements in the subsequence.
8+
9+
Return _the **sum** of **powers** of **all** subsequences of_ `nums` _which have length_ **_equal to_** `k`.
10+
11+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3,4], k = 3
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
There are 4 subsequences in `nums` which have length 3: `[1,2,3]`, `[1,3,4]`, `[1,2,4]`, and `[2,3,4]`. The sum of powers is `|2 - 3| + |3 - 4| + |2 - 1| + |3 - 4| = 4`.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [2,2], k = 2
26+
27+
**Output:** 0
28+
29+
**Explanation:**
30+
31+
The only subsequence in `nums` which has length 2 is `[2,2]`. The sum of powers is `|2 - 2| = 0`.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [4,3,-1], k = 2
36+
37+
**Output:** 10
38+
39+
**Explanation:**
40+
41+
There are 3 subsequences in `nums` which have length 2: `[4,3]`, `[4,-1]`, and `[3,-1]`. The sum of powers is `|4 - 3| + |4 - (-1)| + |3 - (-1)| = 10`.
42+
43+
**Constraints:**
44+
45+
* `2 <= n == nums.length <= 50`
46+
* <code>-10<sup>8</sup> <= nums[i] <= 10<sup>8</sup></code>
47+
* `2 <= k <= n`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3001_3100.s3099_harshad_number;
2+
3+
// #Easy #Math #2024_04_19_Time_0_ms_(100.00%)_Space_40.9_MB_(7.00%)
4+
5+
public class Solution {
6+
public int sumOfTheDigitsOfHarshadNumber(int x) {
7+
int sum = 0;
8+
int digit;
9+
int temp = x;
10+
while (temp != 0) {
11+
digit = temp % 10;
12+
sum = sum + digit;
13+
temp = temp / 10;
14+
}
15+
if (sum != 0 && x % sum == 0) {
16+
return sum;
17+
}
18+
return -1;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
3099\. Harshad Number
2+
3+
Easy
4+
5+
An integer divisible by the **sum** of its digits is said to be a **Harshad** number. You are given an integer `x`. Return _the sum of the digits_ of `x` if `x` is a **Harshad** number, otherwise, return `-1`_._
6+
7+
**Example 1:**
8+
9+
**Input:** x = 18
10+
11+
**Output:** 9
12+
13+
**Explanation:**
14+
15+
The sum of digits of `x` is `9`. `18` is divisible by `9`. So `18` is a Harshad number and the answer is `9`.
16+
17+
**Example 2:**
18+
19+
**Input:** x = 23
20+
21+
**Output:** \-1
22+
23+
**Explanation:**
24+
25+
The sum of digits of `x` is `5`. `23` is not divisible by `5`. So `23` is not a Harshad number and the answer is `-1`.
26+
27+
**Constraints:**
28+
29+
* `1 <= x <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g3001_3100.s3100_water_bottles_ii;
2+
3+
// #Medium #Math #Simulation #2024_04_19_Time_0_ms_(100.00%)_Space_40.8_MB_(45.33%)
4+
5+
public class Solution {
6+
public int maxBottlesDrunk(int numBottles, int numExchange) {
7+
int emptyBottles = numBottles;
8+
int bottleDrinks = numBottles;
9+
while (numExchange <= emptyBottles) {
10+
bottleDrinks += 1;
11+
emptyBottles = 1 + (emptyBottles - numExchange);
12+
numExchange++;
13+
}
14+
return bottleDrinks;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3100\. Water Bottles II
2+
3+
Medium
4+
5+
You are given two integers `numBottles` and `numExchange`.
6+
7+
`numBottles` represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:
8+
9+
* Drink any number of full water bottles turning them into empty bottles.
10+
* Exchange `numExchange` empty bottles with one full water bottle. Then, increase `numExchange` by one.
11+
12+
Note that you cannot exchange multiple batches of empty bottles for the same value of `numExchange`. For example, if `numBottles == 3` and `numExchange == 1`, you cannot exchange `3` empty water bottles for `3` full bottles.
13+
14+
Return _the **maximum** number of water bottles you can drink_.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2024/01/28/exampleone1.png)
19+
20+
**Input:** numBottles = 13, numExchange = 6
21+
22+
**Output:** 15
23+
24+
**Explanation:** The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
25+
26+
**Example 2:**
27+
28+
![](https://assets.leetcode.com/uploads/2024/01/28/example231.png)
29+
30+
**Input:** numBottles = 10, numExchange = 3
31+
32+
**Output:** 13
33+
34+
**Explanation:** The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
35+
36+
**Constraints:**
37+
38+
* `1 <= numBottles <= 100`
39+
* `1 <= numExchange <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3001_3100.s3096_minimum_levels_to_gain_more_points;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minimumLevels() {
11+
assertThat(new Solution().minimumLevels(new int[] {1, 0, 1, 0}), equalTo(1));
12+
}
13+
14+
@Test
15+
void minimumLevels2() {
16+
assertThat(new Solution().minimumLevels(new int[] {1, 1, 1, 1, 1}), equalTo(3));
17+
}
18+
19+
@Test
20+
void minimumLevels3() {
21+
assertThat(new Solution().minimumLevels(new int[] {0, 0}), equalTo(-1));
22+
}
23+
}

0 commit comments

Comments
 (0)