(DP)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 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.
题目大意:
给定一个数组,里面的值表示一支股票每天的价格, 如第i天价格为prices[i],假设只能进行一次买进,一次卖出,请找出最大利润值。
解:
以prices[i]表示第i天股票价格,profit[i]表示到第i天为止所能获取的最大利润。
考虑在第n天卖出股票的最大利润 profit[n]=max(profit[n-1], prices[n]-min), 其中min为前n-1天的股票最低价。
可见我们必须在遍历数组的过程中记录最小值。
而且profit只跟前一个状态有关,所以只需用一个值来记录,而不用数组。
Java代码:
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0)
return 0;
int min = prices[0];
int profit = 0;
for(int i = 1; i < prices.length; i++){
min = prices[i] < min ? prices[i] : min;
profit = prices[i] - min > profit ? prices[i] - min : profit;
}
return profit;
}
}
Python代码:
class Solution:
# @param {integer[]} prices
# @return {integer}
def maxProfit(self, prices):
if not prices:
return 0
tmin = prices[0]
profit = 0
for each in prices[1:]:
profit = max(profit, each-tmin)
tmin = min(each, tmin)
return profit
(DP)Best Time to Buy and Sell Stock的更多相关文章
- [LeetCode] Best Time to Buy and Sell Stock 6道合集【DP】
1. Best Time to Buy and Sell Stock 2. Best Time to Buy and Sell Stock II 3. Best Time to Buy and Sel ...
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
- Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)
Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)
Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...
- Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)
Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode123:Best Time to Buy and Sell Stock III
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
随机推荐
- C与C++中的const
同样,有下面一段代码: #include <iostream> using namespace std; int main() { ; int *j = (int *) &i; * ...
- 程序员必备基础知识:通信协议——Http、TCP、UDP
CP HTTP UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则“说话”,对方才能理解或为之服务. TCP HTTP UDP三者的关系: TCP/IP是个协议组,可分为四个层次: ...
- 传智播客学习之HTML基础语法
一.基本格式 1.不用区分大小写. 2.HTML代码由<html>开始</html>结束.里面由头部分<head></head>和体部分<body ...
- 编写高质量代码改善python程序91个建议学习01
编写高质量代码改善python程序91个建议学习 第一章 建议1:理解pythonic的相关概念 狭隘的理解:它是高级动态的脚本编程语言,拥有很多强大的库,是解释从上往下执行的 特点: 美胜丑,显胜隐 ...
- js为鼠标添加右击事件
<script language="javascript"> /*document.oncontextmenu=Youji;*/ //为当前文档添加鼠标右击事件,防 ...
- unity提取打包资源
untiy打包资源是不可见的,在代码中须要www载入去提取,当然也有别的方法去提取打包资源.这对于非常久远的数据打包资源来说是个非常好的方法,由于太久远了就找不到了,仅仅能拿打包资源去提取,之前我写过 ...
- Android应用开发提高系列(4)——Android动态加载(上)——加载未安装APK中的类
前言 近期做换肤功能,由于换肤程度较高,受限于平台本身,实现起来较复杂,暂时搁置了该功能,但也积累了一些经验,将分两篇文章来写这部分的内容,欢迎交流! 关键字:Android动态加载 声明 欢迎转载, ...
- JavaScript 函数基础
1. JavaScript 函数基础 1. 定义方法 2. 函数的调用方法 3. 函数方法 apply : 将函数作为数组的方法来调用 将参数以数组形式传递给该方法 call : 将函数作为对象的 ...
- 在struts2的action中操作域对象(request、session)
在struts2的Action中,操作域对象一共有三种方式: 1.ActionContext(与servelt API无关联): //相当于request ActionContext.getConte ...
- 软件工程师所需掌握的“终极技术”是什么?
软件工程师所需掌握的"终极技术"是什么? http://yunli.blog.51cto.com/831344/1019990 最近,我在微博上看到@程序员邹欣老师发的一条微博 - ...