From 6a1a69ace8efe5e048d5305bd33946b870813ee3 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 14 Jun 2024 09:21:43 +0300 Subject: [PATCH 1/4] Added tasks 3179-3181 --- .../Solution.java | 43 +++++++++++++ .../readme.md | 47 ++++++++++++++ .../Solution.java | 56 +++++++++++++++++ .../readme.md | 37 +++++++++++ .../Solution.java | 63 +++++++++++++++++++ .../readme.md | 37 +++++++++++ .../SolutionTest.java | 18 ++++++ .../SolutionTest.java | 18 ++++++ .../SolutionTest.java | 18 ++++++ 9 files changed, 337 insertions(+) create mode 100644 src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.java create mode 100644 src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md create mode 100644 src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java create mode 100644 src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md create mode 100644 src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/Solution.java create mode 100644 src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md create mode 100644 src/test/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/SolutionTest.java create mode 100644 src/test/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/SolutionTest.java diff --git a/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.java b/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.java new file mode 100644 index 000000000..7c3e3ee50 --- /dev/null +++ b/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.java @@ -0,0 +1,43 @@ +package g3101_3200.s3179_find_the_n_th_value_after_k_seconds; + +// #Medium #Array #Math #Simulation #Prefix_Sum #Combinatorics +// #2024_06_14_Time_2_ms_(99.86%)_Space_40.9_MB_(85.18%) + +public class Solution { + private final int mod = (int) (Math.pow(10, 9) + 7); + + public int valueAfterKSeconds(int n, int k) { + if (n == 1) { + return 1; + } + return combination(k + n - 1, k); + } + + private int combination(int a, int b) { + long numerator = 1; + long denominator = 1; + + for (int i = 0; i < b; i++) { + numerator = (numerator * (a - i)) % mod; + denominator = (denominator * (i + 1)) % mod; + } + + // Calculate the modular inverse of denominator + long denominatorInverse = power(denominator, mod - 2); + + return (int) ((numerator * denominatorInverse) % mod); + } + + // Function to calculate power + private long power(long x, int y) { + long result = 1; + while (y > 0) { + if (y % 2 == 1) { + result = (result * x) % mod; + } + y = y >> 1; + x = (x * x) % mod; + } + return result; + } +} diff --git a/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md b/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md new file mode 100644 index 000000000..5c0f8c7c0 --- /dev/null +++ b/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md @@ -0,0 +1,47 @@ +3179\. Find the N-th Value After K Seconds + +Medium + +You are given two integers `n` and `k`. + +Initially, you start with an array `a` of `n` integers where `a[i] = 1` for all `0 <= i <= n - 1`. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, `a[0]` remains the same, `a[1]` becomes `a[0] + a[1]`, `a[2]` becomes `a[0] + a[1] + a[2]`, and so on. + +Return the **value** of `a[n - 1]` after `k` seconds. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** n = 4, k = 5 + +**Output:** 56 + +**Explanation:** + +| Second | State After | +|--------|-------------------| +| 0 | `[1, 1, 1, 1]` | +| 1 | `[1, 2, 3, 4]` | +| 2 | `[1, 3, 6, 10]` | +| 3 | `[1, 4, 10, 20]` | +| 4 | `[1, 5, 15, 35]` | +| 5 | `[1, 6, 21, 56]` | + +**Example 2:** + +**Input:** n = 5, k = 3 + +**Output:** 35 + +**Explanation:** + +| Second | State After | +|--------|-------------------| +| 0 | `[1, 1, 1, 1, 1]` | +| 1 | `[1, 2, 3, 4, 5]` | +| 2 | `[1, 3, 6, 10, 15]` | +| 3 | `[1, 4, 10, 20, 35]` | + +**Constraints:** + +* `1 <= n, k <= 1000` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java b/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java new file mode 100644 index 000000000..9ad44467d --- /dev/null +++ b/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java @@ -0,0 +1,56 @@ +package g3101_3200.s3180_maximum_total_reward_using_operations_i; + +// #Medium #Array #Dynamic_Programming #2024_06_14_Time_1_ms_(100.00%)_Space_43.3_MB_(97.85%) + +public class Solution { + private int[] sortedSet(int[] values) { + int max = 0; + for (int x : values) { + if (x > max) { + max = x; + } + } + boolean[] set = new boolean[max + 1]; + int n = 0; + for (int x : values) { + if (!set[x]) { + set[x] = true; + n++; + } + } + int[] result = new int[n]; + for (int x = max; x > 0; x--) { + if (set[x]) { + result[--n] = x; + } + } + return result; + } + + public int maxTotalReward(int[] rewardValues) { + rewardValues = sortedSet(rewardValues); + int n = rewardValues.length; + int max = rewardValues[n - 1]; + boolean[] isSumPossible = new boolean[max]; + isSumPossible[0] = true; + int maxSum = 0; + for (int sum = rewardValues[0], last = 1; sum < max; sum++) { + while (last < n && rewardValues[last] <= sum) { + last++; + } + int s2 = sum / 2; + for (int i = last - 1; i >= 0; i--) { + int x = rewardValues[i]; + if (x <= s2) { + break; + } + if (isSumPossible[sum - x]) { + isSumPossible[sum] = true; + maxSum = sum; + break; + } + } + } + return maxSum + max; + } +} diff --git a/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md b/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md new file mode 100644 index 000000000..1e99f1b4b --- /dev/null +++ b/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md @@ -0,0 +1,37 @@ +3180\. Maximum Total Reward Using Operations I + +Medium + +You are given an integer array `rewardValues` of length `n`, representing the values of rewards. + +Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times: + +* Choose an **unmarked** index `i` from the range `[0, n - 1]`. +* If `rewardValues[i]` is **greater** than your current total reward `x`, then add `rewardValues[i]` to `x` (i.e., `x = x + rewardValues[i]`), and **mark** the index `i`. + +Return an integer denoting the **maximum** _total reward_ you can collect by performing the operations optimally. + +**Example 1:** + +**Input:** rewardValues = [1,1,3,3] + +**Output:** 4 + +**Explanation:** + +During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, which is the maximum. + +**Example 2:** + +**Input:** rewardValues = [1,6,4,3,2] + +**Output:** 11 + +**Explanation:** + +Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum. + +**Constraints:** + +* `1 <= rewardValues.length <= 2000` +* `1 <= rewardValues[i] <= 2000` \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/Solution.java b/src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/Solution.java new file mode 100644 index 000000000..0c6dea269 --- /dev/null +++ b/src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/Solution.java @@ -0,0 +1,63 @@ +package g3101_3200.s3181_maximum_total_reward_using_operations_ii; + +// #Hard #Array #Dynamic_Programming #Bit_Manipulation +// #2024_06_14_Time_2_ms_(100.00%)_Space_53.3_MB_(90.35%) + +public class Solution { + public int maxTotalReward(int[] rewardValues) { + int max = rewardValues[0]; + int n = 0; + for (int i = 1; i < rewardValues.length; i++) { + max = Math.max(max, rewardValues[i]); + } + boolean[] vis = new boolean[max + 1]; + for (int i : rewardValues) { + if (!vis[i]) { + n++; + vis[i] = true; + } + } + int[] rew = new int[n]; + int j = 0; + for (int i = 0; i <= max; i++) { + if (vis[i]) { + rew[j++] = i; + } + } + return rew[n - 1] + getAns(rew, n - 1, rew[n - 1] - 1); + } + + private int getAns(int[] rewards, int i, int validLimit) { + int res = 0; + int j = nextElemWithinLimits(rewards, i - 1, validLimit); + for (; j >= 0; j--) { + if (res >= rewards[j] + Math.min(validLimit - rewards[j], rewards[j] - 1)) { + break; + } + res = + Math.max( + res, + rewards[j] + + getAns( + rewards, + j, + Math.min(validLimit - rewards[j], rewards[j] - 1))); + } + return res; + } + + private int nextElemWithinLimits(int[] rewards, int h, int k) { + int l = 0; + int resInd = -1; + while (l <= h) { + int m = (l + h) / 2; + if (rewards[m] <= k) { + resInd = m; + l = m + 1; + } else { + h = m - 1; + } + } + return resInd; + } +} diff --git a/src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md b/src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md new file mode 100644 index 000000000..8f40bfbc0 --- /dev/null +++ b/src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md @@ -0,0 +1,37 @@ +3181\. Maximum Total Reward Using Operations II + +Hard + +You are given an integer array `rewardValues` of length `n`, representing the values of rewards. + +Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times: + +* Choose an **unmarked** index `i` from the range `[0, n - 1]`. +* If `rewardValues[i]` is **greater** than your current total reward `x`, then add `rewardValues[i]` to `x` (i.e., `x = x + rewardValues[i]`), and **mark** the index `i`. + +Return an integer denoting the **maximum** _total reward_ you can collect by performing the operations optimally. + +**Example 1:** + +**Input:** rewardValues = [1,1,3,3] + +**Output:** 4 + +**Explanation:** + +During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, which is the maximum. + +**Example 2:** + +**Input:** rewardValues = [1,6,4,3,2] + +**Output:** 11 + +**Explanation:** + +Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum. + +**Constraints:** + +* 1 <= rewardValues.length <= 5 * 104 +* 1 <= rewardValues[i] <= 5 * 104 \ No newline at end of file diff --git a/src/test/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/SolutionTest.java b/src/test/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/SolutionTest.java new file mode 100644 index 000000000..6dfb316f7 --- /dev/null +++ b/src/test/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3179_find_the_n_th_value_after_k_seconds; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void valueAfterKSeconds() { + assertThat(new Solution().valueAfterKSeconds(4, 5), equalTo(56)); + } + + @Test + void valueAfterKSeconds2() { + assertThat(new Solution().valueAfterKSeconds(5, 3), equalTo(35)); + } +} diff --git a/src/test/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/SolutionTest.java b/src/test/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/SolutionTest.java new file mode 100644 index 000000000..8473316e3 --- /dev/null +++ b/src/test/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3180_maximum_total_reward_using_operations_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxTotalReward() { + assertThat(new Solution().maxTotalReward(new int[] {1, 1, 3, 3}), equalTo(4)); + } + + @Test + void maxTotalReward2() { + assertThat(new Solution().maxTotalReward(new int[] {1, 6, 4, 3, 2}), equalTo(11)); + } +} diff --git a/src/test/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/SolutionTest.java b/src/test/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/SolutionTest.java new file mode 100644 index 000000000..5457bb618 --- /dev/null +++ b/src/test/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3181_maximum_total_reward_using_operations_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxTotalReward() { + assertThat(new Solution().maxTotalReward(new int[] {1, 1, 3, 3}), equalTo(4)); + } + + @Test + void maxTotalReward2() { + assertThat(new Solution().maxTotalReward(new int[] {1, 6, 4, 3, 2}), equalTo(11)); + } +} From 3b36b65cb2579cde2ff76a82cd37bf0c3eafea46 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 14 Jun 2024 09:24:10 +0300 Subject: [PATCH 2/4] Update Solution.java --- .../s3179_find_the_n_th_value_after_k_seconds/Solution.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.java b/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.java index 7c3e3ee50..52978dc1f 100644 --- a/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.java +++ b/src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.java @@ -16,15 +16,12 @@ public int valueAfterKSeconds(int n, int k) { private int combination(int a, int b) { long numerator = 1; long denominator = 1; - for (int i = 0; i < b; i++) { numerator = (numerator * (a - i)) % mod; denominator = (denominator * (i + 1)) % mod; } - // Calculate the modular inverse of denominator long denominatorInverse = power(denominator, mod - 2); - return (int) ((numerator * denominatorInverse) % mod); } From 6ff3a2ec7696fdde329361dc6b6b200674565c37 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 14 Jun 2024 09:27:57 +0300 Subject: [PATCH 3/4] Fixed sonar --- .../Solution.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java b/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java index 9ad44467d..6f1013a96 100644 --- a/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java +++ b/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java @@ -2,6 +2,7 @@ // #Medium #Array #Dynamic_Programming #2024_06_14_Time_1_ms_(100.00%)_Space_43.3_MB_(97.85%) +@SuppressWarnings("java:S135") public class Solution { private int[] sortedSet(int[] values) { int max = 0; @@ -34,7 +35,8 @@ public int maxTotalReward(int[] rewardValues) { boolean[] isSumPossible = new boolean[max]; isSumPossible[0] = true; int maxSum = 0; - for (int sum = rewardValues[0], last = 1; sum < max; sum++) { + int last = 1; + for (int sum = rewardValues[0]; sum < max; sum++) { while (last < n && rewardValues[last] <= sum) { last++; } @@ -50,6 +52,7 @@ public int maxTotalReward(int[] rewardValues) { break; } } + last++; } return maxSum + max; } From 5a72d258aa7c8fb3b2854741a338a9926d705b0e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 14 Jun 2024 09:31:20 +0300 Subject: [PATCH 4/4] Fixed test --- .../s3180_maximum_total_reward_using_operations_i/Solution.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java b/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java index 6f1013a96..75a33fc36 100644 --- a/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java +++ b/src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java @@ -52,7 +52,6 @@ public int maxTotalReward(int[] rewardValues) { break; } } - last++; } return maxSum + max; }