Skip to content

Commit 644643d

Browse files
authored
Added tasks 3179, 3180, 3181
1 parent d7cb423 commit 644643d

File tree

4 files changed

+295
-0
lines changed
  • src/main/java/g3101_3200
    • s3179_find_the_n_th_value_after_k_seconds
    • s3180_maximum_total_reward_using_operations_i
    • s3181_maximum_total_reward_using_operations_ii

4 files changed

+295
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,9 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3181 |[Maximum Total Reward Using Operations II](src/main/java/g3101_3200/s3181_maximum_total_reward_using_operations_ii)| Hard | Array, Dynamic_Programming, Bit_Manipulation | 2 | 100.00
1820+
| 3180 |[Maximum Total Reward Using Operations I](src/main/java/g3101_3200/s3180_maximum_total_reward_using_operations_i)| Medium | Array, Dynamic_Programming | 1 | 100.00
1821+
| 3179 |[Find the N-th Value After K Seconds](src/main/java/g3101_3200/s3179_find_the_n_th_value_after_k_seconds)| Medium | Array, Math, Simulation, Prefix_Sum, Combinatorics | 2 | 99.86
18191822
| 3178 |[Find the Child Who Has the Ball After K Seconds](src/main/java/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds)| Easy | Math, Simulation | 0 | 100.00
18201823
| 3177 |[Find the Maximum Length of a Good Subsequence II](src/main/java/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii)| Hard | Array, Hash_Table, Dynamic_Programming | 11 | 100.00
18211824
| 3176 |[Find the Maximum Length of a Good Subsequence I](src/main/java/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i)| Medium | Array, Hash_Table, Dynamic_Programming | 4 | 99.70
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3179\. Find the N-th Value After K Seconds
5+
6+
Medium
7+
8+
You are given two integers `n` and `k`.
9+
10+
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.
11+
12+
Return the **value** of `a[n - 1]` after `k` seconds.
13+
14+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 4, k = 5
19+
20+
**Output:** 56
21+
22+
**Explanation:**
23+
24+
| Second | State After |
25+
|--------|-------------------|
26+
| 0 | `[1, 1, 1, 1]` |
27+
| 1 | `[1, 2, 3, 4]` |
28+
| 2 | `[1, 3, 6, 10]` |
29+
| 3 | `[1, 4, 10, 20]` |
30+
| 4 | `[1, 5, 15, 35]` |
31+
| 5 | `[1, 6, 21, 56]` |
32+
33+
**Example 2:**
34+
35+
**Input:** n = 5, k = 3
36+
37+
**Output:** 35
38+
39+
**Explanation:**
40+
41+
| Second | State After |
42+
|--------|-------------------|
43+
| 0 | `[1, 1, 1, 1, 1]` |
44+
| 1 | `[1, 2, 3, 4, 5]` |
45+
| 2 | `[1, 3, 6, 10, 15]` |
46+
| 3 | `[1, 4, 10, 20, 35]` |
47+
48+
**Constraints:**
49+
50+
* `1 <= n, k <= 1000`
51+
52+
## Solution
53+
54+
```java
55+
public class Solution {
56+
private final int mod = (int) (Math.pow(10, 9) + 7);
57+
58+
public int valueAfterKSeconds(int n, int k) {
59+
if (n == 1) {
60+
return 1;
61+
}
62+
return combination(k + n - 1, k);
63+
}
64+
65+
private int combination(int a, int b) {
66+
long numerator = 1;
67+
long denominator = 1;
68+
for (int i = 0; i < b; i++) {
69+
numerator = (numerator * (a - i)) % mod;
70+
denominator = (denominator * (i + 1)) % mod;
71+
}
72+
// Calculate the modular inverse of denominator
73+
long denominatorInverse = power(denominator, mod - 2);
74+
return (int) ((numerator * denominatorInverse) % mod);
75+
}
76+
77+
// Function to calculate power
78+
private long power(long x, int y) {
79+
long result = 1;
80+
while (y > 0) {
81+
if (y % 2 == 1) {
82+
result = (result * x) % mod;
83+
}
84+
y = y >> 1;
85+
x = (x * x) % mod;
86+
}
87+
return result;
88+
}
89+
}
90+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3180\. Maximum Total Reward Using Operations I
5+
6+
Medium
7+
8+
You are given an integer array `rewardValues` of length `n`, representing the values of rewards.
9+
10+
Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times:
11+
12+
* Choose an **unmarked** index `i` from the range `[0, n - 1]`.
13+
* 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`.
14+
15+
Return an integer denoting the **maximum** _total reward_ you can collect by performing the operations optimally.
16+
17+
**Example 1:**
18+
19+
**Input:** rewardValues = [1,1,3,3]
20+
21+
**Output:** 4
22+
23+
**Explanation:**
24+
25+
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.
26+
27+
**Example 2:**
28+
29+
**Input:** rewardValues = [1,6,4,3,2]
30+
31+
**Output:** 11
32+
33+
**Explanation:**
34+
35+
Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.
36+
37+
**Constraints:**
38+
39+
* `1 <= rewardValues.length <= 2000`
40+
* `1 <= rewardValues[i] <= 2000`
41+
42+
## Solution
43+
44+
```java
45+
@SuppressWarnings("java:S135")
46+
public class Solution {
47+
private int[] sortedSet(int[] values) {
48+
int max = 0;
49+
for (int x : values) {
50+
if (x > max) {
51+
max = x;
52+
}
53+
}
54+
boolean[] set = new boolean[max + 1];
55+
int n = 0;
56+
for (int x : values) {
57+
if (!set[x]) {
58+
set[x] = true;
59+
n++;
60+
}
61+
}
62+
int[] result = new int[n];
63+
for (int x = max; x > 0; x--) {
64+
if (set[x]) {
65+
result[--n] = x;
66+
}
67+
}
68+
return result;
69+
}
70+
71+
public int maxTotalReward(int[] rewardValues) {
72+
rewardValues = sortedSet(rewardValues);
73+
int n = rewardValues.length;
74+
int max = rewardValues[n - 1];
75+
boolean[] isSumPossible = new boolean[max];
76+
isSumPossible[0] = true;
77+
int maxSum = 0;
78+
int last = 1;
79+
for (int sum = rewardValues[0]; sum < max; sum++) {
80+
while (last < n && rewardValues[last] <= sum) {
81+
last++;
82+
}
83+
int s2 = sum / 2;
84+
for (int i = last - 1; i >= 0; i--) {
85+
int x = rewardValues[i];
86+
if (x <= s2) {
87+
break;
88+
}
89+
if (isSumPossible[sum - x]) {
90+
isSumPossible[sum] = true;
91+
maxSum = sum;
92+
break;
93+
}
94+
}
95+
}
96+
return maxSum + max;
97+
}
98+
}
99+
```
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3181\. Maximum Total Reward Using Operations II
5+
6+
Hard
7+
8+
You are given an integer array `rewardValues` of length `n`, representing the values of rewards.
9+
10+
Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times:
11+
12+
* Choose an **unmarked** index `i` from the range `[0, n - 1]`.
13+
* 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`.
14+
15+
Return an integer denoting the **maximum** _total reward_ you can collect by performing the operations optimally.
16+
17+
**Example 1:**
18+
19+
**Input:** rewardValues = [1,1,3,3]
20+
21+
**Output:** 4
22+
23+
**Explanation:**
24+
25+
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.
26+
27+
**Example 2:**
28+
29+
**Input:** rewardValues = [1,6,4,3,2]
30+
31+
**Output:** 11
32+
33+
**Explanation:**
34+
35+
Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= rewardValues.length <= 5 * 10<sup>4</sup></code>
40+
* <code>1 <= rewardValues[i] <= 5 * 10<sup>4</sup></code>
41+
42+
## Solution
43+
44+
```java
45+
public class Solution {
46+
public int maxTotalReward(int[] rewardValues) {
47+
int max = rewardValues[0];
48+
int n = 0;
49+
for (int i = 1; i < rewardValues.length; i++) {
50+
max = Math.max(max, rewardValues[i]);
51+
}
52+
boolean[] vis = new boolean[max + 1];
53+
for (int i : rewardValues) {
54+
if (!vis[i]) {
55+
n++;
56+
vis[i] = true;
57+
}
58+
}
59+
int[] rew = new int[n];
60+
int j = 0;
61+
for (int i = 0; i <= max; i++) {
62+
if (vis[i]) {
63+
rew[j++] = i;
64+
}
65+
}
66+
return rew[n - 1] + getAns(rew, n - 1, rew[n - 1] - 1);
67+
}
68+
69+
private int getAns(int[] rewards, int i, int validLimit) {
70+
int res = 0;
71+
int j = nextElemWithinLimits(rewards, i - 1, validLimit);
72+
for (; j >= 0; j--) {
73+
if (res >= rewards[j] + Math.min(validLimit - rewards[j], rewards[j] - 1)) {
74+
break;
75+
}
76+
res =
77+
Math.max(
78+
res,
79+
rewards[j]
80+
+ getAns(
81+
rewards,
82+
j,
83+
Math.min(validLimit - rewards[j], rewards[j] - 1)));
84+
}
85+
return res;
86+
}
87+
88+
private int nextElemWithinLimits(int[] rewards, int h, int k) {
89+
int l = 0;
90+
int resInd = -1;
91+
while (l <= h) {
92+
int m = (l + h) / 2;
93+
if (rewards[m] <= k) {
94+
resInd = m;
95+
l = m + 1;
96+
} else {
97+
h = m - 1;
98+
}
99+
}
100+
return resInd;
101+
}
102+
}
103+
```

0 commit comments

Comments
 (0)