File tree 2 files changed +26
-0
lines changed
2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 72
72
| 349| [ 两个数组的交集] ( https://leetcode-cn.com/problems/intersection-of-two-arrays/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays.js ) | Easy|
73
73
| 350| [ 两个数组的交集 II] ( https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays-ii.js ) | Easy|
74
74
| 387| [ 字符串中的第一个唯一字符] ( https://leetcode-cn.com/problems/first-unique-character-in-a-string/ ) | [ JavaScript] ( ./algorithms/first-unique-character-in-a-string.js ) | Easy|
75
+ | 396| [ 旋转函数] ( https://leetcode.cn/problems/rotate-function/ ) | [ JavaScript] ( ./algorithms/rotate-function.js ) | Medium|
75
76
| 404| [ 左叶子之和] ( https://leetcode.cn/problems/sum-of-left-leaves/ ) | [ JavaScript] ( ./algorithms/sum-of-left-leaves.js ) | Easy|
76
77
| 414| [ 第三大的数] ( https://leetcode.cn/problems/third-maximum-number/ ) | [ JavaScript] ( ./algorithms/third-maximum-number.js ) | Easy|
77
78
| 442| [ 数组中重复的数据] ( https://leetcode.cn/problems/find-all-duplicates-in-an-array/ ) | [ JavaScript] ( ./algorithms/find-all-duplicates-in-an-array.js ) | Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ var maxRotateFunction = function ( nums ) {
6
+ // 参考: https://leetcode.cn/problems/rotate-function/solution/by-ac_oier-sxbi/
7
+
8
+ let n = nums . length ;
9
+ let sum = 0 ;
10
+ let f = 0 , ans = 0 ;
11
+
12
+ for ( let i = 0 ; i < n ; i ++ ) {
13
+ sum += nums [ i ] ; // 计算 sum
14
+ f += i * nums [ i ] ; // 计算 F(0)
15
+ }
16
+
17
+ ans = f ;
18
+
19
+ for ( let i = 1 ; i < n ; i ++ ) { // 迭代计算 F(i)
20
+ f = f + sum - n * nums [ n - i ] ; // F(i) = F(i-1) + sum - n * A(n-i)
21
+ ans = Math . max ( ans , f ) ;
22
+ }
23
+
24
+ return ans ;
25
+ } ;
You can’t perform that action at this time.
0 commit comments