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
相關文章
- Split Array Largest Sum
- LeetCode-Max Sum of Rectangle No Larger Than KLeetCode
- HDU3415 Max Sum of Max-K-sub-sequence (DP+單調佇列)佇列
- 【Leetcode】167. Two Sum II - Input array is sortedLeetCode
- laravel mysql聚合函式使用方法(count,sum,max,min,avg)LaravelMySql函式
- for loopOOP
- GCD SUMGC
- 秒殺 2Sum 3Sum 4Sum 演算法題演算法
- array new 與 array deletedelete
- PHP array_flip() array_merge() array+array的使用總結PHP
- Event LoopOOP
- leetcode15&16_3Sum&4SumLeetCode
- Array()與Array.of()方法區別
- JS Array.reduce 實現 Array.map 和 Array.filterJSFilter
- event loop整理OOP
- node event loopOOP
- javascript - event loopJavaScriptOOP
- JavaScript Event LoopJavaScriptOOP
- Ruby loop 方法OOP
- nested loop心得OOP
- PHP用foreach來表達array_walk/array_filter/array_map/array_reducePHPFilter
- 瀏覽器的event loop和node的event loop瀏覽器OOP
- array_filter ()、array_map ()、array_walk () 區別?容易記混淆!!!Filter
- SQL groupby sum 用法SQL
- Pairwise Sum and DivideAIIDE
- oracle 字串 聚合 sumOracle字串
- Array物件物件
- Array Repetition
- 瀏覽器event loop和node的event loop講解瀏覽器OOP
- FBI? MAX? INDEX FULL SCAN (MIN/MAX)?Index
- waring:in_array()[function.in-array]:wrongdatatypeforsecondargumentFunction
- 淺談event loopOOP
- Performance Without the Event LoopORMOOP
- 月度文章——Event LoopOOP
- JavaScript與Event LoopJavaScriptOOP
- Event Loop個人理解OOP
- Javascript之Event LoopJavaScriptOOP
- Daniel Powter: Free LoopOOP