买卖股票的最佳时机-LintCode
来源:互联网 发布:linux 删除全部内容 编辑:程序博客网 时间:2024/04/28 18:11
假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。
样例
给出一个数组样例 [3,2,3,1,2], 返回 1
#ifndef C149_H#define C149_H#include<iostream>#include<vector>using namespace std;class Solution {public: /** * @param prices: Given an integer array * @return: Maximum profit */ int maxProfit(vector<int> &prices) { // write your code here int len = prices.size(); if (len <= 0) return 0; vector<int> dp(len); int count = INT_MIN; for (int i = 1; i < len; ++i) { if (dp[i - 1] >= 0) dp[i] = dp[i - 1] + prices[i] - prices[i - 1]; else dp[i] = prices[i] - prices[i - 1]; count = dp[i]>count ? dp[i] : count; } return count>0?count:0; }};#endif
阅读全文
0 0
- LintCode-买卖股票的最佳时机
- LintCode--买卖股票的最佳时机
- Lintcode:买卖股票的最佳时机
- 买卖股票的最佳时机,lintcode
- 【LintCode】买卖股票的最佳时机
- lintcode--买卖股票的最佳时机
- Lintcode 买卖股票的最佳时机
- 买卖股票的最佳时机-LintCode
- LintCode--买卖股票的最佳时机 II
- lintcode-买卖股票的最佳时机-149
- lintcode-买卖股票的最佳时机II-150
- LintCode:买卖股票的最佳时机 II
- LintCode:买卖股票的最佳时机 III
- Lintcode 买卖股票的最佳时机 II
- LintCode 151 买卖股票的最佳时机 III
- LintCode 152 买卖股票的最佳时机 IV
- lintcode--买卖股票的最佳时机II
- 买卖股票的最佳时机 II-LintCode
- 数论在ACM中的应用
- JS URL传中文参数引发的乱码问题
- CSDN使用:使用CSDN的MarkDown编辑器,以及MarkDown编辑器的帮助文档
- matlab图像预处理
- Zoj——3203 Light Bulb
- 买卖股票的最佳时机-LintCode
- jbpm工作流框架
- wrap_content引发的血案
- XML文件和JSON文件的解析及生成
- 截取邮箱后缀名,拼接访问邮箱地址
- 刷新页面后,数据保留在原页面方法(session信息共享,ajax局部刷新)
- Invocation of init method failed; nested exception is org.hibernate.MappingException
- 关于java中的Properties类
- TensorFlow 安装