LeetCode Best Time to Buy and Sell Stock 系列
来源:互联网 发布:733动漫软件 编辑:程序博客网 时间:2024/06/10 07:39
121 Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on dayi.
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.
int maxProfit(int* prices, int pricesSize) { int low = *prices, ret = 0; for(int i = 1; i < pricesSize; i++){ if(prices[i] < low ) low = prices[i]; else ret = ret > prices[i] - low ? ret : prices[i] - low; } return ret;}
#122 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on dayi.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
int maxProfit(int* prices, int pricesSize) { int ret = 0; for(int i = 1; i < pricesSize; i++){ ret += prices[i] - prices[i-1] > 0 ? prices[i] - prices[i-1] : 0; } return ret;}
#123 Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on dayi.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
int maxProfit(int* prices, int pricesSize) {int i, j, tran1[pricesSize], tran2[pricesSize];int low = prices[0], high = prices[pricesSize - 1];tran1[0] = 0; tran2[pricesSize - 1] = 0; for(i = 1; i < pricesSize; i ++ ){if(prices[i] < low) low = prices[i];tran1[i] = prices[i] - low > tran1[i-1] ? prices[i] - low : tran1[i-1];}for(j = pricesSize - 2; j >= 0; j --){if(prices[j] > high) high = prices[j]; tran2[j] = high - prices[j] > tran2[j+1] ? high - prices[j] : tran2[j+1];}int ret = 0;for(i = 0; i < pricesSize; i ++){ ret = tran1[i] + tran2[i] > ret ? tran1[i] + tran2[i] : ret;}return ret;}
#188 Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on dayi.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
int max(int x, int y) { return x > y ? x : y; }int maxProfit(int k, int* prices, int pricesSize) { int i, j, ret = 0; if(k >= pricesSize){ for(i = 1; i < pricesSize; i++) ret += prices[i] - prices[i - 1] > 0 ? prices[i] - prices[i - 1] : 0; return ret; } int local[k+1], global[k+1]; for(i = 0; i <= k; i++){ local[i] = global[i] = 0; } for(i = 1; i < pricesSize; i ++){ for(j = k; j >= 1; j--) { local[j] = max( local[j] + prices[i] - prices[i-1], global[j-1] + max( prices[i] - prices[i-1], 0 ) ); global[j] = max(local[j], global[j]); } } return global[k];}
- 【Leetcode长征系列】Best Time to Buy and Sell Stock
- [LeetCode]Best Time to Buy and Sell Stock 系列
- LeetCode-Best Time to Buy and Sell Stock系列
- LeetCode Best Time to Buy and Sell Stock 系列
- LeetCode Week12: Best Time to Buy and Sell Stock系列
- leetCode 系列:best time to buy and sell stock
- LeetCode系列:best time to buy and sell stock
- leetcode 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
- (java)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系列问题
- 【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
- 长沙节假日去哪里玩?
- EXT中的迭代
- mathjax简单教程(翻译)
- 使用Java开源工具包问题及解决
- bestcoder“赛码”GCD
- LeetCode Best Time to Buy and Sell Stock 系列
- codeforces 7D Palindrome Degree
- webService的理论知识与见解
- ext之Date常用方法
- Eclipse设置Java反编译
- list_entry()详解
- web开发学习笔记(4):js获取table的行数和表格中某一行某一列的内容
- hdu 5223 GCD
- DFS_HDU_5222