leetcode 188. 買賣股票的最佳時機 IV(dp)

給定一個整數數組 prices ,它的第 i 個元素 prices[i] 是一支給定的股票在第 i 天的價格。

設計一個算法來計算你所能獲取的最大利潤。你最多可以完成 k 筆交易。

注意:你不能同時參與多筆交易(你必須在再次購買前出售掉之前的股票)。

示例 1:

輸入:k = 2, prices = [2,4,1]
輸出:2
解釋:在第 1 天 (股票價格 = 2) 的時候買入,在第 2 天 (股票價格 = 4) 的時候賣出,這筆交易所能獲得利潤 = 4-2 = 2 。

代碼

class Solution {public int maxProfit(int k, int[] prices) {if(prices.length==0) return  0;int n=prices.length,res=0;int[][] buy=new int[n][k+1];int[][] sell=new int[n][k+1];buy[0][0]=-prices[0];sell[0][0]=0;for(int i=1;i<=k;i++)//第0天時,不能完成任何交易{sell[0][i]=buy[0][i]=Integer.MIN_VALUE/2;}for(int j=1;j<prices.length;j++){buy[j][0]= Math.max(buy[j-1][0],sell[j-1][0]-prices[j]);for(int i=1;i<=k;i++){buy[j][i]= Math.max(buy[j-1][i],sell[j-1][i]-prices[j]);
//上一天買入或者今天買入取最大值sell[j][i]= Math.max(sell[j-1][i],buy[j-1][i-1]+prices[j]);
//今天賣出和保持不賣取最大值}}for(int i=1;i<=k;i++)res= Math.max(sell[n-1][i],res);return res;}
}

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

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

相關文章

kotlin編寫后臺_在Kotlin編寫圖書館的提示

kotlin編寫后臺by Adam Arold亞當阿羅德(Adam Arold) 在Kotlin編寫圖書館的提示 (Tips for Writing a Library in Kotlin) Writing a library in Kotlin seems easy but it can get tricky if you want to support multiple platforms. In this article we’ll explore ways f…

1.Swift教程翻譯系列——關于Swift

英文版PDF下載地址http://download.csdn.net/detail/tsingheng/7480427 我本來是做JAVA的。可是有一顆折騰的心&#xff0c;蘋果公布Swift以后就下載了蘋果的開發文檔。啃了幾天。朦朦朧朧的看了個幾乎相同&#xff0c;想靜下心看能不能整個翻譯出來。我英語一般般&#xff0c;…

核心技術java基礎_JAVA核心技術I---JAVA基礎知識(集合set)

一&#xff1a;集合了解(一)確定性&#xff0c;互異性&#xff0c;無序性確定性&#xff1a;對任意對象都能判定其是否屬于某一個集合互異性&#xff1a;集合內每個元素都是無差異的&#xff0c;注意是內容差異無序性&#xff1a;集合內的順序無關(二)集合接口HashSet&#xff…

nba數據庫統計_NBA板塊的價值-從統計學上講

nba數據庫統計The idea is not to block every shot. The idea is to make your opponent believe that you might block every shot. — Bill Russel這個想法不是要阻止每一個鏡頭。 這個想法是讓你的對手相信你可能會阻擋每一個投籃。 —比爾羅素 The block in basketball ha…

leetcode 330. 按要求補齊數組(貪心算法)

給定一個已排序的正整數數組 nums&#xff0c;和一個正整數 n 。從 [1, n] 區間內選取任意個數字補充到 nums 中&#xff0c;使得 [1, n] 區間內的任何數字都可以用 nums 中某幾個數字的和來表示。請輸出滿足上述要求的最少需要補充的數字個數。 示例 1: 輸入: nums [1,3], …

【煉數成金 NOSQL引航 三】 Redis使用場景與案例分析

驗證redis的主從復制&#xff0c;將實驗過程抓圖 復制配置文件 更改slave的端口 和相關master配置 主從復制測試 研究在OAuth中的“一次數”nonce有什么用途&#xff1f;怎樣使用&#xff1f;以此熟悉OAuth的全流程 nonce &#xff0c;一個隨機的混淆字符串&#xff0c;僅僅被…

SQL Server需要監控哪些計數器 ---指尖流淌

http://www.cnblogs.com/zhijianliutang/p/4174697.html轉載于:https://www.cnblogs.com/zengkefu/p/7044095.html

akka 簡介_Akka HTTP路由簡介

akka 簡介by Miguel Lopez由Miguel Lopez Akka HTTP路由簡介 (An introduction to Akka HTTP routing) Akka HTTP’s routing DSL might seem complicated at first, but once you get the hang of it you’ll see how powerful it is.Akka HTTP的路由DSL乍一看似乎很復雜&…

leetcode 1046. 最后一塊石頭的重量(堆)

有一堆石頭&#xff0c;每塊石頭的重量都是正整數。 每一回合&#xff0c;從中選出兩塊 最重的 石頭&#xff0c;然后將它們一起粉碎。假設石頭的重量分別為 x 和 y&#xff0c;且 x < y。那么粉碎的可能結果如下&#xff1a; 如果 x y&#xff0c;那么兩塊石頭都會被完全…

java2d方法_Java SunGraphics2D.fillRect方法代碼示例

import sun.java2d.SunGraphics2D; //導入方法依賴的package包/類/*** Return a non-accelerated BufferedImage of the requested type with the* indicated subimage of the original image located at 0,0 in the new image.* If a bgColor is supplied, composite the orig…

js建立excel表格_建立Excel足球聯賽表格-傳統vs動態數組方法

js建立excel表格介紹 (Introduction) I am going to show you the different ways you can build a football league table in Excel. Some of the methods are old school but others utilise Excel’s new capabilities.我將向您展示在Excel中建立足球聯賽表格的不同方法。 其…

postman+newman生成html報告

作為測試菜鳥,在學習postmannewman的使用過程中真的是頗費周折......沒辦法技術太菜,只能多學習. postman的下載安裝不多言說,下載地址:https://www.getpostman.com/downloads/ newman的安裝過程: 1.首先需要安裝node.js,可以去官網下載,地址:https://nodejs.org/en/#download …

java jdk1.9新特性_JDK1.9-新特性

1. Java平臺級模塊系統該特性使Java9最大的一個特性&#xff0c;Java提供該功能的主要的動機在于&#xff0c;減少內存的開銷&#xff0c;JVM啟動的時候&#xff0c;至少會有30~60MB的內存加載&#xff0c;主要原因是JVM需要加載rt.jar&#xff0c;不管其中的類是否被classload…

如何在10分鐘內讓Redux發揮作用

Hi everyone ??大家好?? For a while now I’ve been hearing my friends and colleagues complaining about how hard it was to get into Redux.一段時間以來&#xff0c;我一直在聽我的朋友和同事抱怨進入Redux有多困難。 I run a freeCodeCamp Study Group in the So…

兩個鏈接合并_如何找到兩個鏈接列表的合并點

兩個鏈接合并了解問題 (Understand the Problem) We are given two singly linked lists and we have to find the point at which they merge.我們給了兩個單鏈表&#xff0c;我們必須找到它們合并的點。 [SLL 1] 1--->3--->5 \ …

安裝veket到移動硬盤NTFS分區

如果你已經看過《手動安裝veket到硬盤》和《簡單的將veket安裝到U盤的方法》兩篇文章并且安裝成功的話&#xff0c;說明不適用本文的安裝環境&#xff0c;就不用往下看了。 《手動安裝veket到硬盤》一文采用grub4dos來引導硬盤上的veket&#xff0c;主要是用來在本機已安裝Wind…

簡書使用小技巧

1、不同字體  在 設置->基礎設置->富文本 模式下可以實現 2、添加圖片&#xff0c;讓文章更生動 3、添加代碼框 &#xff01;注意&#xff1a;設置為Markdown模式后&#xff0c;只對新創建的文章起作用。轉載于:https://www.cnblogs.com/HMJ-29/p/7049540.html

掩碼 項目編碼_每天進行20天的編碼項目

掩碼 項目編碼by Angela He通過何安佳 每天進行20天的編碼項目 (A coding project a day for 20 days) 我如何在20天內自學Web開發 (How I taught myself web development in 20 days) It was the first day of winter break for Stanford students. Back at home, I opened a…

java循環一年月份天數和_javawhile循環編寫輸入某年某月某日,判斷這一天是這一年的第幾…...

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓public class ZuoYe9 {public static void main(String[] args) {int days0; //存儲變量這一年的第幾天//1.輸入年&#xff0c;月&#xff0c;日Scanner inputnew Scanner(System.in);System.out.println("請輸入年份&#xf…

leetcode 605. 種花問題(貪心算法)

假設你有一個很長的花壇&#xff0c;一部分地塊種植了花&#xff0c;另一部分卻沒有。可是&#xff0c;花卉不能種植在相鄰的地塊上&#xff0c;它們會爭奪水源&#xff0c;兩者都會死去。 給定一個花壇&#xff08;表示為一個數組包含0和1&#xff0c;其中0表示沒種植花&…