LeetCode OJ: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.
简单的DP问题,可是一开始我没读懂题目的意思。这里的买入当然应该在卖出之前,所以说不是那种取一个max一个min相减就能解决的,代码如下:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int sz = prices.size();
if(sz == || sz == ) return ;
int min, maxGain;
min = prices[];//min的初值要注意
maxGain = ;
for(int i = ; i < sz; ++i){
if(min > prices[i])
min = prices[i];
else if(maxGain < prices[i] - min)
maxGain = prices[i] - min;
}
return maxGain;
}
};
下面是java写的,runtime还行,击败了60%的runtime,方法和上面有一点不同,如下:
public class Solution {
public int maxProfit(int[] prices) {
int maxVal = 0;
int start = 0;
for(int i = 1; i < prices.length; ++i){
if(prices[i] < prices[i-1]){
maxVal = Math.max(prices[i-1]-prices[start], maxVal);
if(prices[i] < prices[start])
start = i;
}
}
if(prices.length != 0)//排除长度是0的情况
maxVal = Math.max(prices[prices.length - 1] - prices[start], maxVal); //防止出现一直到结尾都不断增大的问题
return maxVal;
}
}
LeetCode OJ:Best Time to Buy and Sell Stock(股票买卖的最佳时期)的更多相关文章
- [LeetCode OJ] Best Time to Buy and Sell Stock I
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode OJ - Best Time to Buy and Sell Stock
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...
- 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 ...
随机推荐
- Windows工作区目录创建
Windows工作区目录创建批处理脚本,目的是养成工作区目录规范的好习惯. @echo off echo 'Create Jingyu Workshop!' rem Author: Alfred Zh ...
- Mysql数据表字段设置了默认值,插入数据后默认字段的值却为null,不是默认值
我将mysql的数据表的某个字段设置了默认值为1,当向该表插入数据的时候该字段的值不是默认值,而是null. 我的错误原因: 对数据库的操作我使用了持久化工具mybatis,插入数据的时候插入的是整个 ...
- Python框架之Tornado(请求)
概述 本篇就来详细介绍tornado服务器(socket服务端)是如何接收用户请求数据以及如果根据用户请求的URL处理并返回数据,也就是上图的3系列所有步骤,如上图[start]是一个死循环,其中利用 ...
- Android 6.0 Kotlin 蓝牙BLE扫描
package com.arci.myapplication import android.os.Bundleimport android.support.design.widget.Snackbar ...
- linux中相关服务不能访问的排错技巧
Linux相关服务不能访问的排错步骤,以HTTP服务为例: 一.服务端排查思路: 1.检查SELinux是否关闭(针对CentOS6系统) (1)临时关闭 setenforce 0 (2 ...
- cocos2d-x与着色器设计--入门篇(游云凌天原创)
http://blog.csdn.net/danjinxiangsi/article/details/43949955 着色器(Shader)应用与计算机图形学领域,指一组提供计算机图形资源在渲染时执 ...
- 高通平台下安卓opencl小例子
http://blog.csdn.net/wcj0626/article/details/26272019 先到高通的qdn下载adreno GPU SDK,里面有OpenCL的例子.https:// ...
- 详解Linux系统中的文件名和文件种类以及文件权限
Linux文件种类与副文件名 一直强调一个概念,那就是:任何装置在Linux底下都是文件, 不仅如此,连资料沟通的介面也有专属的文件在负责-所以,你会瞭解到,Linux的文件种类真的很多- 除了前面提 ...
- [POI2001]和平委员会
题目描述 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条件: 每个党派都在委 ...
- TNS-12541: TNS:no listener , TNS-12542: TNS:address already in use
查看数据库监听状态不对$ lsnrctl status LSNRCTL for IBM/AIX RISC System/6000: Version 10.2.0.5.0 - Production on ...