Skip to content

Commit d21b96b

Browse files
committed
2202. K 次操作后最大化顶端元素
1 parent 157f044 commit d21b96b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
|1832|[判断句子是否为全字母句](https://leetcode.cn/problems/check-if-the-sentence-is-pangram/)|[JavaScript](./algorithms/check-if-the-sentence-is-pangram.js)|Easy|
225225
|1863|[找出所有子集的异或总和再求和](https://leetcode.cn/problems/sum-of-all-subset-xor-totals/)|[JavaScript](./algorithms/sum-of-all-subset-xor-totals.js)|Easy|
226226
|1945|[字符串转化后的各位数字之和](https://leetcode.cn/problems/sum-of-digits-of-string-after-convert/)|[JavaScript](./algorithms/sum-of-digits-of-string-after-convert.js)|Easy|
227+
|2202|[K 次操作后最大化顶端元素](https://leetcode.cn/problems/maximize-the-topmost-element-after-k-moves/)|[JavaScript](./algorithms/maximize-the-topmost-element-after-k-moves.js)|Medium|
227228
|2032|[至少在两个数组中出现的值](https://leetcode.cn/problems/two-out-of-three/)|[JavaScript](./algorithms/two-out-of-three.js)|Easy|
228229
|2042|[检查句子中的数字是否递增](https://leetcode.cn/problems/check-if-numbers-are-ascending-in-a-sentence/)|[JavaScript](./algorithms/check-if-numbers-are-ascending-in-a-sentence.js)|Easy|
229230
|2283|[判断一个数的数字计数是否等于数位的值](https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/)|[JavaScript](./algorithms/check-if-number-has-equal-digit-count-and-digit-value.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var maximumTop = function (nums, k) {
7+
// 分类讨论
8+
const n = nums.length;
9+
10+
if (n === 1) {
11+
if (k % 2 === 1) {
12+
return -1;
13+
}
14+
return nums[0];
15+
}
16+
if (k <= 1) {
17+
return nums[k];
18+
}
19+
if (k > n) {
20+
return Math.max(...nums);
21+
}
22+
if (k === n) {
23+
return Math.max(...nums.slice(0, n - 1));
24+
}
25+
return Math.max(...nums.slice(0, k - 1), nums[k]);
26+
};

0 commit comments

Comments
 (0)