forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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** <code>10<sup>9</sup> + 7</code>. | ||
|
||
**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` |
58 changes: 58 additions & 0 deletions
58
src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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%) | ||
|
||
@SuppressWarnings("java:S135") | ||
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; | ||
int last = 1; | ||
for (int sum = rewardValues[0]; 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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
63 changes: 63 additions & 0 deletions
63
src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:** | ||
|
||
* <code>1 <= rewardValues.length <= 5 * 10<sup>4</sup></code> | ||
* <code>1 <= rewardValues[i] <= 5 * 10<sup>4</sup></code> |
18 changes: 18 additions & 0 deletions
18
src/test/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/g3101_3200/s3180_maximum_total_reward_using_operations_i/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |