LeetCode & Q121-Best Time to Buy and Sell Stock-Easy
Array DP
Description:
Say you have an array for which the ith element is the price of a given stock on day i.
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.
Example 1:
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)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0.
先说明题目意思,刚开始我看了很久也没搞明白,去Discuss上才看明白了。就是说,Input数组表示这段时间每天的货物价格,你以某个价格买入,之后再以那天的价格卖出,获取利益。例如:
Input: [7,1,5,3,6,4]
在价格为1时买入,价格为6时卖出,可获利润为5
Input: [7,6,4,3,1]
一直再降价,所以不买该货物,利润为0
Input: [2,4,1]
第一天买,第二天卖,可获利为2
Input: [3,2,6,5,0,3]
第二天买,第三天卖出,获利为4,比价格为0时买入,价格为3时卖出获利更多
my Solution:
public class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int profit = 0;
if (n == 0) {
return profit;
}
else {
int min = prices[0];
int max = prices[0];
for (int i = 1; i < n; i++) {
if (min >= prices[i]) {
min = prices[i];
max = min;
}
if (max <= prices[i]) {
max = prices[i];
}
profit = Math.max(profit, max-min);
}
return profit;
}
}
}
我的方法非常常规,这题最好的方法就是用动态规划的思想来解。
Best Solution:
public class Solution {
public int maxProfit(int[] prices) {
int maxCur = 0;
int maxSoFar = 0;
for (int i = 1; i < prices.length; i++) {
maxCur = Math.max(0, maxCur + prices[i] - prices[i-1]);
maxSoFar = Math.max(maxCur, maxSoFar);
}
return maxSoFar;
}
}
我对动态规划的理解还不够深刻,还需要多学习,多做这方面的题,加油!
LeetCode & Q121-Best Time to Buy and Sell Stock-Easy的更多相关文章
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode】Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- [Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
随机推荐
- 360加固apk并自动签名
我们知道Android加混淆之后,代码的安全性得到了提高,即使你hook,反编译得到的也是乱码的,对于阅读性造成了影响,为了增强代码的破解难度,我们通常退对apk进行加固,常见的有腾讯,360,爱加密 ...
- hive java编写udf函数
(一)创建JAVA 代码--例子 package hiveOpt; import org.apache.hadoop.hive.ql.exec.UDF;import org.apache.hadoop ...
- 初步谈谈 C# 多线程、异步编程与并发服务器
多线程与异步编程可以达到避免调用线程异步阻塞作用,但是两者还是有点不同. 多线程与异步编程的异同: 1.线程是cpu 调度资源和分配的基本单位,本质上是进程中的一段并发执行的代码. 2.线程编程的思维 ...
- Hybrid App混合模式开发的了解
Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app,兼具"Native App良好用户交互体验的优势"和"Web Ap ...
- Wp-UserAgent——让WordPress在评论后面加上浏览器和操作系统信息
在很多的博客网站都看到过在评论的后面显示了浏览器和操作系统的信息,网上也用过一些插件,但是都不是很好看,有一次在一个网页上看见了这个评论后面不仅显示了浏览器和操作系统的图片,还有文字信息, 感觉不错, ...
- 流式处理新秀Flink原理与实践
随着大数据技术在各行各业的广泛应用,要求能对海量数据进行实时处理的需求越来越多,同时数据处理的业务逻辑也越来越复杂,传统的批处理方式和早期的流式处理框架也越来越难以在延迟性.吞吐量.容错能力以及使用便 ...
- Python创建容器和集合之源码分析
_collections_abc.py文件中提供了许多抽象基类,这些类将集合分解成许多互相独立的属性集 __all__ = ["Awaitable", "Coroutin ...
- Linux中查看进程树
pstree -p 查看当前的shell的进程和执行shell脚本的子进程的方法:echo $$
- angularJS前台传list数组,后台springMVC接收数组
有时候需要在前台自定义对象,然后把对象封装在list中,在传送到后台,这样的思想也比较合理 1. 前台代码 $scope.saveScore = function () { $scope.userSc ...
- mysql limit 接收变量
参考文章:https://blog.csdn.net/ljz2009y/article/details/7887743 PREPARE s1 FROM 'SELECT * FROM t LIMIT ? ...