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 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.
分析
求买卖最大收益问题。简单来说就是给定一个整数序列,求最大差值。
该题目的关键是,效率。
我们很容易想到二次循环解决该问题,但是很不幸,是超时的! 所以,必须另择他法。
其实在O(n)时间内,也可以解决问题,只需要加一个记录当前最低价格的变量即可;价低购入,价高售出嘛~
AC代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty())
return 0;
//maximum记录最大收益,price记录当前最低价格
int maximum = 0, price = prices[0] ,size = prices.size();
for (int i = 1; i < size ; ++i)
{
//价低时买入
if (prices[i] < price)
{
price = prices[i];
continue;
}
//价高时卖出,比较并记录最大收益
else{
int tmp = prices[i] - price;
if (tmp > maximum)
maximum = tmp;
}
}//for
return maximum;
}
};
LeetCode(121) Best Time to Buy and Sell Stock的更多相关文章
- LeetCode(122) 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 ...
- LeetCode(123) 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 ...
- LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
- <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(121):买卖股票的最佳时机
Easy! 题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买 ...
- LeetCode Array Easy 122. Best Time to Buy and Sell Stock II
Description Say you have an array for which the ith element is the price of a given stock on day i. ...
- [Leetcode 122]买股票II 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 ...
- [LeetCode&Python] Problem 122. 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 (贪心策略 or 动态规划)
Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions Say you ...
随机推荐
- queue模块
queue队列 :使用import queue,用法与进程Queue一样 queue is especially useful in threaded programming when informa ...
- Codeforces 1114E(简单交互)
这里有一道老实题,大家快来踩爆它! 交互题:根据你的输出决定下一次的输入. 请听题: 管理员有个乱序数列(举例:{14, 24, 9, 19}),排序以后是个等差数列({9, 14, 19, 24}) ...
- getpass不起作用
#! /usr/bin/env python# -*- coding:utf-8 -*- # login 模块中登录时输入密码,想用getPass模块实现密码的不回显操作.#如下: import ge ...
- mongodb 文本索引
启用文本搜索: 最初文本搜索是一个实验性功能,但2.6版本开始,配置是默认启用的.但是,如果使用的是以前 MongoDB 的版本,那么必须启用文本搜索,使用下面的代码: >db.adminCom ...
- 8、二进制中1的个数------------>剑指offer系列
题目 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 法一:分析 这是一道考察二进制的题目 二进制或运算符(or):符号为|,表示若两个二进制位都为0,则结果为0,否则为1. 二进制 ...
- CF1060D Social Circles
思路: 贪心.既然每个人的左边是其他人的右边,每个人的右边是其他人的左边,那么使重叠的部分最多即可. 实现: #include <bits/stdc++.h> using namespac ...
- 关于h5中背景音乐的自动播放
音乐的自动播放属性,这里也介绍一下: <audio controls="controls" autoplay="autoplay"> <sou ...
- greendao 查询之数据去重
最近使用greendao的过程中,有一个需求:将数据库的内容根据组别展示.意思就是需要将数据库中的所有组别取出来,然后根据组别加载数据.之前我的笨办法是获取所有的数据,然后对得到的数据手动去重(比较每 ...
- 导入maven的java web项目运行报错找不到Spring监听器
本地成功运行的一个maven项目,在另一台机器复制下来并导入,运行时报错: java.lang.ClassNotFoundException: org.springframework.web.cont ...
- 【读书笔记】构建之法(CH4~CH6)
从chapter4至chapter6,围绕着构建过程的合作讨论构建之法,而合作与个人工作的区别却以一个微妙的问题为开端:阅读别人的代码有多难? 两人合作:(驾驶员与领航员) 合作要注意代码风格规范与设 ...