Best Time to Buy and Sell Stock II
来源:互联网 发布:网络状况调查报告交流 编辑:程序博客网 时间:2024/05/18 08:18
题目描述: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).
解题思路:在交易中,当股价小于曾经最大股价时,卖出股价,累加利润。
Java代码实现:
public class Solution { public int maxProfit(int[] prices) { if(prices.length==0) return 0; int min=prices[0],max=prices[0]; int profit=max-min; for(int i=1;i<prices.length;i++){ if(prices[i]>max) max=prices[i]; if(prices[i]<max) { profit+=max-min; min=prices[i]; max=prices[i]; } } profit+=max-min; return profit; }}
原题题目:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
0 0
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- LeetCode : Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [LeetCode] Best time to buy and sell a stock II
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- 【leetcode】Best Time to Buy and Sell Stock II
- LeetCode - Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock II
- LeetCode之Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- Index for Html
- android学习笔记(21)用户配置信息的保存
- 策划设计
- 导航栏
- Environment.getExternalStorageDirectory()与Environment.getExternalStoragePublicDirectory(String type)
- Best Time to Buy and Sell Stock II
- 机器学习实战——第一章
- 基础知识备忘录
- 自然界的目的,更深层次的密码以及未知的灾难----读《三体》第一部第三节
- lintcode-最小路径和-110
- js中SetInterval与setTimeout用法
- Adobe Photoshop CS6学习要点
- mac 常用shell命令
- 解析响应式图片