Best Time to Buy and Sell Stock【找出最大利润】
来源:互联网 发布:淘宝装修工具2.1下载 编辑:程序博客网 时间:2024/05/17 08:17
题目:
https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/
Best Time to Buy and Sell Stock
Total Accepted: 17471 Total Submissions: 56335My SubmissionsSay 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.
题意:
给你一组数据。第 i 个数据代表第 i 天的股价。
你只可以进行一次完整的交易(先买进一支股,再抛出一支股,注意顺序),求最大利润。
本质:求一个 int 数组中的最大差值变种。
算法:
算的上是贪心吧...如果这样也叫算法的话~
心情不好,水题刷着玩,难怪这么弱。
Submit Time Status Run Time Language
code:
/**author:free斩Accepted 52 mscpp*/#include <iostream>#include <vector>using namespace std;class Solution {public: int maxProfit(vector<int> &prices) { int len = prices.size(); if(len <= 0) return 0; int minVal = prices[0]; int maxProfit = 0; for(int i = 0; i < len; i++) { minVal = min(minVal, prices[i]); maxProfit = max(maxProfit, prices[i] - minVal); } return maxProfit; }};int main(){ vector<int> v; v.push_back(3); v.push_back(4); v.push_back(1); Solution ss; int ans = ss.maxProfit(v); cout << ans << endl; return 0;}
0 0
- Best Time to Buy and Sell Stock【找出最大利润】
- (LeetCode)Best Time to Buy and Sell Stock -- 求股票买卖的最大利润
- LeetCode-121:Best Time to Buy and Sell Stock (一次股票交易最大利润)
- LeetCode-122:Best Time to Buy and Sell Stock II (多次股票交易最大利润)
- LeetCode--Best Time to Buy and Sell Stock(最大利润)Python
- Best Time to Buy and Sell Stock 股票交易最大收益
- leetcode_122. Best Time to Buy and Sell Stock II 多次买卖股票,求交易的最大利润
- [LeetCode]121. Best Time to Buy and Sell Stock(求近期股票能获得的最大利润)
- LeetCode-714:Best Time to Buy and Sell Stock with Transaction Fee (带有抛售费用的股票最大利润) -- medium
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- BYOD时代下的无代理准入控制系统
- Windows API OpenClipboard 剪切板
- C++ 进阶
- OC学习(一)
- list去重的两种方法大比拼
- Best Time to Buy and Sell Stock【找出最大利润】
- Eclipse中java和Xml文件字体大小调整:
- No.05 Xcode(5.1.x) 服务器消息推送
- 如何在centos下,安装ffmpeg和vlc
- 图 基本术语
- Windows7如何开启telnet服务
- pdf文档转换器绿色版
- 【日历】铃声设置为SDcard铃声后卸载SDcard,提示音铃声显示为数字事件响应时无铃声输出
- 如何更改SQL Server 登陆验证方式