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.

解题:

遍历数组,随时记录当前出现过的最小值,和出现过的最大收益;

代码:

 class Solution {
public:
int maxProfit(vector<int> &prices) {
int size = prices.size();
int minV = INT_MAX;
int maxP = ; for (int i = ; i < size; ++i) {
minV = min(minV, prices[i]);
maxP = max(maxP, prices[i] - minV);
}
return maxP;
}
};

【Leetcode】【Medium】Best Time to Buy and Sell Stock的更多相关文章

  1. &lt;LeetCode OJ&gt; 121. /122. Best Time to Buy and Sell Stock(I / II)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  2. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  3. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...

  7. 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 ...

  8. 【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 ...

  9. 【LeetCode OJ】Best Time to Buy and Sell Stock III

    Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ Linear Time Solut ...

  10. 【leetcode】123. Best Time to Buy and Sell Stock III

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

随机推荐

  1. Tensorflow样例代码分析cifar10

    github地址:https://github.com/tensorflow/models.git 本文分析tutorial/image/cifar10教程项目的cifar10_input.py代码. ...

  2. Web测试注意事项

    参考文章:http://www.51testing.com/html/07/n-3723307.html 总结下遇到的web测试的时候需要注意的地方: 页面分辨率:  通常是计算机的默认分辨率,但是还 ...

  3. 案例15-基本的表单校验使用validate

    1 导入插件 <!--引入jquery相关文件 --> <script src="js/jquery-1.11.3.min.js" type="text ...

  4. java获取当前秒数输出

    Date的getSeconds()已经过时了.不建议用.所以用了下面方法 Calendar c = Calendar.getInstance(); while(true) {            c ...

  5. java里面如何提升编写速度

    一般情况下,编写程序,是在规定的时间内,并且在一段时间内很好的按成,那么就必须要套用现成的东西.在一个新的功能中,如何调用现成的东西呢,那么就是使用第三方包或者是使用自己总结的代码库.接来下是自己看到 ...

  6. 在C代码中将结构体变量作为参数传递效率忒低

    在C语言编程中,我们几乎不可能看见有人将一个结构体变量作为参数进行传递,因为效率太低了.本文尝试从反汇编的角度给出其中的缘由. 对于C语言来说,所有的参数传递都是值传递.如果一个变量为指针,那么传递的 ...

  7. KMeans实现

    KMeans实现 符号 \(K\): 聚类的个数 \(x^{(i)}\): 第i个样本 \(\mu_{1},\mu_{2},...\mu_{K}\): K个中心节点 \(c^{(i)}\): 第i个样 ...

  8. jmeter(5)——参数化

    之前接触过QTP或者Loadrunner的小伙伴,应该对参数化不陌生,在<badboy详解篇>中也介绍了badboy的参数化,今天说一下jmeter的参数化,同样,我们举例说明,以msn. ...

  9. 从零实现一个简易的jQuery框架之二—核心思路详解

    如何读源码 jQuery整体框架甚是复杂,也不易读懂.但是若想要在前端的路上走得更远.更好,研究分析前端的框架无疑是进阶路上必经之路.但是庞大的源码往往让我们不知道从何处开始下手.在很长的时间里我也被 ...

  10. JQuery extend()与工具方法、实例方法

    使用jQuery的时候会发现,jQuery中有的函数是这样使用的: $.get(); $.post(); $.getJSON(); 有些函数是这样使用的: $('div').css(); $('ul' ...