Skip to content

Commit 0033019

Browse files
committed
✨feat: add 2656
1 parent 2c4cf20 commit 0033019

File tree

3 files changed

+266
-0
lines changed

3 files changed

+266
-0
lines changed

Index/数学.md

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
| [1775. 通过最少操作次数使数组的和相等](https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/1775.%20%E9%80%9A%E8%BF%87%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%92%8C%E7%9B%B8%E7%AD%89%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
8585
| [1780. 判断一个数字是否可以表示成三的幂的和](https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/) | [LeetCode 题解链接](https://acoier.com/2022/12/12/1780.%20%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%95%B0%E5%AD%97%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E8%A1%A8%E7%A4%BA%E6%88%90%E4%B8%89%E7%9A%84%E5%B9%82%E7%9A%84%E5%92%8C%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 |
8686
| [1802. 有界数组中指定下标处的最大值](https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-value-at-a-given-index-in-a-bounded-array/solutions/2363016/gong-shui-san-xie-chang-gui-zong-he-ti-b-ohvx/) | 中等 | 🤩🤩🤩🤩 |
87+
| [2656. K 个元素的最大和](https://leetcode.cn/problems/maximum-sum-with-exactly-k-elements/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-sum-with-exactly-k-elements/solutions/2527384/gong-shui-san-xie-deng-chai-shu-lie-qiu-b2g88/) | 简单 | 🤩🤩🤩 |
8788
| [剑指 Offer 44. 数字序列中某一位的数字](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/solution/by-ac_oier-wgr8/) | 中等 | 🤩🤩🤩🤩 |
8889
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
8990
| [面试题 17.19. 消失的两个数字](https://leetcode.cn/problems/missing-two-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/missing-two-lcci/solution/by-ac_oier-pgeh/) | 困难 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[2656. K 个元素的最大和](https://leetcode.cn/problems/maximum-sum-with-exactly-k-elements/solutions/2527384/gong-shui-san-xie-deng-chai-shu-lie-qiu-b2g88/)** ,难度为 **简单**
4+
5+
Tag : 「数学」
6+
7+
8+
9+
给你一个下标从 `0` 开始的整数数组 `nums` 和一个整数 `k`
10+
11+
你需要执行以下操作恰好 `k` 次,最大化你的得分:
12+
13+
1.`nums` 中选择一个元素 `m`
14+
2. 将选中的元素 `m` 从数组中删除。
15+
3. 将新元素 `m + 1` 添加到数组中。
16+
4. 你的得分增加 `m`
17+
18+
请你返回执行以上操作恰好 `k` 次后的最大得分。
19+
20+
示例 1:
21+
```
22+
输入:nums = [1,2,3,4,5], k = 3
23+
24+
输出:18
25+
26+
解释:我们需要从 nums 中恰好选择 3 个元素并最大化得分。
27+
第一次选择 5 。和为 5 ,nums = [1,2,3,4,6] 。
28+
第二次选择 6 。和为 6 ,nums = [1,2,3,4,7] 。
29+
第三次选择 7 。和为 5 + 6 + 7 = 18 ,nums = [1,2,3,4,8] 。
30+
所以我们返回 18 。
31+
18 是可以得到的最大答案。
32+
```
33+
示例 2:
34+
```
35+
输入:nums = [5,5,5], k = 2
36+
37+
输出:11
38+
39+
解释:我们需要从 nums 中恰好选择 2 个元素并最大化得分。
40+
第一次选择 5 。和为 5 ,nums = [5,5,6] 。
41+
第二次选择 6 。和为 6 ,nums = [5,5,7] 。
42+
所以我们返回 11 。
43+
11 是可以得到的最大答案。
44+
```
45+
46+
提示:
47+
* $1 <= nums.length <= 100$
48+
* $1 <= nums[i] <= 100$
49+
* $1 <= k <= 100$
50+
51+
---
52+
53+
### 数学
54+
55+
为了使得分最高,每次应从 `nums` 中选最大值,选完后重放仍为最大值。
56+
57+
假设原始 `nums` 中的最大值为 `max`,那么问题转换为「等差数列」求和:首项为 `max`,末项为 `max + k - 1`,项数为 $k$,公差为 $1$。
58+
59+
Java 代码:
60+
```Java
61+
class Solution {
62+
public int maximizeSum(int[] nums, int k) {
63+
int max = 0;
64+
for (int x : nums) max = Math.max(max, x);
65+
return k * (max + max + k - 1) / 2;
66+
}
67+
}
68+
```
69+
C++ 代码:
70+
```C++
71+
class Solution {
72+
public:
73+
int maximizeSum(vector<int>& nums, int k) {
74+
int maxv = 0;
75+
for (auto x : nums) maxv = max(maxv, x);
76+
return k * (maxv + maxv + k - 1) / 2;
77+
}
78+
};
79+
```
80+
Python 代码:
81+
```Python
82+
class Solution:
83+
def maximizeSum(self, nums: List[int], k: int) -> int:
84+
return k * (2 * max(nums) + k - 1) // 2
85+
```
86+
TypeScript 代码:
87+
```TypeScript
88+
function maximizeSum(nums: number[], k: number): number {
89+
let max = 0;
90+
for (const x of nums) max = Math.max(max, x);
91+
return k * (max + max + k - 1) / 2;
92+
};
93+
```
94+
* 时间复杂度:$O(n)$
95+
* 空间复杂度:$O(1)$
96+
97+
---
98+
99+
### 最后
100+
101+
这是我们「刷穿 LeetCode」系列文章的第 `No.2656` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
102+
103+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
104+
105+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
106+
107+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
108+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[2698. 求一个整数的惩罚数]()** ,难度为 **简单**
4+
5+
Tag : 「双指针」
6+
7+
8+
9+
给你一个下标从 $0$ 开始的整数数组 `nums` 和一个整数 `threshold`
10+
11+
请你从 `nums` 的子数组中找出以下标 `l` 开头、下标 `r` 结尾 ($0 <= l <= r < nums.length$) 且满足以下条件的 最长子数组 :
12+
13+
* `nums[l] % 2 == 0`
14+
* 对于范围 $[l, r - 1]$ 内的所有下标 `i``nums[i] % 2 != nums[i + 1] % 2`
15+
* 对于范围 $[l, r]$ 内的所有下标 `i``nums[i] <= threshold`
16+
17+
以整数形式返回满足题目要求的最长子数组的长度。
18+
19+
注意:子数组 是数组中的一个连续非空元素序列。
20+
21+
示例 1:
22+
```
23+
输入:nums = [3,2,5,4], threshold = 5
24+
25+
输出:3
26+
27+
解释:在这个示例中,我们选择从 l = 1 开始、到 r = 3 结束的子数组 => [2,5,4] ,满足上述条件。
28+
因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。
29+
```
30+
示例 2:
31+
```
32+
输入:nums = [1,2], threshold = 2
33+
34+
输出:1
35+
36+
解释:
37+
在这个示例中,我们选择从 l = 1 开始、到 r = 1 结束的子数组 => [2] 。
38+
该子数组满足上述全部条件。可以证明 1 是满足题目要求的最大长度。
39+
```
40+
示例 3:
41+
```
42+
输入:nums = [2,3,4,5], threshold = 4
43+
44+
输出:3
45+
46+
解释:
47+
在这个示例中,我们选择从 l = 0 开始、到 r = 2 结束的子数组 => [2,3,4] 。
48+
该子数组满足上述全部条件。
49+
因此,答案就是这个子数组的长度 3 。可以证明 3 是满足题目要求的最大长度。
50+
```
51+
52+
提示:
53+
* $1 <= nums.length <= 100$
54+
* $1 <= nums[i] <= 100$
55+
* $1 <= threshold <= 100$
56+
57+
---
58+
59+
### 双指针
60+
61+
整体题意:求 `nums` 中的最长的子数组 $[l, r]$,对于任意 $nums[i]$ 不超过 `threshold`,且从 $nums[l]$ 开始按照「先偶后奇」顺序交替。
62+
63+
容易想到「双指针」做法:
64+
65+
* 变量 `i` 作为当前子数组左端点,从前往后扫描 `nums`,首先确保 `i` 的合法性(跳过不满足 `nums[i] % 2 = 0``nums[i] <= threshold` 的位置);随后在固定左端点 `i` 前提下,找最远的右端点 `j`(值不超过 `threshold`,且奇偶性与前值交替)
66+
* 得到当前连续段长度,更新 `ans`,从当前结束位置开始,重复上述过程,直到处理完 `nums`
67+
68+
Java 代码
69+
70+
```Java
71+
class Solution {
72+
public int longestAlternatingSubarray(int[] nums, int threshold) {
73+
int n = nums.length, ans = 0, i = 0;
74+
while (i < n) {
75+
if ((nums[i] % 2 != 0 || nums[i] > threshold) && ++i >= 0) continue;
76+
int j = i + 1, cur = nums[i] % 2;
77+
while (j < n) {
78+
if (nums[j] > threshold || nums[j] % 2 == cur) break;
79+
cur = nums[j] % 2; j++;
80+
}
81+
ans = Math.max(ans, j - i);
82+
i = j;
83+
}
84+
return ans;
85+
}
86+
}
87+
```
88+
C++ 代码
89+
```C++
90+
class Solution {
91+
public:
92+
int longestAlternatingSubarray(vector<int>& nums, int threshold) {
93+
int n = nums.size(), ans = 0, i = 0;
94+
while (i < n) {
95+
if ((nums[i] % 2 != 0 || nums[i] > threshold) && ++i >= 0) continue;
96+
int j = i + 1, cur = nums[i] % 2;
97+
while (j < n) {
98+
if (nums[j] > threshold || nums[j] % 2 == cur) break;
99+
cur = nums[j] % 2; j++;
100+
}
101+
ans = max(ans, j - i);
102+
i = j;
103+
}
104+
return ans;
105+
}
106+
};
107+
108+
```
109+
Python 代码
110+
```Python
111+
class Solution:
112+
def longestAlternatingSubarray(self, nums: List[int], threshold: int) -> int:
113+
n, ans, i = len(nums), 0, 0
114+
while i < n:
115+
if nums[i] % 2 != 0 or nums[i] > threshold:
116+
i += 1
117+
continue
118+
j, cur = i + 1, nums[i] % 2
119+
while j < n:
120+
if nums[j] > threshold or nums[j] % 2 == cur: break
121+
cur, j = nums[j] % 2, j + 1
122+
ans = max(ans, j - i)
123+
i = j
124+
return ans
125+
```
126+
TypeScript 代码
127+
```TypeScript
128+
function longestAlternatingSubarray(nums: number[], threshold: number): number {
129+
let n = nums.length, ans = 0, i = 0
130+
while (i < n) {
131+
if ((nums[i] % 2 != 0 || nums[i] > threshold) && ++i >= 0) continue;
132+
let j = i + 1, cur = nums[i] % 2;
133+
while (j < n) {
134+
if (nums[j] > threshold || nums[j] % 2 == cur) break;
135+
cur = nums[j] % 2; j++;
136+
}
137+
ans = Math.max(ans, j - i);
138+
i = j;
139+
}
140+
return ans;
141+
};
142+
```
143+
* 时间复杂度:$O(n)$
144+
* 空间复杂度:$O(1)$
145+
146+
---
147+
148+
### 最后
149+
150+
这是我们「刷穿 LeetCode」系列文章的第 `No.2760` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
151+
152+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
153+
154+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
155+
156+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
157+

0 commit comments

Comments
 (0)