File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 127
127
| 304| [ 二维区域和检索 - 矩阵不可变] ( https://leetcode.cn/problems/range-sum-query-2d-immutable/ ) | [ JavaScript] ( ./algorithms/range-sum-query-2d-immutable.js ) | Medium|
128
128
| 328| [ 奇偶链表] ( https://leetcode.cn/problems/odd-even-linked-list/ ) | [ JavaScript] ( ./algorithms/odd-even-linked-list.js ) | Medium|
129
129
| 332| [ 重新安排行程] ( https://leetcode.cn/problems/reconstruct-itinerary/ ) | [ JavaScript] ( ./algorithms/reconstruct-itinerary.js ) | Hard|
130
+ | 343| [ 整数拆分] ( https://leetcode.cn/problems/integer-break/ ) | [ JavaScript] ( ./algorithms/integer-break.js ) | Medium|
130
131
| 344| [ 反转字符串] ( https://leetcode-cn.com/problems/reverse-string/ ) | [ JavaScript] ( ./algorithms/reverse-string.js ) | Easy|
131
132
| 347| [ 前 K 个高频元素] ( https://leetcode.cn/problems/top-k-frequent-elements/ ) | [ JavaScript] ( ./algorithms/top-k-frequent-elements.js ) | Medium|
132
133
| 349| [ 两个数组的交集] ( https://leetcode-cn.com/problems/intersection-of-two-arrays/ ) | [ JavaScript] ( ./algorithms/intersection-of-two-arrays.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 343. 整数拆分
3
+ * @param {number } n
4
+ * @return {number }
5
+ */
6
+ var integerBreak = function ( n ) {
7
+ const dp = Array ( n + 1 ) . fill ( 0 ) ;
8
+ dp [ 2 ] = 1 ;
9
+
10
+ for ( let i = 3 ; i <= n ; i ++ ) {
11
+ for ( let j = 1 ; j <= i >> 1 ; j ++ ) {
12
+ dp [ i ] = Math . max ( dp [ i ] , ( i - j ) * j , dp [ i - j ] * j ) ;
13
+ }
14
+ }
15
+
16
+ return dp [ n ] ;
17
+ } ;
18
+
19
+ // 贪心
20
+ var integerBreak = function ( n ) {
21
+ if ( n === 2 ) return 1 ;
22
+ if ( n === 3 ) return 2 ;
23
+ if ( n === 4 ) return 4 ;
24
+
25
+ let a = 1 ;
26
+ while ( n > 4 ) {
27
+ n -= 3 ;
28
+ a *= 3 ;
29
+ }
30
+
31
+ return a * n ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments