leetcode:122. Best Time to Buy and Sell Stock II(java)解答

原创 2016年03月05日 17:43:02

转载请注明出处:z_zhaojun的博客
原文地址
题目地址
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 day i.

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 int maxProfit(int[] prices) {
        int maxPro = 0;
        int length = prices.length - 1;
        for (int i = 0; i < length; i++) {
            if (prices[i] < prices[i + 1]) {
                maxPro += prices[i + 1] - prices[i];
            }
        }
        return maxPro;
    }
版权声明:本文为博主原创文章,转载请注明出处。博客首页:http://blog.csdn.net/u012975705。订阅:http://blog.csdn.net/u012975705/rss/list

leetcode-java-121. Best Time to Buy and Sell Stock

思路: 要获得最大利益,其实就是找到相差最大的两个数。 所以用动态规划,找出当前数组中最小的值和当前最大利益public class Solution { public int maxPr...
  • github_34514750
  • github_34514750
  • 2016年06月05日 10:32
  • 314

weekly contest 55 Best Time to Buy and Sell Stock with Transaction Fee

题目Your are given an array of integers prices, for which the i-th element is the price of a given sto...
  • weixin_38739799
  • weixin_38739799
  • 2017年10月23日 14:21
  • 466

用go实现linux命令行

用go实现linux命令行
  • caijhBlog
  • caijhBlog
  • 2017年10月17日 20:26
  • 129

Leetcode算法学习日志-714 Best Time to Buy and Sell Stock with Transaction Fee

Leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee 题目原文 Your are given an array o...
  • Zarlove
  • Zarlove
  • 2017年10月23日 21:16
  • 1344

Leetcode:714. Best Time to Buy and Sell Stock with Transaction Fee

DescriptionYour are given an array of integers prices, for which the i-th element is the price of a ...
  • csdn_cinderella
  • csdn_cinderella
  • 2017年12月13日 14:02
  • 48

LeetCode题解系列--714. Best Time to Buy and Sell Stock with Transaction Fee

描述Your are given an array of integers prices, for which the i-th element is the price of a given sto...
  • bowen_wu_sysu
  • bowen_wu_sysu
  • 2017年12月13日 16:39
  • 62

714-

Best Time to Buy and Sell Stock with Transaction Fee Your are given an array of integers prices, fo...
  • hefenglian
  • hefenglian
  • 2017年10月22日 12:11
  • 248

714. Best Time to Buy and Sell Stock with Transaction Fee

Your are given an array of integers prices, for which the i-th element is the price of a given stock...
  • xdhc304
  • xdhc304
  • 2018年01月27日 16:32
  • 37

714. Best Time to Buy and Sell Stock with Transaction Fee

Your are given an array of integers prices, for which the i-th element is the price of a given stoc...
  • zjucor
  • zjucor
  • 2017年10月23日 10:12
  • 728

【12月19日】LeetCode刷题日志(三):Best Time to Buy and Sell Stock with Transaction Fee

题目描述: Your are given an array of integers prices, for which the i-th element is the price of a giv...
  • Moon_treee
  • Moon_treee
  • 2017年12月19日 15:22
  • 47
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:122. Best Time to Buy and Sell Stock II(java)解答
举报原因:
原因补充:

(最多只允许输入30个字)