LeetCode: Best Time to Buy and Sell Stock
来源:互联网 发布:js页面加载时执行函数 编辑:程序博客网 时间:2024/05/16 13:47
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 min = INT_MAX, profit = 0; for(int i = 0; i < prices.size(); i++) { if(prices[i] < min){ min = prices[i]; } if(prices[i] - min > profit) profit = prices[i] - min; } return profit; }};
Round 2:
class Solution {public: int maxProfit(vector<int> &prices) { int min = INT_MAX; int result = 0; for(int i = 0; i < prices.size(); i++) { if(prices[i] < min) min = prices[i]; if(prices[i] - min > result) result = prices[i] - min; } return result; }};
0 0
- 【LeetCode】Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock
- [LeetCode] [LeetCode]Best Time to Buy and Sell Stock
- 【LeetCode with Python】 Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock
- LeetCode Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- Leetcode: Best time to buy and sell a stock I
- Leetcode: Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock III
- [Leetcode] Best Time to Buy and Sell Stock
- [Leetcode] Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock III
- LeetCode : Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock
- leetcode 16: Best Time to Buy and Sell Stock III
- LeetCode Best Time to Buy and Sell Stock
- 租谞族谞转注浊座咨捉罪左爪
- 他地图就有今天都已经有
- 归并排序 merge sort
- UIView中动画及其属性说明
- IOS-UIDynamic-UIKit动力学(整合版)
- LeetCode: Best Time to Buy and Sell Stock
- 【安卓篇】线程间通信之Message
- 快速排序 quick sort
- Log4j输出终端(Appender)详解
- Linux中让进程在后台运行的方法
- 求关键路径(AOE)
- Android 学习笔记17:Handler,Looper,Message原理
- gdfgdhjfjfdjnfd
- 毕业这三年