leetcode 213. 打家劫舍 II(dp)

你是一個專業的小偷,計劃偷竊沿街的房屋,每間房內都藏有一定的現金。這個地方所有的房屋都 圍成一圈 ,這意味著第一個房屋和最后一個房屋是緊挨著的。同時,相鄰的房屋裝有相互連通的防盜系統,如果兩間相鄰的房屋在同一晚上被小偷闖入,系統會自動報警 。

給定一個代表每個房屋存放金額的非負整數數組,計算你 在不觸動警報裝置的情況下 ,能夠偷竊到的最高金額。

示例 1:

輸入:nums = [2,3,2]
輸出:3
解釋:你不能先偷竊 1 號房屋(金額 = 2),然后偷竊 3 號房屋(金額 = 2), 因為他們是相鄰的。
示例 2:

輸入:nums = [1,2,3,1]
輸出:4
解釋:你可以先偷竊 1 號房屋(金額 = 1),然后偷竊 3 號房屋(金額 = 3)。
偷竊到的最高金額 = 1 + 3 = 4 。
示例 3:

輸入:nums = [0]
輸出:0

解題思路

因為房子是環形的,第一家和最后一家是連著的
因此我們可以分為3種情況

  1. 既不偷第一家,也不偷最后一家
  2. 只偷第一家
  3. 只偷最后一家

只偷第一家就等于忽略最后一個元素,只對前面n-1個元素動態規劃。只偷最后一家的情況同理。
而既不偷第一家,也不偷最后一家的情況,已經出現在前面的動態規劃的情況中了,所以不需要另外處理

代碼

class Solution {public int rob(int[] nums) {int n=nums.length;int[][] dp = new int[n][2];if(n==1) return nums[0];dp[1][0]=0;dp[1][1]=nums[1];for(int i=2;i<n;i++){dp[i][0]= Math.max(dp[i-1][0],dp[i-1][1]);dp[i][1]=dp[i-1][0]+nums[i];}int res=Math.max(dp[n-1][0],dp[n-1][1]);dp[n-2][0]=0;dp[n-2][1]=nums[n-2];for(int j=n-3;j>=0;j--){dp[j][0]= Math.max(dp[j+1][0],dp[j+1][1]);dp[j][1]=dp[j+1][0]+nums[j];}return Math.max(res, Math.max(dp[0][0],dp[0][1]));}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391414.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391414.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391414.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

HTTP緩存的深入介紹:Cache-Control和Vary

簡介-本文范圍 (Introduction - scope of the article) This series of articles deals with caching in the context of HTTP. When properly done, caching can increase the performance of your application by an order of magnitude. On the contrary, when overlooked o…

059——VUE中vue-router之路由嵌套在文章系統中的使用方法:

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>vue-router之路由嵌套在文章系統中的使用方法&#xff1a;</title><script src"vue.js"></script><script src"v…

web前端效率提升之瀏覽器與本地文件的映射-遁地龍卷風

1.chrome瀏覽器&#xff0c;機制是攔截url&#xff0c;      1.在瀏覽器Element中調節的css樣式可以直接同步到本地文件&#xff0c;反之亦然&#xff0c;瀏覽器會重新加載css&#xff0c;省去刷新   2.在source面板下對js的編輯可以同步到本地文件&#xff0c;反之亦然…

linux : 各個發行版中修改python27默認編碼為utf-8

該方法可解決robot報錯&#xff1a;ascii codec cant encode character u\xf1 in position 16: ordinal not in range(128) 在下面目錄中新增文件&#xff1a;sitecustomize.py 內容為 #codingutf-8 import sysreload(sys) sys.setdefaultencoding(utf8) 各個發行版放置位置&a…

歸因分析_歸因分析:如何衡量影響? (第2部分,共2部分)

歸因分析By Lisa Cohen, Ryan Bouchard, Jane Huang, Daniel Yehdego and Siddharth Kumar由 麗莎科恩 &#xff0c; 瑞安布沙爾 &#xff0c; 黃美珍 &#xff0c; 丹尼爾Yehdego 和 亞洲時報Siddharth庫馬爾 介紹 (Introduction) This is our second article in a series wh…

ubuntu恢復系統_Ubuntu恢復菜單:揭開Linux系統恢復神秘面紗

ubuntu恢復系統Don’t try to convince yourself otherwise: along with all the good stuff, you’re going to have bad days with Linux.否則&#xff0c;請不要試圖說服自己&#xff1a;與所有好的東西一起&#xff0c;您將在Linux上度過糟糕的日子。 You (or the users y…

linux與磁盤相關的內容

本節所講內容1.認識SAS-SATA-SSD-SCSI-IDE硬盤2.使用fdisk對磁盤進行操作&#xff0c;分區&#xff0c;格式化3.開機自動掛載分區4.使用parted操作大于等于4T硬盤5.擴展服務器swap內存空間 MBR(Master Boot Record)主引導記錄&#xff0c;也就是現有的硬盤分區模式。MBR分區的標…

leetcode 87. 擾亂字符串(dp)

使用下面描述的算法可以擾亂字符串 s 得到字符串 t &#xff1a; 如果字符串的長度為 1 &#xff0c;算法停止 如果字符串的長度 > 1 &#xff0c;執行下述步驟&#xff1a; 在一個隨機下標處將字符串分割成兩個非空的子字符串。即&#xff0c;如果已知字符串 s &#xff0c…

頁面布局

頁面布局兩大類&#xff1a;   主站&#xff1a; 1 <div classpg-header> 2 <div stylewidth:980px;margin:0 auto;> 3 內容自動居中 4 </div> 5 <div classpg-content></div> 6 <div classpg-footer></div&…

sonar:默認的掃描規則

https://blog.csdn.net/liumiaocn/article/details/83550309 https://note.youdao.com/ynoteshare1/index.html?id3c1e6a08a21ada4dfe0123281637e299&typenote https://blog.csdn.net/liumiaocn/article/details/83550309 文本版&#xff1a; soanr規則java版 …

多變量線性相關分析_如何測量多個變量之間的“非線性相關性”?

多變量線性相關分析現實世界中的數據科學 (Data Science in the Real World) This article aims to present two ways of calculating non linear correlation between any number of discrete variables. The objective for a data analysis project is twofold : on the one …

wp博客寫文章500錯誤_500多個博客文章教我如何撰寫出色的文章

wp博客寫文章500錯誤Ive written a lot of blog posts. Somewhere north of 500 to be exact. All of them are technical. 我寫了很多博客文章。 確切地說是在500以北的某個地方。 所有這些都是技術性的。 About two dozen of them are actually good. 實際上大約有兩打是不錯…

leetcode 220. 存在重復元素 III(排序)

給你一個整數數組 nums 和兩個整數 k 和 t 。請你判斷是否存在 兩個不同下標 i 和 j&#xff0c;使得 abs(nums[i] - nums[j]) < t &#xff0c;同時又滿足 abs(i - j) < k 。 如果存在則返回 true&#xff0c;不存在返回 false。 示例 1&#xff1a; 輸入&#xff1a…

ON DUPLICATE KEY UPDATE

INSERT INTO ON DUPLICATE KEY UPDATE 與 REPLACE INTO&#xff0c;兩個命令可以處理重復鍵值問題&#xff0c;在實際上它之間有什么區別呢&#xff1f;前提條件是這個表必須有一個唯一索引或主鍵。1、REPLACE發現重復的先刪除再插入&#xff0c;如果記錄有多個字段&#xff0c…

os.path 模塊

os.path.abspath(path) #返回絕對路徑os.path.basename(path) #返回文件名os.path.commonprefix(list) #返回list(多個路徑)中&#xff0c;所有path共有的最長的路徑。os.path.dirname(path) #返回文件路徑os.path.exists(path) #路徑存在則返回True,路徑損壞返回Falseos.path…

探索性數據分析(EDA):Python

什么是探索性數據分析(EDA)&#xff1f; (What is Exploratory Data Analysis(EDA)?) If we want to explain EDA in simple terms, it means trying to understand the given data much better, so that we can make some sense out of it.如果我們想用簡單的術語來解釋EDA&a…

微服務框架---搭建 go-micro環境

1.安裝micro 需要使用GO1.11以上版本 #linux 下 export GO111MODULEon export GOPROXYhttps://goproxy.io # windows下設置如下環境變量 setx GO111MODULE on setx GOPROXY https://goproxy.io # 使用如下指令安裝 go get -u -v github.com/micro/micro go get -u -v github.co…

angular dom_Angular 8 DOM查詢:ViewChild和ViewChildren示例

angular domThe ViewChild and ViewChildren decorators in Angular provide a way to access and manipulate DOM elements, directives and components. In this tutorial, well see an Angular 8 example of how to use the two decorators.Angular中的ViewChild和ViewChild…

浪潮之巔——IT產業的三大定律

http://www.cnblogs.com/ysocean/p/7641540.html轉載于:https://www.cnblogs.com/czlovezmt/p/8325772.html

DStream算子講解(一)

先把目錄列好&#xff0c;方便有條理的進行整理轉載于:https://www.cnblogs.com/leodaxin/p/7507600.html