Skip to content

Commit dfa03e4

Browse files
authored
Added tasks 3096-3100
1 parent 5578f47 commit dfa03e4

File tree

15 files changed

+468
-0
lines changed

15 files changed

+468
-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_20_Time_850_ms_(100.00%)_Space_67.9_MB_(97.22%)
4+
5+
class Solution {
6+
fun minimumLevels(possible: IntArray): Int {
7+
val n = possible.size
8+
var sum = 0
9+
for (p in 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+
var sumLeft = 0
19+
for (i in 0 until n - 1) {
20+
sumLeft += possible[i]
21+
val sumRight = sum - sumLeft
22+
val danScore = sumLeft - ((i + 1) - sumLeft)
23+
val 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,33 @@
1+
package g3001_3100.s3097_shortest_subarray_with_or_at_least_k_ii
2+
3+
// #Medium #Array #Bit_Manipulation #Sliding_Window
4+
// #2024_04_20_Time_489_ms_(93.33%)_Space_79.6_MB_(13.33%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minimumSubarrayLength(nums: IntArray, k: Int): Int {
10+
val n = nums.size
11+
if (nums[0] >= k) {
12+
return 1
13+
}
14+
var res = Int.MAX_VALUE
15+
for (i in 1 until n) {
16+
if (nums[i] >= k) {
17+
return 1
18+
}
19+
var j = i - 1
20+
while (j >= 0 && (nums[i] or nums[j]) != nums[j]) {
21+
nums[j] = nums[j] or nums[i]
22+
if (nums[j] >= k) {
23+
res = min(res, (i - j + 1))
24+
}
25+
j--
26+
}
27+
}
28+
if (res == Int.MAX_VALUE) {
29+
return -1
30+
}
31+
return res
32+
}
33+
}
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,44 @@
1+
package g3001_3100.s3098_find_the_sum_of_subsequence_powers
2+
3+
// #Hard #Array #Dynamic_Programming #Sorting #2024_04_20_Time_294_ms_(77.78%)_Space_49_MB_(66.67%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
private var len = 0
9+
10+
private fun dfs(lastIdx: Int, k: Int, minDiff: Int, dp: MutableMap<Long, Int>, nums: IntArray): Int {
11+
if (k == 0) {
12+
return minDiff
13+
}
14+
val key = ((minDiff.toLong()) shl 12) + (lastIdx.toLong() shl 6) + k
15+
if (dp.containsKey(key)) {
16+
return dp[key]!!
17+
}
18+
var res = 0
19+
for (i in lastIdx + 1..len - k) {
20+
res = (
21+
res + dfs(
22+
i, k - 1, min(minDiff, (nums[i] - nums[lastIdx])), dp, nums
23+
)
24+
) % MOD
25+
}
26+
dp[key] = res
27+
return res
28+
}
29+
30+
fun sumOfPowers(nums: IntArray, k: Int): Int {
31+
len = nums.size
32+
nums.sort()
33+
val dp: MutableMap<Long, Int> = HashMap()
34+
var res = 0
35+
for (i in 0..len - k) {
36+
res = (res + dfs(i, k - 1, nums[len - 1] - nums[0], dp, nums)) % MOD
37+
}
38+
return res
39+
}
40+
41+
companion object {
42+
private const val MOD = 1000000007
43+
}
44+
}
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_20_Time_119_ms_(82.67%)_Space_33.2_MB_(45.33%)
4+
5+
class Solution {
6+
fun sumOfTheDigitsOfHarshadNumber(x: Int): Int {
7+
var sum = 0
8+
var digit: Int
9+
var temp = x
10+
while (temp != 0) {
11+
digit = temp % 10
12+
sum += digit
13+
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,18 @@
1+
package g3001_3100.s3100_water_bottles_ii
2+
3+
// #Medium #Math #Simulation #2024_04_20_Time_137_ms_(70.49%)_Space_33.8_MB_(50.82%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun maxBottlesDrunk(numBottles: Int, numExchange: Int): Int {
8+
var numExchange = numExchange
9+
var emptyBottles = numBottles
10+
var bottleDrinks = numBottles
11+
while (numExchange <= emptyBottles) {
12+
bottleDrinks += 1
13+
emptyBottles = 1 + (emptyBottles - numExchange)
14+
numExchange++
15+
}
16+
return bottleDrinks
17+
}
18+
}
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`

0 commit comments

Comments
 (0)