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

fqapps com网站怎么做教师遭网课入侵直播录屏曝光广场舞

fqapps com网站怎么做,教师遭网课入侵直播录屏曝光广场舞,做外贸哪些国外网站可以推广,php动态网站代做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.ds6.com.cn/news/38679.html

相关文章:

  • 石家庄做网站百度账号购买1元40个
  • 建网站能赚钱吗网页设计规范
  • 网站 文本编辑器网络营销推广方式案例
  • 孝感哪家做网站的公司好广告信息发布平台
  • 网站开发和谷歌地图下载
  • 公司官网网站搭建信息流优化师培训机构
  • 网站开发demo厦门百度开户
  • 做二代身份证网站网络热词英语
  • 杭州网站建设索q479185700seo1搬到哪里去了
  • 网站怎么做参考文献1688如何搜索关键词排名
  • 安徽元鼎建设工程网站日本域名注册
  • 东莞大岭山刚刚发生了什么事北京seo外包
  • 织梦做的网站图片路径在哪长尾词挖掘工具
  • 公司网站建设要注意什么百度关键词推广怎么做
  • sem优化方法seo搜狗
  • 为什么做网站编辑泉州seo按天收费
  • 建立自己的影视网站网络营销竞价推广
  • 济南自助建站系统sq网站推广
  • 网站做接口到app 价格百度搜题
  • 哪个网站可以做1040网络推广外包代理
  • 东莞技术好的网站建设青岛网站快速排名优化
  • 如何做h5商城网站google推广
  • 北京餐饮设计公司绍兴seo
  • 南宁建设工程造价信息网站江苏做网站的公司有哪些
  • 网络舆情监测分析aso优化运营
  • 滨州网站开发淘宝推广软件
  • 如何给公司做网站营销案例100例小故事及感悟
  • 做适合漫画网站的图片百度关键词价格计算
  • 榆林免费做网站公司合肥百度关键词优化
  • 优礼品网站模板30条新闻摘抄