4.9 leetcode -9 best-time-to-buy-and-sell-stock
来源:互联网 发布:linux perl 编辑:程序博客网 时间:2024/06/06 00:11
题目描述
Say you have an array for which the i th element is the price of a given stock on day i.
Say you have an array for which the i th 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 nmoney = 0; int nmoneymin = 0; for(int i = 0;i < prices.size();i++) { if(i == 0) { nmoneymin = prices[i]; } else { if(prices[i] < nmoneymin) nmoneymin = prices[i]; else { if((prices[i] - nmoneymin) > nmoney) nmoney = prices[i] - nmoneymin; } } } return nmoney; }};
阅读全文
0 0
- 4.9 leetcode -9 best-time-to-buy-and-sell-stock
- 【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
- solr的各种deirectoryfactory
- 在中科院计算所实习的一年(更新中。。)
- R语言链接MySQL的两种方法
- Leaders make mistakes
- 利用JDBC获得INSERT插入后生成的主键ID
- 4.9 leetcode -9 best-time-to-buy-and-sell-stock
- JQuery 1.9版本以上的浏览器判断方法
- 使用squid配置透明代理并对上网行为进行控制
- Android开发辅助工具类 Utils 汇总
- ECharts.js学习(一) 简单入门
- 彻底理解javascript的回调函数(推荐)
- Java并发编程的暗自努力(五)Java内存模型
- SSM整合实现增删改查
- 错误提示是因为没有引入javax.servlet.http.HttpServletRequest所在的包,编译错误。 项目–>右键–>properties–>Java build bath–>Lib