2018-08-12 non-adjacent max two-number sum in loop array
迴圈陣列,兩數相加最大且兩數不能相鄰的情況,在O(n)情況下完成:
想了很久,海總想出了遞推公式:
譬如 a = [4,1,2,4] max = 6
class Solution:
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a = nums
l = len(a)
if l==0:
return 0
if l<3:
return max(a)
if l==3:
return a[1]
m1=0
m2=2
max1 = a[m1]+a[m2]
for i in range(l):
if i>=3:
nbs = [m1-1,m1,m1+1,m2-1,m2,m2+1]
max2 = 0
for j in nbs:
if j<=i and j>=0 and j!=i-1 and j!= i-1 and j!=i and j!=0:
max_temp = a[i] + a[j]
if max_temp > max2:
max2 = max_temp
m1_t = i
m2_t = j
if max2>max1:
max1 = max2
return max1
相關文章
- Array Sum up increment. 1526, 3229REM
- [ABC376E] Max × Sum 題解
- 【Leetcode】167. Two Sum II - Input array is sortedLeetCode
- laravel mysql聚合函式使用方法(count,sum,max,min,avg)LaravelMySql函式
- 秒殺 2Sum 3Sum 4Sum 演算法題演算法
- GCD SUMGC
- Sum Problem
- 集合sum
- 15+18、3Sum 4Sum
- Event loopOOP
- Array.from和 Array.of
- leetcode Sum系列LeetCode
- Sum of Left Leaves
- Path-sum
- Path Sum III
- Leetcode Path SumLeetCode
- B - Minimum Sum
- Range Minimum Sum
- Missing Subsequence Sum
- array
- PHP用foreach來表達array_walk/array_filter/array_map/array_reducePHPFilter
- javascript - event loopJavaScriptOOP
- node event loopOOP
- JavaScript Event LoopJavaScriptOOP
- event loop整理OOP
- Array()與Array.of()方法區別
- JS Array.reduce 實現 Array.map 和 Array.filterJSFilter
- 瀏覽器的event loop和node的event loop瀏覽器OOP
- array_filter ()、array_map ()、array_walk () 區別?容易記混淆!!!Filter
- HBO Max for Mac HBO Max影片客戶端Mac客戶端
- 瀏覽器event loop和node的event loop講解瀏覽器OOP
- Leetcode 39 Combination SumLeetCode
- md5sum
- 112-Path Sum
- Leetcode 1 two sumLeetCode
- 7.22 APPROX_SUMAPP
- the Sum of Cube hd 5053
- LeetCode | 1 Two SumLeetCode