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 ...
随机推荐
- [UE4]C++创建对象的三种方式
#include <iostream> using namespace std; class A { private: int n; public: A(int m):n(m) { } ~ ...
- java 为什么wait(),notify(),notifyAll()必须在同步方法/代码块中调用?
在Java中,所有对象都能够被作为"监视器monitor"——指一个拥有一个独占锁,一个入口队列和一个等待队列的实体entity.所有对象的非同步方法都能够在任意时刻被任意线程调用 ...
- VC字符串转换常用函数
最近在做一些关于VC的ActiveX小插件,经常会遇到字符串处理的问题,狂查CSDN和MSDN,结果并不理想.先说明一下,相关处理函数在VC++6.00测试通过.也许很多人不能理解,现在都什么年代了, ...
- 生存分析与R
生存分析与R 2018年05月19日 19:55:06 走在码农路上的医学狗 阅读数:4399更多 个人分类: R语言 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- uva-639-枚举
题意: 象棋里的車可以吃横竖的車,题目加了一个墙,用于阻断攻击,问4x4的棋盘最多可以放多少只車, 思路:枚举每一个点,2^16次方种情况 #include<stdio.h> #inclu ...
- text-overflow:ellipsis ,溢出文本显示省略号
text-overflow:ellipsis 对溢出文本显示省略号有两个好处, 一是不用通过程序限定字数 二是有利于SEO. 需要使用对对溢出文本显示省略号的通常是文章标题列表,这样处理对搜索引擎更友 ...
- HTML5 Canvas ( 填充图形的绘制 ) closePath, fillStyle, fill
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- sts安装出现could not find jar:file解决办法
转自:https://blog.csdn.net/weixin_43702329/article/details/84823912 标题sts插件下载好但是安装出错 我的eclipse是4.5.2,在 ...
- java垃圾回收几种算法
1.引用计数法 2.标记——清除法 3.标记——整理算法 4.copying算法 5.generation算法(新生代.老年代.持久代) 详情参考:深入理解 Java 垃圾回收机制
- tomcat 服务器发布网站
第一种方法:在tomcat中的conf目录中,在server.xml中的,<host/>节点中添加: D:\Program Files\Apache Software Foundation ...