当前位置: 首页 > news >正文

做家居网站设计贵阳网站建设王道下拉惠

做家居网站设计,贵阳网站建设王道下拉惠,网络公司有几家,棋牌小程序开发一个多少钱啊714. 买卖股票的最佳时机含手续费 原题链接:完成情况:解题思路:ExplanationSummary 参考代码:_714买卖股票的最佳时机含手续费 错误经验吸取 原题链接: 714. 买卖股票的最佳时机含手续费 https://leetcode.cn/probl…

714. 买卖股票的最佳时机含手续费

  • 原题链接:
  • 完成情况:
  • 解题思路:
      • Explanation
      • Summary
  • 参考代码:
    • _714买卖股票的最佳时机含手续费
  • 错误经验吸取

原题链接:

714. 买卖股票的最佳时机含手续费

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

完成情况:

在这里插入图片描述

解题思路:

Sure, let’s break down the code and explain it step by step.

The problem is to maximize the profit from buying and selling stocks given that there is a transaction fee each time you sell a stock. You can perform as many transactions as you like, but you must sell the stock before you can buy again.

Explanation

  1. Initialization:

    int n = prices.length;
    int [][] dp = new int[n][2];
    dp[0][0] = 0;
    dp[0][1] = -prices[0];
    
    • n is the length of the prices array.
    • dp is a 2D array where dp[i][0] represents the maximum profit at day i when you don’t have any stock, and dp[i][1] represents the maximum profit at day i when you have one stock.
    • On day 0, if you don’t own a stock, the profit is 0 (dp[0][0] = 0).
    • On day 0, if you own a stock, the profit is -prices[0] because you bought the stock at prices[0] (dp[0][1] = -prices[0]).
  2. DP Transition:

    for (int i = 1; i < n; i++){dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i] - fee);dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] - prices[i]);
    }
    
    • For each day i from 1 to n-1, we update the dp array:
      • dp[i][0] can be obtained by either:
        • Doing nothing on day i, hence dp[i][0] = dp[i-1][0].
        • Selling the stock on day i, hence dp[i][0] = dp[i-1][1] + prices[i] - fee (selling the stock gives the price at day i, but we must subtract the transaction fee).
      • dp[i][1] can be obtained by either:
        • Doing nothing on day i, hence dp[i][1] = dp[i-1][1].
        • Buying the stock on day i, hence dp[i][1] = dp[i-1][0] - prices[i] (we subtract the price at day i from the profit since we are buying the stock).
  3. Result:

    return dp[n-1][0];
    
    • At the end of the loop, dp[n-1][0] will contain the maximum profit we can achieve on the last day if we don’t own any stock (which is the desired result since we want to end up with no stock to realize the profit).

Summary

  • The algorithm uses dynamic programming to keep track of the maximum profit for each day, considering both states of holding a stock or not holding a stock.
  • The transition equations consider the profit from both holding and selling a stock, incorporating the transaction fee.
  • Finally, the algorithm returns the maximum profit achievable by the end of the last day when no stock is held.

参考代码:

_714买卖股票的最佳时机含手续费

package leetcode板块;public class _714买卖股票的最佳时机含手续费 {/**** @param prices* @param fee* @return*/public int maxProfit(int[] prices, int fee) {//  你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。//  返回获得利润的最大值。int n = prices.length;int [][] dp = new int[n][2];//  0 代表当前股票的空余情况, 1代表当前股票处于抛售的情况dp[0][0] = 0;dp[0][1] = -prices[0];//  TODO   交易的过程中,引入卖出时的手续费for (int i = 1; i < n; i++){dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1] + prices[i] - fee);dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0] - prices[i]);}return dp[n-1][0];}
}

错误经验吸取

http://www.yayakq.cn/news/364764/

相关文章:

  • 深圳网站建设hi0755wordpress代码发布文章
  • 公众号里链接的网站怎么做的深圳市住房和建设局官网站
  • 深圳网站平台vi设计样机
  • 手机网站建设介绍如何通过网络推广产品
  • 做异性的视频网站有哪些深圳模板建站多少钱
  • 网站定制生成器免费元素素材网站
  • 织梦网站标题被改wordpress 首页缓存
  • wordpress网站的配置文件nas搭建网站
  • 企业营销微网站建设福州做网站价格
  • 凡科建站有哪些弊端四川网站建设那家好
  • 成都营销型网站公司电话装潢设计用什么软件比较好
  • 辽宁自助网站建设价格网站商城建设费用
  • 上海外贸网站建设公司价格网站建设和优化
  • logo设计网站国外微博推广效果怎么样
  • 建设银行甘肃省分行网站缴费做网站业务好干吗
  • 怎么给网站做反链北京网站建设有限公司
  • 网站推广员工作咋样校际凡科平台
  • 建设商城网站的书籍钱网络营销方式优缺点
  • 你愿意做我女朋友吗表白网站wordpress 顶部导航条
  • 杭州企业建设网站公司企业门户网站开发
  • 网站关键词优化价格个人做商机网站如何盈利
  • 攻击网站方法wordpress去-
  • 制作小程序和网站的公司站长网站建设
  • 深圳龙岗个人网站建设昆明专业网站制作公司
  • 专业网站建设空间取公司名称大全简单大气
  • 小程序联盟郑州网站关键词优化公司哪家好
  • 上海网站建设公司排名php mysql 网站建设
  • 公司变更登记申请书青州网站优化
  • 宠物网站模板下载福建高速公路建设指挥部网站
  • 中国建设银行wap网站无障碍插件wordpress