当前位置:网站首页>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
边栏推荐
- Eight bit binary multiplier VHDL
- 22年字节跳动飞书人力套件三面面经
- SQL中函数 decode()与 replace()的用法
- 剑指 Offer II 116. 省份数量-空间复杂度O(n),时间复杂度O(n)
- [popular science] CRC verification (I) what is CRC verification?
- ESP32 LVGL8. 1 - BTN button (BTN 15)
- 【C语言进阶11——字符和字符串函数及其模拟实现(2))】
- ESP32 LVGL8. 1 - input devices (input devices 18)
- Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
- Recyclerview control list item layout match_ Fundamental principle of parent attribute invalidation
猜你喜欢
WebView opens H5 video and displays gray background or black triangle button. Problem solved
视频边框背景如何虚化,简单操作几步实现
[mathematical modeling] - analytic hierarchy process (AHP)
ESP32 LVGL8. 1. Detailed migration tutorial of m5stack + lvgl + IDF (27)
MySQL学习第五弹——事务及其操作特性详解
Esp32 drive encoder -- siq-02fvs3 (vscade + IDF)
[popular science] CRC verification (I) what is CRC verification?
ESP32 LVGL8. 1 - slider slider (slider 22)
Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
ESP32 LVGL8. 1 - label (style 14)
随机推荐
Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
Scrollto and scrollby
Accessing private members using templates
Practice of Druid SQL and security in meituan review
Practice of Druid SQL and security in meituan review
Iptables - L executes slowly
Zlib realizes streaming decompression
Solutions such as unknown or garbled code or certificate problem prompt in Charles's mobile phone packet capture, actual measurement.
实战业务优化方案总结---主目录---持续更新
中金财富怎么样?在上边开户安全吗
机器学习实战 -朴素贝叶斯
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
Nacos作为服务配置中心实战
c#:泛型反射
Advanced transfer learning
K210 serial communication
机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
Screen right-click menu in souI
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system