当前位置:网站首页>2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
2022-04-23 18:53:00 【Leeli9316】

Method : greedy

class Solution {
public int maxProfit(int[] prices, int fee) {
int profit = 0;
//buy It means that on the premise of maximizing benefits , The lowest purchase price of a stock
int buy = prices[0] + fee;
for (int i = 1; i < prices.length; i++) {
// If the current stock price plus handling fee is less than buy
// It means you can buy at a lower price , to update buy
if (prices[i] + fee < buy) {
buy = prices[i] + fee;
// If the current stock price is greater than buy, You can get income
// But actually , At this time, selling stocks may not be globally optimal ( For example, the stock price continues to rise the next day )
} else if (prices[i] > buy) {
profit += prices[i] - buy;
// So will buy Updated to prices[i], If the stock price continues to rise the next day ,
// Will get prices[i+1]−prices[i] Revenue , Add this day prices[i]−buy Revenue ,
// It's exactly the same as not doing anything on this day , And the return on selling shares the next day
buy = prices[i];
}
}
return profit;
}
}
版权声明
本文为[Leeli9316]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231851339576.html
边栏推荐
- Advanced transfer learning
- 微搭低代码零基础入门课(第三课)
- 简化路径(力扣71)
- The corresponding permissions required to automatically open the app in the setting interface through accessibility service
- c#:泛型反射
- Druid SQL和Security在美团点评的实践
- 2022.04.23(LC_714_买卖股票的最佳时机含手续费)
- 深入理解 Golang 中的 new 和 make 是什么, 差异在哪?
- 视频边框背景如何虚化,简单操作几步实现
- ESP32 LVGL8. 1. Detailed migration tutorial of m5stack + lvgl + IDF (27)
猜你喜欢

ctfshow-web361(SSTI)

C: generic reflection

Raspberry pie uses root operation, and the graphical interface uses its own file manager

mysql_ Download and installation of Linux version

STM32: LCD display

Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha

Esp32 (UART 485 communication) - 485 communication of serial port (3)

c#:泛型反射

ESP32 LVGL8. 1 - msgbox message box (msgbox 28)

实战业务优化方案总结---主目录---持续更新
随机推荐
7、 DOM (Part 2) - chapter after class exercises and answers
Nacos集群搭建和mysql持久化配置
Accessing private members using templates
配置iptables
ESP32 LVGL8. 1 - textarea text area (textarea 26)
Get a list of recent apps
os_ authent_ Prefix
WebView saves the last browsing location
On iptables
Golang 语言实现TCP UDP通信
视频边框背景如何虚化,简单操作几步实现
根据快递单号查询物流查询更新量
【历史上的今天】4 月 23 日:YouTube 上传第一个视频;网易云音乐正式上线;数字音频播放器的发明者出生
程序员如何快速开发高质量的代码?
os_authent_prefix
listener.log
From technical system to business insight, the closing chapter of the practice of small and medium-sized R & D team structure
MVVM model
Introduction to micro build low code zero Foundation (lesson 3)
ESP32 LVGL8. 1 - label (style 14)