Leetcode Best Time to Buy and Sell Stock
来源:互联网 发布:南京大学cssci数据库 编辑:程序博客网 时间:2024/06/05 11:41
Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题。
#include <iostream>#include <vector>using namespace std;// We used a base var save the current smallest, and the max of profite comes from the exist max or// the left element subtract current base element.class Solution {public: int maxProfit(vector<int>& prices) { if (prices.size() == 0) { return 0; } int base = prices[0]; int max = 0; int len = prices.size(); for (int i = 0; i < len; i ++) { if (prices[i] < base) { base = prices[i]; } else if (prices[i] - base > max) { max = prices[i] - base; } } return max; }};int main(int argc, char* argv[]) { Solution so; vector<int> test; test.push_back(3); test.push_back(5); test.push_back(2); test.push_back(7); test.push_back(8); int re = so.maxProfit(test); cout<<"result is : "<<re<<endl;}
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
- 【工具】工作中常用到的工具
- Android内存后台进程管理内存阀值minfree修改
- 【剑指Offer】二叉树中和为某一值的路径
- PopupWindow的使用
- ubuntu 安装tengine
- Leetcode Best Time to Buy and Sell Stock
- Widget的使用
- 【剑指Offer】二叉搜索树与双向链表
- 最近忙得实在是不行
- 基于VSM的命名实体识别、歧义消解和指代消解
- Binary Search Tree 二叉查找树
- Eclipse快捷键大全
- Android下拉刷新实现原理
- git & repo 命令