【Leetcode】【Medium】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.
解题:
遍历数组,随时记录当前出现过的最小值,和出现过的最大收益;
代码:
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的更多相关文章
- <LeetCode OJ> 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 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- 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】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 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 ...
- 【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 ...
随机推荐
- webpack查缺补漏
webpack是模块化打包工具,通过webpack,可以使得我们更加方便地组织代码.压缩.转译等等. 但是学习webpack也需要一定的成本,这里记录使用webpack许久以来一些模糊的知识点,方便以 ...
- 纯CSS让overflow:auto页面滚动条出现时不跳动
现代浏览器滚动条默认是overflow:auto类型的,也就是如果尺寸不足一屏,没有滚动条:超出,出现滚动条.于是,问题来了: 信息流页面,如新浪微博,是从上往下push渲染的.开始只有头部一些信息加 ...
- 配置文件详解和核心api讲解
一.配置文件详解 1.映射文件详解 1.映射配置文件的位置和名称没有限制. -建议:位置:和实体类放在统一目录下. 名称:实体类名称.hbm.xml. 2.在映射配置文件中,标签内的name属 ...
- Golang教程:常量
定义常量 常量(constant)表示固定的值,比如:5,-89,"I love Go",67.89 等等. 考虑如下程序: var b string = "I love ...
- js 屏蔽浏览器事件汇总
//js 屏蔽 window.document.oncontextmenu = function () { event.returnValue = false; }//屏蔽鼠标右键 window.do ...
- Linux定时任务crontab使用指南
crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查 ...
- C++Array类模板编写笔记
C++Array类模板 函数模板和类模板都属于泛型技术,利用函数模板和类模板来创建一个具有通用功能的函数和类,以支持多种不同的形参,从而进一步简化重载函数的函数体设计. 声明方法:template&l ...
- 二、redis的配置
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...
- vcfc之zk+postsql+keystore(cassandra)框架分析
vcfc框架总结: 1 一. bus和keystore是如何协调处理的,什么样的问题是处理不了的? 1. 如果在备重启的过程中,主处理了某个时间1,备机如果同步数据呢? 二 .数据可靠性和一致性分析 ...
- 从官网下载mod_jk.so
最近在搞apache和tomcat集群,这里就要用到mol_jk.so,在官网找了很久才知道在哪里下载,英语是硬伤啊. 1.打开tomcat官网 http://tomcat.apache.org/ 2 ...