当前位置:网站首页>贪心——122. 买卖股票的最佳时机 II
贪心——122. 买卖股票的最佳时机 II
2022-07-28 03:21:00 【向着百万年薪努力的小赵】
1 题目描述
- 买卖股票的最佳时机 II
给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。
2 题目示例
示例 1:
输入:prices = [7,1,5,3,6,4]
输出:7
解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3 。
总利润为 4 + 3 = 7 。
示例 2:
输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
总利润为 4 。
示例 3:
输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii
3 题目提示
1 <= prices.length <= 3 * 104
0 <= prices[i] <= 104
4 思路
本题首先要清楚两点:
- 只有一只股票!
- 当前只有买股票或者卖股票的操作
想获得利润至少要两天为一个交易单元。
这道题目可能我们只会想,选一个低的买入,在选个高的卖,在选一个低的买入…循环反复。
如果想到其实最终利润是可以分解的,那么本题就很容易了!
如何分解呢?
假如第0天买入,第3天卖出,那么利润为: prices[3] - prices[0]。
相当于(prices[3] - prices[2])+ (prices[2] - prices[1])+(prices[1] - prices[o])。此时就是把利润分解为每天为单位的维度,而不是从o天到第3天整体去考虑!
那么根据prices可以得到每天的利润序列:(prices[i] - prices[i - 1……(prices[1] - prices[0])。
—些同学陷入:第一天怎么就没有利润呢,第一天到底算不算的困惑中。
第一天当然没有利润,至少要第二天才会有利润,所以利润的序列比股票序列少—天!
从图中可以发现,其实我们需要收集每天的正利润就可以,收集正利润的区间,就是股票买卖的区间,而我们只需要关注最终利润,不需要记录区间。
那么只收集正利润就是贪心所贪的地方!
局部最优:收集每天的正利润,全局最优:求得最大利润。
局部最优可以推出全局最优,找不出反例,试—试贪心!
5 我的答案
// 贪心思路
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;
}
}
边栏推荐
- 一篇文章掌握Postgresql中对于日期类数据的计算和处理
- Redis source code analysis (who says C language can't analyze it?)
- Shell: one click deployment PXE
- Mouse operation and response
- Volvo: what on earth does the deep-rooted "sense of security" rely on?
- 20220726汇承科技的蓝牙模块HC-05的AT命令测试
- Shell:一键部署pxe
- 超好看的Nteam官网PHP程序源码
- 一键重装win7系统详细教程
- Assembly method of golang Gorm query arbitrary fields
猜你喜欢

【5G NR】RRC Reject解析

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

Redis source code analysis (who says C language can't analyze it?)

Detailed tutorial of one click reinstallation of win7 system

机器人开发--丝杠与导轨

Log analysis tool (Splunk)

20220726汇承科技的蓝牙模块HC-05的AT命令测试

Summary of concurrent programming interview questions

颜色的识别方法和探索 基于matlab

695. 岛屿的最大面积
随机推荐
Digital economy has become the biggest attraction
Methods of SQL server backup database
动画(animation)
Assembly method of golang Gorm query arbitrary fields
Practice of online problem feedback module (16): realize the function of checking details
容器相关的概念
What if the word selection box of win11 input method is missing?
8000字讲透OBSA原理与应用实践
Redis基本操作
ASEMI整流桥GBPC5010,GBPC5010参数,GBPC5010大小
Volvo: what on earth does the deep-rooted "sense of security" rely on?
2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb
Mouse operation and response
Redis basic operation
A treasure simulates login and reduces the method of secondary verification
如何使用JDBC操作数据库
695. 岛屿的最大面积
同时导出多个excel,并且一个excel中包含多个sheet
2022 summary of the latest Android handler related interview questions
Version compatibility issues