2530. Maximal Score After Applying K Operations Medium
1/**
2 * [2530] Maximal Score After Applying K Operations
3 *
4 * You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0.
5 * In one operation:
6 * <ol>
7 * choose an index i such that 0 <= i < nums.length,
8 * increase your score by nums[i], and
9 * replace nums[i] with ceil(nums[i] / 3).
10 * </ol>
11 * Return the maximum possible score you can attain after applying exactly k operations.
12 * The ceiling function ceil(val) is the least integer greater than or equal to val.
13 *
14 * Example 1:
15 *
16 * Input: nums = [10,10,10,10,10], k = 5
17 * Output: 50
18 * Explanation: Apply the operation to each array element exactly once. The final score is 10 + 10 + 10 + 10 + 10 = 50.
19 *
20 * Example 2:
21 *
22 * Input: nums = [1,10,3,3,3], k = 3
23 * Output: 17
24 * Explanation: You can do the following operations:
25 * Operation 1: Select i = 1, so nums becomes [1,<u>4</u>,3,3,3]. Your score increases by 10.
26 * Operation 2: Select i = 1, so nums becomes [1,<u>2</u>,3,3,3]. Your score increases by 4.
27 * Operation 3: Select i = 2, so nums becomes [1,1,<u>1</u>,3,3]. Your score increases by 3.
28 * The final score is 10 + 4 + 3 = 17.
29 *
30 *
31 * Constraints:
32 *
33 * 1 <= nums.length, k <= 10^5
34 * 1 <= nums[i] <= 10^9
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/maximal-score-after-applying-k-operations/
40// discuss: https://leetcode.com/problems/maximal-score-after-applying-k-operations/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn max_kelements(nums: Vec<i32>, k: i32) -> i64 {
46
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_2530() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.