121. Best Time to Buy and Sell Stock买卖股票12
一
[抄题]:
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
[一句话思路]:贪心算法。不要用数组i,j角标,直接用min, profit表示价格,更方便。
[画图]:
[一刷]:
min, profit都记得要初始化
[总结]:
[复杂度]:
n/1
[英文数据结构]:
Range Queries
[其他解法]:
[题目变变变]:
maximum subarray最大求和子数组
class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 0 || prices == null) {
return 0;
}
int min = Integer.MAX_VALUE;
int profit = 0;
for (int i = 0; i < prices.length; i++) {
min = min < prices[i] ? min : prices[i];
profit = (prices[i] - min) > profit ? prices[i] - min : profit;
}
return profit;
}
}
二
[抄题]:
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).可以买卖无数次,但是必须先卖后买。
[一句话思路]:
股票profit或者数组sum只有一个时,用贪心算法,容易定义。
股票profit是很多利润的累加时,用数组i,j角标来叠加。因为只要上涨就要卖出,攫取利润。
[画图]:
[一刷]:
由于出现了prices[i + 1], 循环体的上界要减1,为for (int i = 0; i < prices.length - 1; i++)
[总结]:
[复杂度]:
[英文数据结构]:
[其他解法]:
[题目变变变]:
class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 0 || prices == null) {
return 0;
}
int diff = 0;
for (int i = 0; i < prices.length - 1; i++) {
if (prices[i + 1] - prices[i] > 0) {
diff += prices[i + 1] - prices[i];
}
}
return diff;
}
}
121. Best Time to Buy and Sell Stock买卖股票12的更多相关文章
- [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机
网址:https://leetcode.com/problems/Best-Time-to-Buy-and-Sell-Stock/ 第一想法是滑动窗口法,稍微尝试后发现不可行,至少我不会... 而后想 ...
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 121. Best Time to Buy and Sell Stock(股票最大收益)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)
You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...
- [Leetcode] Best time to buy and sell stock 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. If you were ...
- LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)
题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...
- 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
随机推荐
- 亚马逊云EC2做PPTP SERVER的笔记
1.在亚马逊控制台上启动实例 2. 3. 4. 5. 6.配置安全组,把你的IP开放所有流量. 7. 用你自己的亚马逊KEY或者生成一个新的KEY来登录EC2 8.开始搭建VPN-PPTP——how ...
- 开发组件:tmpfs
[Linux]tmpfs简介及增加方式 https://blog.csdn.net/nextaction/article/details/57076924
- pomelo RPC调用时新增字段缺失
接触pomelo开发一个月,正式开始参与项目开发有10天,遇到很多细节的坑,今天讲讲标题:后端服务器节点之间的rpc调用过程中,返回的数据中新增字段缺失问题. 先讲结果:原因是该rpc调用已经采用了p ...
- [Python] numpy.random.rand
numpy.random.rand numpy.random.rand(d0, d1, ..., dn) Random values in a given shape. Create an array ...
- CSS 3 学习笔记
css 3 学习笔记 文本: word-wrap : normal | break-word取值:normal: 控制连续文本换行.break-word: 内容将在边界内换行.如果需要,词 ...
- SignalR (二)
在上节中,我们已经初步对 SignalR 进行了了解,这一节我们将做一个SignalR Demon,具体的步骤如下: 1. 创建一个 mvc 4 web 应用程序,并选择 Basic 2. 创建一个 ...
- 2018-2019-2 《网络对抗技术》Exp1 PC平台逆向破解 Week3 20165233
Exp1 PC平台逆向破解 实验内容 一.基础知识点 NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP指令即"空指令",执行到NOP指令时,CPU什么也不做,机 ...
- centos73下php5.6安装GD库
yum --enablerepo=remi-php56 install php-gd php-mysql php-mbstring php-xml php-mcrypt YUM安装的 找到了源 分分 ...
- div+css样式命名规则,值得收藏
div+css样式命名规则,值得收藏 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:w ...
- sts安装出现could not find jar:file解决办法
转自:https://blog.csdn.net/weixin_43702329/article/details/84823912 标题sts插件下载好但是安装出错 我的eclipse是4.5.2,在 ...