当前位置:网站首页>Greed 122. The best time to buy and sell stocks II
Greed 122. The best time to buy and sell stocks II
2022-07-28 03:44:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
- The best time to buy and sell stocks II
Give you an array of integers prices , among prices[i] Represents a stock i Sky price .
Every day , You can decide whether to buy and / Or sell shares . You at any time most Can only hold A wave of Stocks . You can also buy... First , And then in On the same day sell .
return What you can get Maximum profits .
2 Title Example
Example 1:
Input :prices = [7,1,5,3,6,4]
Output :7
explain : In the 2 God ( Stock price = 1) Buy when , In the 3 God ( Stock price = 5) Sell when , The exchange will make a profit = 5 - 1 = 4 .
And then , In the 4 God ( Stock price = 3) Buy when , In the 5 God ( Stock price = 6) Sell when , The exchange will make a profit = 6 - 3 = 3 .
The total profit is 4 + 3 = 7 .
Example 2:
Input :prices = [1,2,3,4,5]
Output :4
explain : In the 1 God ( Stock price = 1) Buy when , In the 5 God ( Stock price = 5) Sell when , The exchange will make a profit = 5 - 1 = 4 .
The total profit is 4 .
Example 3:
Input :prices = [7,6,4,3,1]
Output :0
explain : under these circumstances , There is no positive profit from trading , So you can get the maximum profit by not participating in the transaction , The biggest profit is 0 .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii
3 Topic tips
1 <= prices.length <= 3 * 104
0 <= prices[i] <= 104
4 Ideas
First of all, this topic should be clear about two points :
- Only one stock !
- At present, only the operation of buying or selling stocks
It takes at least two days for a trading unit to make a profit .
Maybe we just want to , Choose a low buy , Choose a high one to sell , Choose a low buy … Cyclic repetition .
If we think that the final profit can be decomposed , So the problem is very easy !
How to decompose ?
If No 0 Sky buying , The first 3 Day sell , Then the profit is : prices[3] - prices[0].
amount to (prices[3] - prices[2])+ (prices[2] - prices[1])+(prices[1] - prices[o]). At this time, the profit is decomposed into the dimension of daily unit , Not from o To the first day 3 Think about it as a whole !
So according to prices You can get the daily profit sequence :(prices[i] - prices[i - 1……(prices[1] - prices[0]).
— Some students fall into : Why is there no profit on the first day , In the confusion of whether the first day was counted or not .
Of course there was no profit on the first day , It won't be profitable until at least the next day , So the sequence of profits is less than that of stocks — God !
As you can see from the diagram , In fact, we need to collect positive profits every day , Collect positive profit intervals , Is the range of stock trading , And we just need to focus on the final profit , There is no need to record the interval .
Then only collecting positive profits is what greed is greedy for !
Local optimum : Collect daily positive profits , The best in the world : To maximize profits .
Local optimum can deduce global optimum , No counterexample , try — Try greed !
5 My answer
// Greedy thought
class Solution {
public int maxProfit(int[] prices) {
int result = 0;
for (int i = 1; i < prices.length; i++) {
result += Math.max(prices[i] - prices[i - 1], 0);
}
return result;
}
}
边栏推荐
- AI首席架构师12-AICA-百度OCR垂类规模化落地实践
- Ch340 RTS DTR pin programming drives OLED
- 【力扣】1337.矩阵中战斗力最弱的k行
- Tungsten Fabric SDN — BGP as a Service
- Redis source code analysis (who says C language can't analyze it?)
- Daily practice ----- realize the lottery function of two-color ball. Rules: Six non repeating numbers are randomly selected from 36 red balls, and one from 15 basketball is randomly selected to form a
- SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe 试读版
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions
- 【论文笔记】基于深度学习的移动机器人自主导航实验平台
- [openvx] VX for basic use of objects_ image
猜你喜欢

ES6 从入门到精通 # 08:扩展的对象的功能
![[paper notes] mobile robot autonomous navigation experimental platform based on deep learning](/img/6a/7f0c2b2a53332636f3172bc3b0b74d.png)
[paper notes] mobile robot autonomous navigation experimental platform based on deep learning

数据挖掘-02

Ch340 RTS DTR pin programming drives OLED

C语言:求一个整数存储在内存中的二进制中1的个数

Prefix-Tuning: Optimizing Continuous Prompts for Generation

高等数学(第七版)同济大学 习题3-4 个人解答(前8题)

Qt:qmessagebox message box, custom signal and slot

Outlook tutorial, how to use color categories and reminders in outlook?

Light year admin background management system template
随机推荐
我的创作纪念日
8000字讲透OBSA原理与应用实践
[leetcode] 34. Find the first and last positions of elements in the sorted array
In December, the PMP Exam adopted the new syllabus for the first time. How to learn?
[wrong question]mocha and railgun
Capacity expansion and reduction of RBD block storage device (VI)
高等数学(第七版)同济大学 习题3-5 个人解答
Leetcode brush question: dynamic planning 09 (weight of the last stone II)
数据丰富的计算:M.2在边缘遇到AI
Redis source code analysis (who says C language can't analyze it?)
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions
贪心——122. 买卖股票的最佳时机 II
C语言力扣第45题之跳跃游戏 II。遍历跳跃
Outlook tutorial, how to use color categories and reminders in outlook?
How to uninstall clean ZABBIX service? (super detailed)
【OPENVX】对象基本使用之vx_distribution
MySQL Basics (create, manage, add, delete, and modify tables)
Volvo: what on earth does the deep-rooted "sense of security" rely on?
【力扣】1337.矩阵中战斗力最弱的k行
Responsive high-end website template source code Gallery material resource download platform source code