LeetCode:Best Time to Buy and Sell Stock
来源:互联网 发布:阿里数据分析师面试 编辑:程序博客网 时间:2024/06/16 09:15
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.
// Source : https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ // Author : Chao Zeng // Date : 2014-12-26 class Solution {public: int maxProfit(vector<int> &prices) {int n = prices.size();if (n == 0)return 0;int mp[n];//表示以prices[i]结尾的max profitint minnumber = prices[0];memset(mp,0,sizeof(mp));for (int i = 1; i < n; i++){if (minnumber > prices[i])minnumber = prices[i];int dis = prices[i] - minnumber;if (mp[i-1] < dis)mp[i] = dis;elsemp[i] =mp[i-1];}return mp[n-1]; }};
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
- 生产者消费者问题理解与Java实现
- VIM用指定编码保存文件
- java jdbc 连接 mysql 数据库, mysql 实现 查询 指定行
- AC dreamoj 1011 树状数组+hash维护字符串的前缀和
- AABB和OBB包围盒简介
- LeetCode:Best Time to Buy and Sell Stock
- 第三章队列知识导图
- java 学习书籍-想精通java的必看
- Swift创建UIButton
- 线程和进程的区别
- 信跟几十万人一起成
- DUILIB配置
- 中科燕园GIS外包团队(手机:13261043797 QQ:1216807928 )预祝各位同行元旦快乐
- Oracle存储过程的编写与测试