Skip to content

Commit e55a6f4

Browse files
authored
Added task 2233.
1 parent fa2340b commit e55a6f4

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.10'
13841384
| 2236 |[Root Equals Sum of Children](src/main/java/g2201_2300/s2236_root_equals_sum_of_children/Solution.java)| Easy | Tree, Binary_Tree | 0 | 100.00
13851385
| 2235 |[Add Two Integers](src/main/java/g2201_2300/s2235_add_two_integers/Solution.java)| Easy | Math | 1 | 48.94
13861386
| 2234 |[Maximum Total Beauty of the Gardens](src/main/java/g2201_2300/s2234_maximum_total_beauty_of_the_gardens/Solution.java)| Hard | Array, Sorting, Greedy, Binary_Search, Two_Pointers | 63 | 73.03
1387+
| 2233 |[Maximum Product After K Increments](src/main/java/g2201_2300/s2233_maximum_product_after_k_increments/Solution.java)| Medium | Array, Greedy, Heap_Priority_Queue | 364 | 75.06
13871388
| 2232 |[Minimize Result by Adding Parentheses to Expression](src/main/java/g2201_2300/s2232_minimize_result_by_adding_parentheses_to_expression/Solution.java)| Medium | String, Enumeration | 1 | 99.60
13881389
| 2231 |[Largest Number After Digit Swaps by Parity](src/main/java/g2201_2300/s2231_largest_number_after_digit_swaps_by_parity/Solution.java)| Easy | Sorting, Heap_Priority_Queue | 1 | 98.32
13891390
| 2227 |[Encrypt and Decrypt Strings](src/main/java/g2201_2300/s2227_encrypt_and_decrypt_strings/Encrypter.java)| Hard | Array, String, Hash_Table, Design, Trie | 143 | 74.74
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2201_2300.s2233_maximum_product_after_k_increments;
2+
3+
// #Medium #Array #Greedy #Heap_Priority_Queue
4+
// #2022_06_14_Time_364_ms_(75.06%)_Space_52.3_MB_(95.74%)
5+
6+
import java.util.PriorityQueue;
7+
8+
public class Solution {
9+
public int maximumProduct(int[] nums, int k) {
10+
PriorityQueue<Integer> pq = new PriorityQueue<>();
11+
for (int num : nums) {
12+
pq.add(num);
13+
}
14+
while (k-- > 0) {
15+
pq.add(pq.poll() + 1);
16+
}
17+
long ans = 1;
18+
int mod = 1000000007;
19+
while (!pq.isEmpty()) {
20+
ans = (ans * pq.poll()) % mod;
21+
}
22+
return (int) ans;
23+
}
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2233\. Maximum Product After K Increments
2+
3+
Medium
4+
5+
You are given an array of non-negative integers `nums` and an integer `k`. In one operation, you may choose **any** element from `nums` and **increment** it by `1`.
6+
7+
Return _the **maximum** **product** of_ `nums` _after **at most**_ `k` _operations._ Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>. Note that you should maximize the product before taking the modulo.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [0,4], k = 5
12+
13+
**Output:** 20
14+
15+
**Explanation:** Increment the first number 5 times.
16+
17+
Now nums = [5, 4], with a product of 5 \* 4 = 20.
18+
19+
It can be shown that 20 is maximum product possible, so we return 20.
20+
21+
Note that there may be other ways to increment nums to have the maximum product.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [6,3,3,2], k = 2
26+
27+
**Output:** 216
28+
29+
**Explanation:** Increment the second number 1 time and increment the fourth number 1 time.
30+
31+
Now nums = [6, 4, 3, 3], with a product of 6 \* 4 \* 3 \* 3 = 216.
32+
33+
It can be shown that 216 is maximum product possible, so we return 216.
34+
35+
Note that there may be other ways to increment nums to have the maximum product.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length, k <= 10<sup>5</sup></code>
40+
* <code>0 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2201_2300.s2233_maximum_product_after_k_increments;
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 maximumProduct() {
11+
assertThat(new Solution().maximumProduct(new int[] {0, 4}, 5), equalTo(20));
12+
}
13+
14+
@Test
15+
void maximumProduct2() {
16+
assertThat(new Solution().maximumProduct(new int[] {6, 3, 3, 2}, 2), equalTo(216));
17+
}
18+
}

0 commit comments

Comments
 (0)