Skip to content

Added tasks 3096-3100 #1742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g3001_3100.s3096_minimum_levels_to_gain_more_points;

// #Medium #Array #Prefix_Sum #2024_04_19_Time_3_ms_(99.97%)_Space_59.6_MB_(49.99%)

public class Solution {
public int minimumLevels(int[] possible) {
int n = possible.length;
int sum = 0;
for (int p : possible) {
sum += p;
}
if (sum == 0 && n == 2) {
return -1;
}
if (sum == 0 && n > 2) {
return 1;
}
int sumLeft = 0;
for (int i = 0; i < n - 1; i++) {
sumLeft += possible[i];
int sumRight = sum - sumLeft;
int danScore = sumLeft - ((i + 1) - sumLeft);
int bobScore = sumRight - ((n - i - 1) - sumRight);
if (danScore > bobScore) {
return i + 1;
}
}
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
3096\. Minimum Levels to Gain More Points

Medium

You are given a binary array `possible` of length `n`.

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.

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.

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.

Return _the **minimum** number of levels Alice should play to gain more points_. _If this is **not** possible, return_ `-1`.

**Note** that each player must play at least `1` level.

**Example 1:**

**Input:** possible = [1,0,1,0]

**Output:** 1

**Explanation:**

Let's look at all the levels that Alice can play up to:

* 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.
* 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.
* 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.

Alice must play a minimum of 1 level to gain more points.

**Example 2:**

**Input:** possible = [1,1,1,1,1]

**Output:** 3

**Explanation:**

Let's look at all the levels that Alice can play up to:

* If Alice plays only level 0 and Bob plays the rest of the levels, Alice has 1 point, while Bob has 4 points.
* If Alice plays till level 1 and Bob plays the rest of the levels, Alice has 2 points, while Bob has 3 points.
* If Alice plays till level 2 and Bob plays the rest of the levels, Alice has 3 points, while Bob has 2 points.
* If Alice plays till level 3 and Bob plays the rest of the levels, Alice has 4 points, while Bob has 1 point.

Alice must play a minimum of 3 levels to gain more points.

**Example 3:**

**Input:** possible = [0,0]

**Output:** \-1

**Explanation:**

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.

**Constraints:**

* <code>2 <= n == possible.length <= 10<sup>5</sup></code>
* `possible[i]` is either `0` or `1`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g3001_3100.s3097_shortest_subarray_with_or_at_least_k_ii;

// #Medium #Array #Bit_Manipulation #Sliding_Window
// #2024_04_19_Time_7_ms_(98.43%)_Space_70.2_MB_(74.25%)

public class Solution {
public int minimumSubarrayLength(int[] nums, int k) {
int n = nums.length;
if (nums[0] >= k) {
return 1;
}
int res = Integer.MAX_VALUE;
for (int i = 1; i < n; i++) {
if (nums[i] >= k) {
return 1;
}
for (int j = i - 1; j >= 0 && (nums[i] | nums[j]) != nums[j]; j--) {
nums[j] |= nums[i];
if (nums[j] >= k) {
res = Math.min(res, i - j + 1);
}
}
}
if (res == Integer.MAX_VALUE) {
return -1;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3097\. Shortest Subarray With OR at Least K II

Medium

You are given an array `nums` of **non-negative** integers and an integer `k`.

An array is called **special** if the bitwise `OR` of all of its elements is **at least** `k`.

Return _the length of the **shortest** **special** **non-empty** subarray of_ `nums`, _or return_ `-1` _if no special subarray exists_.

**Example 1:**

**Input:** nums = [1,2,3], k = 2

**Output:** 1

**Explanation:**

The subarray `[3]` has `OR` value of `3`. Hence, we return `1`.

**Example 2:**

**Input:** nums = [2,1,8], k = 10

**Output:** 3

**Explanation:**

The subarray `[2,1,8]` has `OR` value of `11`. Hence, we return `3`.

**Example 3:**

**Input:** nums = [1,2], k = 0

**Output:** 1

**Explanation:**

The subarray `[1]` has `OR` value of `1`. Hence, we return `1`.

**Constraints:**

* <code>1 <= nums.length <= 2 * 10<sup>5</sup></code>
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
* <code>0 <= k <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package g3001_3100.s3098_find_the_sum_of_subsequence_powers;

// #Hard #Array #Dynamic_Programming #Sorting #2024_04_19_Time_34_ms_(91.54%)_Space_47.9_MB_(65.64%)

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {
private static final int MOD = 1_000_000_007;
private int len;

private int dfs(int lastIdx, int k, int minDiff, Map<Long, Integer> dp, int[] nums) {
if (k == 0) {
return minDiff;
}
long key = (((long) minDiff) << 12) + ((long) lastIdx << 6) + k;
if (dp.containsKey(key)) {
return dp.get(key);
}
int res = 0;
for (int i = lastIdx + 1; i <= len - k; i++) {
res = (res + dfs(i, k - 1, Math.min(minDiff, nums[i] - nums[lastIdx]), dp, nums)) % MOD;
}
dp.put(key, res);
return res;
}

public int sumOfPowers(int[] nums, int k) {
len = nums.length;
Arrays.sort(nums);
Map<Long, Integer> dp = new HashMap<>();
int res = 0;
for (int i = 0; i <= len - k; i++) {
res = (res + dfs(i, k - 1, nums[len - 1] - nums[0], dp, nums)) % MOD;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
3098\. Find the Sum of Subsequence Powers

Hard

You are given an integer array `nums` of length `n`, and a **positive** integer `k`.

The **power** of a subsequence is defined as the **minimum** absolute difference between **any** two elements in the subsequence.

Return _the **sum** of **powers** of **all** subsequences of_ `nums` _which have length_ **_equal to_** `k`.

Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** nums = [1,2,3,4], k = 3

**Output:** 4

**Explanation:**

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`.

**Example 2:**

**Input:** nums = [2,2], k = 2

**Output:** 0

**Explanation:**

The only subsequence in `nums` which has length 2 is `[2,2]`. The sum of powers is `|2 - 2| = 0`.

**Example 3:**

**Input:** nums = [4,3,-1], k = 2

**Output:** 10

**Explanation:**

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`.

**Constraints:**

* `2 <= n == nums.length <= 50`
* <code>-10<sup>8</sup> <= nums[i] <= 10<sup>8</sup></code>
* `2 <= k <= n`
20 changes: 20 additions & 0 deletions src/main/java/g3001_3100/s3099_harshad_number/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3001_3100.s3099_harshad_number;

// #Easy #Math #2024_04_19_Time_0_ms_(100.00%)_Space_40.9_MB_(7.00%)

public class Solution {
public int sumOfTheDigitsOfHarshadNumber(int x) {
int sum = 0;
int digit;
int temp = x;
while (temp != 0) {
digit = temp % 10;
sum = sum + digit;
temp = temp / 10;
}
if (sum != 0 && x % sum == 0) {
return sum;
}
return -1;
}
}
29 changes: 29 additions & 0 deletions src/main/java/g3001_3100/s3099_harshad_number/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
3099\. Harshad Number

Easy

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`_._

**Example 1:**

**Input:** x = 18

**Output:** 9

**Explanation:**

The sum of digits of `x` is `9`. `18` is divisible by `9`. So `18` is a Harshad number and the answer is `9`.

**Example 2:**

**Input:** x = 23

**Output:** \-1

**Explanation:**

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`.

**Constraints:**

* `1 <= x <= 100`
16 changes: 16 additions & 0 deletions src/main/java/g3001_3100/s3100_water_bottles_ii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package g3001_3100.s3100_water_bottles_ii;

// #Medium #Math #Simulation #2024_04_19_Time_0_ms_(100.00%)_Space_40.8_MB_(45.33%)

public class Solution {
public int maxBottlesDrunk(int numBottles, int numExchange) {
int emptyBottles = numBottles;
int bottleDrinks = numBottles;
while (numExchange <= emptyBottles) {
bottleDrinks += 1;
emptyBottles = 1 + (emptyBottles - numExchange);
numExchange++;
}
return bottleDrinks;
}
}
39 changes: 39 additions & 0 deletions src/main/java/g3001_3100/s3100_water_bottles_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
3100\. Water Bottles II

Medium

You are given two integers `numBottles` and `numExchange`.

`numBottles` represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:

* Drink any number of full water bottles turning them into empty bottles.
* Exchange `numExchange` empty bottles with one full water bottle. Then, increase `numExchange` by one.

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.

Return _the **maximum** number of water bottles you can drink_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2024/01/28/exampleone1.png)

**Input:** numBottles = 13, numExchange = 6

**Output:** 15

**Explanation:** The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/01/28/example231.png)

**Input:** numBottles = 10, numExchange = 3

**Output:** 13

**Explanation:** The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.

**Constraints:**

* `1 <= numBottles <= 100`
* `1 <= numExchange <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3001_3100.s3096_minimum_levels_to_gain_more_points;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void minimumLevels() {
assertThat(new Solution().minimumLevels(new int[] {1, 0, 1, 0}), equalTo(1));
}

@Test
void minimumLevels2() {
assertThat(new Solution().minimumLevels(new int[] {1, 1, 1, 1, 1}), equalTo(3));
}

@Test
void minimumLevels3() {
assertThat(new Solution().minimumLevels(new int[] {0, 0}), equalTo(-1));
}
}
Loading
Loading