LeetCode——Best Time to Buy and Sell Stock
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.
只允许买卖一次,求最大利润。
动态规划,从后向前找最大股价,减去当前股价,求利润,找最大利润。O(n).
public class Solution {
public int maxProfit(int[] prices) { int len = prices.length;
if(len == 0) return 0;
int highestPrice = prices[len - 1];
int res = 0;
for(int i=len-1; i>=0; i--) {
highestPrice = max(highestPrice, prices[i]);
res = max(res, highestPrice-prices[i]);
} return res;
} int max(int a, int b) {
return a > b ? a : b;
}
}
LeetCode——Best Time to Buy and Sell Stock的更多相关文章
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [LeetCode] 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 买卖股票的最佳时间之四
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 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] Best Time to Buy and Sell Stock 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] 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 —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
随机推荐
- ExtJS获取Grid的行数
1. grid.getSelectionModel().getCount() ; // 获得当前选中的行数 2. grid.getStore().getTotalCount(); ...
- Fork of LGPL version of JPedal
https://github.com/on-site/JPedal —————————————————————————————————————————————————————————————————— ...
- Java实现一个简单的缓存方法
缓存是在web开发中经常用到的,将程序经常使用到或调用到的对象存在内存中,或者是耗时较长但又不具有实时性的查询数据放入内存中,在一定程度上可以提高性能和效率.下面我实现了一个简单的缓存,步骤如下. 创 ...
- Lost connection to MySQL server at 'waiting for initial communication packet', system error: 0
场景: 192.168.7.27 需要访问 192.168.7.175 上的MySQL数据库,连接时报错. 原因: MySQL的配置文件默认没有为远程连接配置好,需要更改下MySQL的配置文件. 解决 ...
- PHP与ASP.NET的优劣比较
PHP与ASP.NET的比较 表 1 PHP 4 PHP5 ASP.NET 软件价格 免费 免费 免费 平台价格 免费 免费 $$ 速度 强 强 弱 效率 强 强 弱 安全性 强 强 强 平台 强 强 ...
- 帝国CMS“建立目录不成功!请检查目录权限”的解决办法
初次安装帝国CMS就遇到了一个问题,在提交或者修改信息的时候提示“建立目录不成功!请检查目录权限”,无法生成页面.检查了文件夹的读写权限和用户访问权限,发现都一切正常.那么到底是哪里出错了呢? 其实是 ...
- win7语音识别开发(sapi)
参考:http://msdn.microsoft.com/en-us/library/ee125663(v=vs.85).aspx (sapi5.4 reference) http://msdn ...
- 使用ffmpeg获取视频流后如何封装存储成mp4文件
int main(int argc,char *argv[]) 02 { 03 AVFormatContext *pFormatCtx; 04 int i,videoStream; 05 AVC ...
- linux -- ubuntu 何为软件源
新手学Ubuntu的时候,一般不知道什么是源,但源又是Ubuntu下常用到的东西.因此,本文就详细介绍一下Ubuntu 源. 什么是软件源? 源,在Ubuntu下,它相当于软件库,需要什么软件,只要记 ...
- 最短路径问题-Dijkstra
概述 与前面说的Floyd算法相比,Dijkstra算法只能求得图中特定顶点到其余所有顶点的最短路径长度,即单源最短路径问题. 算法思路 1.初始化,集合K中加入顶点v,顶点v到其自身的最短距离为0, ...