99_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 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
1:遍历数组;2:每个数字获得该数字之前的最小的数字,并与当时保存的最大值相比較
int maxProfit(vector<int> &prices)
{
if(prices.size() <= 1)
{
return 0;
} int maxValue = 0;
int minPrice = prices[0];
int size = (int)prices.size(); for(int i = 1; i < size; i++)
{
if(prices[i] > minPrice)
{
maxValue = (maxValue > prices[i] - minPrice ? maxValue : prices[i] - minPrice);
}
else
{
minPrice = prices[i];
}
} return maxValue;
}
99_leetcode_Best Time to Buy and sell Stock的更多相关文章
- [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 ...
- [LintCode] 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 ...
- [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——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
随机推荐
- 使用JQuery MiniUI,json数据构建TreeGrid(树图)
index.html直接上代码. 需要引用MiniUI的boot.js <!DOCTYPE html> <html> <head> <meta charset ...
- java自动机器人自动生成修姓名工具类
public class GenerateName { public static String getName() { Random random = new Random(); String[] ...
- LeetCode1-5
Leetcode1: Given an array of integers, return indices of the two numbers such that they add up to a ...
- 邮箱地址自动提示jQuery插件
// mailAutoComplete.js v1.0 邮箱输入自动提示// 2010-06-18 v2.0 使用CSS class类代替CSS对象,同时增强代码可读性// 2010-06-18 v2 ...
- oracle亲手安装过程
适用于centos6 radhat6版本 1.检查依赖库: rpm -q binutils compat-libcap1 compat-libstdc++ compat-libstdc++.i686 ...
- navicat连接mysql8报错,错误提示为1251,原因及解决步骤
一.错误原因: MySQL8.0版本的加密方式和MySQL5.0的不一样,连接会报错. 二.解决步骤: 1.在linux虚拟机上登录mysql 2.更改加密方式: ALTER USER 'root'@ ...
- c#数据库连接学习
/*通过C#winform程序访问数据库数据 用到的命名空间和变量类型: using System.Data.SqlClient; SqlConnection:数据库连接类 SqlCommand:数据 ...
- stark组件之显示页面内容搭建(六)
之前主要介绍了前端页面list_fiter功能的显示,但是list_display功能的展示并没有过多介绍,这里介绍一下是如何实现的. 可以看到凡是蓝线圈起来的都是通过字段名反射一个个取出来的,红线的 ...
- Springboot开启事务
参考资料: https://blog.csdn.net/message_lx/article/details/77584847
- 理解javascript中的Array类型
引子: 从事前端开发有段时间了,个人观点:想在前端开发这条路上走的更远,不仅要学好HTML&HTML5.CSS&CSS3,最重要的就是要学好javascript了.所以打好javasc ...