3.Best Time to Buy and Sell Stock(买卖股票)
Level:
Easy
题目描述:
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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example 1:
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 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
Explanation: In this case, no transaction is done, i.e. max profit = 0.
思路分析:
遍历数组,保存当前遇到的最小元素minval,然后计算访问到的元素与其差值,保留遇到的最大差值,就是结果。
代码:
class Solution {
public int maxProfit(int[] prices) {
int minVal=Integer.MAX_VALUE;
int difference=0;
for(int i=0;i<prices.length;i++){
minVal=Math.min(prices[i],minVal);
difference=Math.max(difference,prices[i]-minVal);
}
return difference;
}
}
3.Best Time to Buy and Sell Stock(买卖股票)的更多相关文章
- [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 ...
- [LintCode] 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] 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 ...
- 121. Best Time to Buy and Sell Stock买卖股票12
一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...
- [Leetcode] Best time to buy and sell stock 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. If you were ...
- LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)
题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...
- 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机
网址:https://leetcode.com/problems/Best-Time-to-Buy-and-Sell-Stock/ 第一想法是滑动窗口法,稍微尝试后发现不可行,至少我不会... 而后想 ...
- 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】121. Best Time to Buy and Sell Stock(股票问题)
You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...
- [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 ...
随机推荐
- 开发环境入门 linux基础 (部分)正则表达式 grep sed
/etc/profile /etc/bashrc .变量添加到shell环境中,永久生效. /root/.bashrc /root/.bash_profile 正则表达式 定义:正则就是用一些具有特 ...
- C获取当前时间
#include <stdio.h> #include <time.h> #include <string> #include <windows.h> ...
- css垂直居中方法(二)
第四种方法: 这个方法把一些div的显示方式设置为表格,因此我们可以使用表格的vartial-align属性. 代码如下: <!doctype html> <html lang=&q ...
- spring-boot 热加载实现替换Jrebel
导读: 本文主要说说,在玩spring-boot时,我们经常要遇到重启服务这种浪费时间的事情,为了割掉这个痛点,我们一般有2中方式实现. 一个是springload , 另外一个是 spring-bo ...
- JavaScript的流程控制语句
JS的核心ECMAScript规定的流程控制语句和其他的程序设计语言还是蛮相似的.我们选择一些实用的例子来看一下这些语句.顺序结构我们在这里就不再提到,直接说条件和循环以及其他语句.一.条件选择结构 ...
- Realsense深度相机资料
1.Realsense SDK 2.0 Ubuntu 16.04 安装指导网址 https://github.com/IntelRealSense/librealsense/blob/master/d ...
- nginx负载均衡, 配置地址带端口
nginx.conf 配置如下: upstream wlcf.dev.api { server 127.0.0.1:8833; server 127.0.0.2:8833; } server { l ...
- ruby 变量和方法
def say_goodnight(name) result ="Good night ." +name return result end def say_goodmorning ...
- c++ 拷贝构造函数 继承
拷贝构造函数要求把所有变量都需要做拷贝.在有继承关系情况先,子类的拷贝构造函数,需要调用父类拷贝构造函数.示例代码如下: class Base{ public: virtual ~Base(); Ba ...
- CF 464E The Classic Problem
补一补之前听课时候的题. 考虑使用dij算法求最短路,因为边权存不下,所以考虑用主席树维护二进制位,因为每一次都只会在一个位置进行修改,所以可以暴力进位,这样均摊复杂度是对的. <算法导论> ...