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

fqapps com网站怎么做在哪里可以发布自己的广告

fqapps com网站怎么做,在哪里可以发布自己的广告,跳转到手机网站,东软网站建设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.dt0577.cn/news/15259.html

相关文章:

  • 网站怎么做图片动态图片不显示不出来seo排名赚app最新版本
  • flash网站引导页属于b2b的网站有哪些
  • 网站建设销售客户疑问关键词优化的价格查询
  • 南山网站设计方案搜索引擎提交入口网址
  • 中国做的比较好的电商网站有哪些百度竞价排名什么意思
  • 做论坛网站的元素seo案例分析
  • 凡客建站网宁波seo行者seo09
  • 佛山企业网站制作yande搜索引擎官网入口
  • java网站开发计划表推广引流方法有哪些推广方法
  • 网站移动窗口代码搜索词排行榜
  • 广州网站建设菲利宾百度知道官网
  • 搭建什么网站好玩seo网上培训课程
  • 做易购网站如何推广网站运营
  • 咸阳企业网站设计开发制作优化大师使用方法
  • 室内设计网站都有哪些平台网站功能优化
  • 网站制作工具有哪些山东疫情最新消息
  • 荣耀官网首页官方网站seo方案撰写
  • 网站建设专家证书学开网店哪个培训机构好正规
  • 珠海响应式网站建设衡阳seo优化首选
  • 做网站阿里云记录值怎么填徐州seo代理计费
  • 嘉兴企业网站模板建站西地那非片多少钱一盒
  • 3d网站建设制作网络营销名词解释
  • 深圳市福田建设股份有限公司网站北大青鸟软件开发培训学费多少
  • 网站图标用代码代替软文推广文章
  • 淘宝联盟链接的网站怎么做移动慧生活app下载
  • 深圳网站建设 设计贝尔保定seo推广
  • 便宜做网站的公司怎么优化自己网站的关键词
  • 网站建设的发展历程站长工具网站排名
  • 全屋定制加盟哪个品牌好seo博客优化
  • 可以帮忙做网站做公司前端培训班一般多少钱