Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 92526c4

Browse files
Merge pull request SharingSource#479 from SharingSource/ac_oier
✨feat: Add 396
2 parents 8a75553 + 8267935 commit 92526c4

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

‎Index/前缀和.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| [304. 二维区域和检索 - 矩阵不可变](https://leetcode-cn.com/problems/range-sum-query-2d-immutable/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-query-2d-immutable/solution/xia-ci-ru-he-zai-30-miao-nei-zuo-chu-lai-ptlo/) | 中等 | 🤩🤩🤩🤩🤩 |
55
| [303. 区域和检索 - 数组不可变](https://leetcode-cn.com/problems/range-sum-query-immutable/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-query-immutable/solution/sha-shi-qian-zhui-he-ya-tu-jie-qian-zhui-0rla/) | 简单 | 🤩🤩🤩🤩🤩 |
66
| [363. 矩形区域不超过 K 的最大数值和](https://leetcode-cn.com/problems/max-sum-of-rectangle-no-larger-than-k/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-sum-of-rectangle-no-larger-than-k/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-dh8s/) | 困难 | 🤩🤩🤩 |
7+
| [396. 旋转函数](https://leetcode-cn.com/problems/rotate-function/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-function/solution/by-ac_oier-sxbi/) | 中等 | 🤩🤩🤩🤩🤩 |
78
| [437. 路径总和 III](https://leetcode-cn.com/problems/path-sum-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-sum-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-q-usa7/) | 中等 | 🤩🤩🤩🤩 |
89
| [523. 连续的子数组和](https://leetcode-cn.com/problems/continuous-subarray-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/continuous-subarray-sum/solution/gong-shui-san-xie-tuo-zhan-wei-qiu-fang-1juse/) | 中等 | 🤩🤩🤩🤩 |
910
| [525. 连续数组](https://leetcode-cn.com/problems/contiguous-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/contiguous-array/solution/gong-shui-san-xie-qian-zhui-he-ha-xi-bia-q400/) | 中等 | 🤩🤩🤩🤩 |

‎Index/滑动窗口.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| [187. 重复的DNA序列](https://leetcode-cn.com/problems/repeated-dna-sequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/repeated-dna-sequences/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-30pg/) | 中等 | 🤩🤩🤩🤩 |
66
| [219. 存在重复元素 II](https://leetcode-cn.com/problems/contains-duplicate-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/contains-duplicate-ii/solution/gong-shui-san-xie-hua-dong-chuang-kou-yu-q02i/) | 简单 | 🤩🤩🤩🤩 |
77
| [220. 存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/contains-duplicate-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-dlnv/) | 中等 | 🤩🤩🤩 |
8+
| [396. 旋转函数](https://leetcode-cn.com/problems/rotate-function/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/rotate-function/solution/by-ac_oier-sxbi/) | 中等 | 🤩🤩🤩🤩🤩 |
89
| [424. 替换后的最长重复字符](https://leetcode-cn.com/problems/longest-repeating-character-replacement/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-repeating-character-replacement/solution/ping-ping-wu-qi-shuang-zhi-zhen-da-bai-h-fgif/) | 中等 | 🤩🤩🤩🤩 |
910
| [438. 找到字符串中所有字母异位词](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-all-anagrams-in-a-string/solution/gong-shui-san-xie-shuang-zhi-zhen-shi-xi-t5hc/) | 中等 | 🤩🤩🤩🤩 |
1011
| [480. 滑动窗口中位数](https://leetcode-cn.com/problems/sliding-window-median/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sliding-window-median/solution/xiang-jie-po-su-jie-fa-you-xian-dui-lie-mo397/) | 困难 | 🤩🤩🤩🤩 |
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[396. 旋转函数](https://leetcode-cn.com/problems/rotate-function/solution/by-ac_oier-sxbi/)** ,难度为 **中等**
4+
5+
Tag : 「前缀和」、「滑动窗口」
6+
7+
8+
9+
给定一个长度为 $n$ 的整数数组 $nums$ 。
10+
11+
假设 $arr_k$ 是数组 $nums$ 顺时针旋转 $k$ 个位置后的数组,我们定义 $nums$ 的 旋转函数 `F` 为:
12+
13+
* `F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1]`
14+
15+
返回 `F(0), F(1), ..., F(n-1)`中的最大值 。
16+
17+
生成的测试用例让答案符合 32ドル$ 位 整数。
18+
19+
示例 1:
20+
```
21+
输入: nums = [4,3,2,6]
22+
23+
输出: 26
24+
25+
解释:
26+
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
27+
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
28+
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
29+
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
30+
所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。
31+
```
32+
示例 2:
33+
```
34+
输入: nums = [100]
35+
36+
输出: 0
37+
```
38+
39+
提示:
40+
* $n == nums.length$
41+
* 1ドル <= n <= 10^5$
42+
* $-100 <= nums[i] <= 100$
43+
44+
---
45+
46+
### 前缀和 + 滑动窗口
47+
48+
为了方便,我们将 $nums$ 的长度记为 $n$。
49+
50+
题目要对「旋转数组」做逻辑,容易想到将 $nums$ 进行复制拼接,得到长度为 2ドル * n$ 的新数组,在新数组上任意一个长度为 $n$ 的滑动窗口都对应了一个旋转数组。
51+
52+
然后考虑在窗口的滑动过程中,计算结果会如何变化,假设当前我们处理到下标为 $[i, i + n - 1]$ 的滑动窗口,根据题意,当前结果为:
53+
54+
$$
55+
cur = nums[i] * 0 + nums[i + 1] * 1 + ... + nums[i + n - 1] * (n - 1)
56+
$$
57+
58+
当窗口往后移动一位,也就是窗口的右端点来到 $i + n$ 的位置,左端点来到 $i + 1$ 的位置。
59+
60+
我们需要增加「新右端点」的值,即增加 $nums[i + n] * (n - 1),ドル同时减去「旧左端点」的值,即减少 $nums[i] * 0$(固定为 0ドル$),然后更新新旧窗口的公共部分 $[i + 1, i + n - 1]$。
61+
62+
不难发现,随着窗口的逐步右移,每一位公共部分的权值系数都会进行减一。
63+
64+
$$
65+
nums[i + 1] * 1 + nums[i + 2] * 2 + ... + nums[i + n - 1] * (n - 1)
66+
$$
67+
68+
变为
69+
70+
$$
71+
nums[i + 1] * 0 + nums[i + 2] * 1 + ... + nums[i + n - 1] * (n - 2)
72+
$$
73+
74+
因此,公共部分的差值为 $\sum_{idx = i + 1}^{i + n - 1}nums[idx],ドル这引导我们可以使用前缀和进行优化。
75+
76+
至此,我们从旧窗口到新窗口的过渡,都是 $O(1),ドル整体复杂度为 $O(n)$。
77+
78+
> 实现上,我们并不需要真正对 $nums$ 进行复制拼接,而只需要在计算前缀和数组 $sum$ 进行简单的下标处理即可。
79+
80+
代码:
81+
```Java
82+
class Solution {
83+
public int maxRotateFunction(int[] nums) {
84+
int n = nums.length;
85+
int[] sum = new int[n * 2 + 10];
86+
for (int i = 1; i <= 2 * n; i++) sum[i] = sum[i - 1] + nums[(i - 1) % n];
87+
int ans = 0;
88+
for (int i = 1; i <= n; i++) ans += nums[i - 1] * (i - 1);
89+
for (int i = n + 1, cur = ans; i < 2 * n; i++) {
90+
cur += nums[(i - 1) % n] * (n - 1);
91+
cur -= sum[i - 1] - sum[i - n];
92+
if (cur > ans) ans = cur;
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
* 时间复杂度:$O(n)$
99+
* 空间复杂度:$O(n)$
100+
101+
---
102+
103+
### 最后
104+
105+
这是我们「刷穿 LeetCode」系列文章的第 `No.396` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
106+
107+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
108+
109+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
110+
111+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
112+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /