多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > 綜合技術(shù) > leetcode 198 House Robber

leetcode 198 House Robber

來源:程序員人生   發(fā)布時間:2015-06-04 08:30:22 閱讀次數(shù):3470次

今天看了1個華為西安研究院的1個女生代碼大神的總結(jié)很有感悟,下面這句話送給大家:

只有好的程序員才能寫出人類可以理解的代碼

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

the objective function is basically:

dp(i) = max(dp[i⑵] + num[i], dp[i⑴]),

this means the current max is the max of the position i⑵ plus the current num[i], or the max of the previous one i⑴ (cannot including num[i] with i⑴ position, otherwise it will trigger the alarm)

我的解決方案:
這里寫圖片描述

class Solution { public: int rob(vector<int>& nums) { if(nums.empty())return 0; int length = nums.size(); vector<int> dp(length,0); dp[0] = nums[0]; dp[1] = max(nums[0],nums[1]); for(int i =2; i< length; ++i) { dp[i] = max(dp[i-2]+nums[i],dp[i-1]); } return dp[length-1]; } };

c語言解決方案:

#define max(a, b) ((a)>(b)?(a):(b)) int rob(int num[], int n) { int a = 0; int b = 0; for (int i=0; i<n; i++) { if (i%2==0) { a = max(a+num[i], b); } else { b = max(a, b+num[i]); } } return max(a, b); }

python解決方案:

class Solution: # @param num, a list of integer # @return an integer def rob(self, num): # DP O(n) time, O(1) space # ik: max include house k # ek: max exclude house k, (Note: ek is also the maximum for house 1,...,k⑴) # i[k+1]: num[k] + ek #can't include house k # e[k+1]: max(ik, ek) # can either include house k or exclude house k i, e = 0, 0 for n in num: #from k⑴ to k i, e = n+e, max(i,e) return max(i,e)
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 欧美精品亚洲 | 国产女人成人精品视频 | 欧美a级在线 | 国产极品嫩模在线观看91精品 | 欧美成人综合在线 | 国产内地激情精品毛片在线一 | 波多野结衣视频免费看 | 亚洲国产精品日韩一线满 | 亚洲www在线观看 | 一区二区三区免费视频网站 | 久久久夜 | 在线h网站 | 泰国一级毛片aaa下面毛多 | 一级爱爱片一级毛片-一毛 一级爱一级做a性视频 | 亚洲精品色综合久久久 | 国产亚洲精品观看91在线 | 精品一区二区三区四区五区 | 亚洲国产欧美目韩成人综合 | аⅴ天堂 在线8 | 国产乱淫a∨片免费视频 | 一级特黄aa大片一又好看 | h网站免费看 | 午夜精品久久久久久久第一页 | 免费在线h视频 | 国产aaa女人十八毛片 | 亚洲水蜜桃久久综合网站 | www.欧美黄色| jlzzjlzzjlzz亚洲女 | 久草在线综合 | 一级做性色a爰片久久毛片免费 | 好好的曰www视频在线观看 | 欧美成人三级一区二区在线观看 | 欧美gv在线| 动漫美女口工h福利动画网站 | 在线观看精品国产福利片尤物 | 在线观看视频一区二区 | 欧美性受xxxx喷水性欧洲 | 无遮挡免费视频 | 91丨九色丨首页 | 秋霞理论一级在线观看手机版 | 成人自拍小视频 |