LeetCode:Best Time to Buy and Sell Stock
来源:互联网 发布:matlab table转为矩阵 编辑:程序博客网 时间:2024/06/04 23:26
题目描述:
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.
代码:
int maxProfit(vector<int> &prices){ int length = prices.size(); if(length == 0) return 0; int max_profit = 0; int low = prices[0]; for(int i = 1;i < length;i++) { int temp = prices[i] - low; if(temp > max_profit) max_profit = temp; if(prices[i] < low) low = prices[i]; } return max_profit;}
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
- c# 实现ComboBox自动模糊匹配
- input标签的hidden属性的应用及作用
- 已知两点经纬度求球面最短距离的公式推导
- c++中冒号和双冒号的用法
- jvm调优总结
- LeetCode:Best Time to Buy and Sell Stock
- at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) 报空指针
- 嘴角定位
- [数据结构] 队列的循环数组实现
- setAliasTexParameters抗锯齿
- 20141119 【 Arduino - DHT11 - 单片机 】 利用单片机数码管 显示 DHT11温湿度
- 分解条件表达式
- java32为md5加密
- [译]信仰是如何毁掉程序员的How religion destroys programmers