Skip to content

Commit cd2f210

Browse files
authored
Added tasks 3179-3181
1 parent 0fc60c3 commit cd2f210

File tree

9 files changed

+336
-0
lines changed

9 files changed

+336
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3101_3200.s3179_find_the_n_th_value_after_k_seconds;
2+
3+
// #Medium #Array #Math #Simulation #Prefix_Sum #Combinatorics
4+
// #2024_06_14_Time_2_ms_(99.86%)_Space_40.9_MB_(85.18%)
5+
6+
public class Solution {
7+
private final int mod = (int) (Math.pow(10, 9) + 7);
8+
9+
public int valueAfterKSeconds(int n, int k) {
10+
if (n == 1) {
11+
return 1;
12+
}
13+
return combination(k + n - 1, k);
14+
}
15+
16+
private int combination(int a, int b) {
17+
long numerator = 1;
18+
long denominator = 1;
19+
for (int i = 0; i < b; i++) {
20+
numerator = (numerator * (a - i)) % mod;
21+
denominator = (denominator * (i + 1)) % mod;
22+
}
23+
// Calculate the modular inverse of denominator
24+
long denominatorInverse = power(denominator, mod - 2);
25+
return (int) ((numerator * denominatorInverse) % mod);
26+
}
27+
28+
// Function to calculate power
29+
private long power(long x, int y) {
30+
long result = 1;
31+
while (y > 0) {
32+
if (y % 2 == 1) {
33+
result = (result * x) % mod;
34+
}
35+
y = y >> 1;
36+
x = (x * x) % mod;
37+
}
38+
return result;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
3179\. Find the N-th Value After K Seconds
2+
3+
Medium
4+
5+
You are given two integers `n` and `k`.
6+
7+
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.
8+
9+
Return the **value** of `a[n - 1]` after `k` seconds.
10+
11+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 4, k = 5
16+
17+
**Output:** 56
18+
19+
**Explanation:**
20+
21+
| Second | State After |
22+
|--------|-------------------|
23+
| 0 | `[1, 1, 1, 1]` |
24+
| 1 | `[1, 2, 3, 4]` |
25+
| 2 | `[1, 3, 6, 10]` |
26+
| 3 | `[1, 4, 10, 20]` |
27+
| 4 | `[1, 5, 15, 35]` |
28+
| 5 | `[1, 6, 21, 56]` |
29+
30+
**Example 2:**
31+
32+
**Input:** n = 5, k = 3
33+
34+
**Output:** 35
35+
36+
**Explanation:**
37+
38+
| Second | State After |
39+
|--------|-------------------|
40+
| 0 | `[1, 1, 1, 1, 1]` |
41+
| 1 | `[1, 2, 3, 4, 5]` |
42+
| 2 | `[1, 3, 6, 10, 15]` |
43+
| 3 | `[1, 4, 10, 20, 35]` |
44+
45+
**Constraints:**
46+
47+
* `1 <= n, k <= 1000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g3101_3200.s3180_maximum_total_reward_using_operations_i;
2+
3+
// #Medium #Array #Dynamic_Programming #2024_06_14_Time_1_ms_(100.00%)_Space_43.3_MB_(97.85%)
4+
5+
@SuppressWarnings("java:S135")
6+
public class Solution {
7+
private int[] sortedSet(int[] values) {
8+
int max = 0;
9+
for (int x : values) {
10+
if (x > max) {
11+
max = x;
12+
}
13+
}
14+
boolean[] set = new boolean[max + 1];
15+
int n = 0;
16+
for (int x : values) {
17+
if (!set[x]) {
18+
set[x] = true;
19+
n++;
20+
}
21+
}
22+
int[] result = new int[n];
23+
for (int x = max; x > 0; x--) {
24+
if (set[x]) {
25+
result[--n] = x;
26+
}
27+
}
28+
return result;
29+
}
30+
31+
public int maxTotalReward(int[] rewardValues) {
32+
rewardValues = sortedSet(rewardValues);
33+
int n = rewardValues.length;
34+
int max = rewardValues[n - 1];
35+
boolean[] isSumPossible = new boolean[max];
36+
isSumPossible[0] = true;
37+
int maxSum = 0;
38+
int last = 1;
39+
for (int sum = rewardValues[0]; sum < max; sum++) {
40+
while (last < n && rewardValues[last] <= sum) {
41+
last++;
42+
}
43+
int s2 = sum / 2;
44+
for (int i = last - 1; i >= 0; i--) {
45+
int x = rewardValues[i];
46+
if (x <= s2) {
47+
break;
48+
}
49+
if (isSumPossible[sum - x]) {
50+
isSumPossible[sum] = true;
51+
maxSum = sum;
52+
break;
53+
}
54+
}
55+
}
56+
return maxSum + max;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
3180\. Maximum Total Reward Using Operations I
2+
3+
Medium
4+
5+
You are given an integer array `rewardValues` of length `n`, representing the values of rewards.
6+
7+
Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times:
8+
9+
* Choose an **unmarked** index `i` from the range `[0, n - 1]`.
10+
* 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`.
11+
12+
Return an integer denoting the **maximum** _total reward_ you can collect by performing the operations optimally.
13+
14+
**Example 1:**
15+
16+
**Input:** rewardValues = [1,1,3,3]
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
22+
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.
23+
24+
**Example 2:**
25+
26+
**Input:** rewardValues = [1,6,4,3,2]
27+
28+
**Output:** 11
29+
30+
**Explanation:**
31+
32+
Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.
33+
34+
**Constraints:**
35+
36+
* `1 <= rewardValues.length <= 2000`
37+
* `1 <= rewardValues[i] <= 2000`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package g3101_3200.s3181_maximum_total_reward_using_operations_ii;
2+
3+
// #Hard #Array #Dynamic_Programming #Bit_Manipulation
4+
// #2024_06_14_Time_2_ms_(100.00%)_Space_53.3_MB_(90.35%)
5+
6+
public class Solution {
7+
public int maxTotalReward(int[] rewardValues) {
8+
int max = rewardValues[0];
9+
int n = 0;
10+
for (int i = 1; i < rewardValues.length; i++) {
11+
max = Math.max(max, rewardValues[i]);
12+
}
13+
boolean[] vis = new boolean[max + 1];
14+
for (int i : rewardValues) {
15+
if (!vis[i]) {
16+
n++;
17+
vis[i] = true;
18+
}
19+
}
20+
int[] rew = new int[n];
21+
int j = 0;
22+
for (int i = 0; i <= max; i++) {
23+
if (vis[i]) {
24+
rew[j++] = i;
25+
}
26+
}
27+
return rew[n - 1] + getAns(rew, n - 1, rew[n - 1] - 1);
28+
}
29+
30+
private int getAns(int[] rewards, int i, int validLimit) {
31+
int res = 0;
32+
int j = nextElemWithinLimits(rewards, i - 1, validLimit);
33+
for (; j >= 0; j--) {
34+
if (res >= rewards[j] + Math.min(validLimit - rewards[j], rewards[j] - 1)) {
35+
break;
36+
}
37+
res =
38+
Math.max(
39+
res,
40+
rewards[j]
41+
+ getAns(
42+
rewards,
43+
j,
44+
Math.min(validLimit - rewards[j], rewards[j] - 1)));
45+
}
46+
return res;
47+
}
48+
49+
private int nextElemWithinLimits(int[] rewards, int h, int k) {
50+
int l = 0;
51+
int resInd = -1;
52+
while (l <= h) {
53+
int m = (l + h) / 2;
54+
if (rewards[m] <= k) {
55+
resInd = m;
56+
l = m + 1;
57+
} else {
58+
h = m - 1;
59+
}
60+
}
61+
return resInd;
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
3181\. Maximum Total Reward Using Operations II
2+
3+
Hard
4+
5+
You are given an integer array `rewardValues` of length `n`, representing the values of rewards.
6+
7+
Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times:
8+
9+
* Choose an **unmarked** index `i` from the range `[0, n - 1]`.
10+
* 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`.
11+
12+
Return an integer denoting the **maximum** _total reward_ you can collect by performing the operations optimally.
13+
14+
**Example 1:**
15+
16+
**Input:** rewardValues = [1,1,3,3]
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
22+
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.
23+
24+
**Example 2:**
25+
26+
**Input:** rewardValues = [1,6,4,3,2]
27+
28+
**Output:** 11
29+
30+
**Explanation:**
31+
32+
Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= rewardValues.length <= 5 * 10<sup>4</sup></code>
37+
* <code>1 <= rewardValues[i] <= 5 * 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3179_find_the_n_th_value_after_k_seconds;
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 valueAfterKSeconds() {
11+
assertThat(new Solution().valueAfterKSeconds(4, 5), equalTo(56));
12+
}
13+
14+
@Test
15+
void valueAfterKSeconds2() {
16+
assertThat(new Solution().valueAfterKSeconds(5, 3), equalTo(35));
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3180_maximum_total_reward_using_operations_i;
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 maxTotalReward() {
11+
assertThat(new Solution().maxTotalReward(new int[] {1, 1, 3, 3}), equalTo(4));
12+
}
13+
14+
@Test
15+
void maxTotalReward2() {
16+
assertThat(new Solution().maxTotalReward(new int[] {1, 6, 4, 3, 2}), equalTo(11));
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3181_maximum_total_reward_using_operations_ii;
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 maxTotalReward() {
11+
assertThat(new Solution().maxTotalReward(new int[] {1, 1, 3, 3}), equalTo(4));
12+
}
13+
14+
@Test
15+
void maxTotalReward2() {
16+
assertThat(new Solution().maxTotalReward(new int[] {1, 6, 4, 3, 2}), equalTo(11));
17+
}
18+
}

0 commit comments

Comments
 (0)