日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)

發布時間:2024/2/28 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/

題解

經典的 暴力遞歸 -> 傻緩存 -> DP,重要思路來源于之前做過的 leetcode 309. Best Time to Buy and Sell Stock with Cooldown | 309. 最佳買賣股票時機含冷凍期(動態規劃)

當年看答案也摸不清門路的 dp,現在應不是玄學了,哈哈。

將「買入」和「賣出」分開進行考慮:「買入」為負收益,而「賣出」為正收益。在初入股市時,你只有「買入」的權利,只能獲得負收益。而當你「買入」之后,你就有了「賣出」的權利,可以獲得正收益。顯然,我們需要盡可能地降低負收益而提高正收益,因此我們的目標總是將收益值最大化。因此,我們可以使用動態規劃的方法,維護在股市中每一天結束后可以獲得的「累計最大收益」,并以此進行狀態轉移,得到最終的答案。

沒有草稿,直接上代碼:

class Solution {public int maxProfit(int[] prices, int fee) {// Approach 1: Recursive, Brute Force // return process1(prices, 0, false,fee);// Approach 2: Recursion with Memoization // int[][] dp = new int[2][prices.length + 1]; // Arrays.fill(dp[0], Integer.MIN_VALUE); // Arrays.fill(dp[1], Integer.MIN_VALUE); // dp[0][prices.length] = 0; // dp[1][prices.length] = 0; // return process2(prices, 0, 0, fee, dp);// Approach 3: Dynamic Programmingint[][] dp = new int[2][prices.length + 1];Arrays.fill(dp[0], Integer.MIN_VALUE);Arrays.fill(dp[1], Integer.MIN_VALUE);dp[0][prices.length] = 0;dp[1][prices.length] = 0;for (int i = prices.length - 1; i >= 0; i--) {for (int pending = 0; pending <= 1; pending++) {int p1 = Integer.MIN_VALUE;int p2 = Integer.MIN_VALUE;int p3 = Integer.MIN_VALUE;int p4 = Integer.MIN_VALUE;if (pending == 1) {p1 = dp[1][i + 1]; // 不賣p2 = dp[0][i + 1] + prices[i] - fee; // 賣} else {p3 = dp[1][i + 1] - prices[i]; // 買p4 = dp[0][i + 1]; // 不買}dp[pending][i] = Math.max(Math.max(p1, p2), Math.max(p3, p4));}}return dp[0][0];}// 從i位置開始做決策,獲得的最大收益(pending表示當前是否持有股票) // public int process1(int[] prices, int i, boolean pending, int fee) { // if (i == prices.length) return 0; // // int p1 = Integer.MIN_VALUE; // int p2 = Integer.MIN_VALUE; // int p3 = Integer.MIN_VALUE; // int p4 = Integer.MIN_VALUE; // if (pending) {// 持有股票 // p1 = process1(prices, i + 1, true, fee); // 不賣 // p2 = process1(prices, i + 1, false, fee) + prices[i] - fee; // 賣 // } else { // 未持有股票 // p3 = process1(prices, i + 1, true, fee) - prices[i]; // 買 // p4 = process1(prices, i + 1, false, fee); // 不買 // } // return Math.max(Math.max(p1, p2), Math.max(p3, p4)); // }// public int process2(int[] prices, int i, int pending, int fee, int[][] dp) { // if (dp[pending][i] != Integer.MIN_VALUE) return dp[pending][i]; // // int p1 = Integer.MIN_VALUE; // int p2 = Integer.MIN_VALUE; // int p3 = Integer.MIN_VALUE; // int p4 = Integer.MIN_VALUE; // if (pending == 1) {// 持有股票 // p1 = process2(prices, i + 1, 1, fee, dp); // 不賣 // p2 = process2(prices, i + 1, 0, fee, dp) + prices[i] - fee; // 賣 // } else { // 未持有股票 // p3 = process2(prices, i + 1, 1, fee, dp) - prices[i]; // 買 // p4 = process2(prices, i + 1, 0, fee, dp); // 不買 // } // // int result = Math.max(Math.max(p1, p2), Math.max(p3, p4)); // dp[pending][i] = result; // return result; // } }

總結

以上是生活随笔為你收集整理的leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。