(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 ...
随机推荐
- cf413E Maze 2D
E. Maze 2D time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- FreeBSD简单配置SSH并用root远程登陆方法
FreeBSD简单配置SSH并用root远程登陆方法 前言:最近下载了FreeBSD,在虚拟机上安装,第一步先要开启SSH服务,用终端putty软件可以实现在windows系统进行远程管理, 初级 = ...
- Apache+php配置 Mysql安装出错解决办法
此文包括的注意内容:软件版本及下载地址Apache2.4的配置和安装php7.0的配置mysql5.5的安装常见问题及解决方法1.软件版本Windows server 2008 r2+ 64位Apac ...
- poj 1486 Sorting Slides(二分图匹配的查找应用)
Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...
- AllocConsole
#include<iostream> using namespace std; AllocConsole(); freopen("CONIN$", "r+t& ...
- PHP的错误处理方式
错误类型 PHP 主要有两种错误:触发错误和异常.其中触发错误大概可以分为:编译错误.引擎错误和运行时错误,其中前两个是无法捕获的:异常都是可以捕获的,当没有尝试捕获时则会中断代码. 触发错误可以通过 ...
- ORACLE SEQUENCE 介绍
在oracle中sequence就是所谓的序列号,每次取的时候它会自己主动添加,一般用在须要按序列号排序的地方. 1.Create Sequence 你首先要有CREATE SEQUENCE或者C ...
- Android开发(20)--RadioGroup的使用
RadioGroup 有时候比較实用.主要特征是给用户提供多选一机制. MainActivity.java package com.example.lesson16_radio; import and ...
- 使用PLSql连接Oracle时报错ORA-12541: TNS: 无监听程序
非常多时候为了优化我们的启动项把oracle的服务禁止了.但是重新启动启动之后使用PLSQL登陆oracle时会出现无监听程序,这说明我们有一些服务没有启动.我们先查看一下oracle的服务是否启动, ...
- Laravel Cheat 表 http://cheats.jesse-obrien.ca/#
Laravel Cheat Sheet Toggle Code Comments PDF Version Github Laravel 3 Docs Laravel 4 Docs Artisan ph ...