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).
public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function if (prices == null || prices.length <= 0) return 0; int min = 0; int max = 0; int result = 0; for (int i = 1; i <= prices.length - 1; i++) { if (prices[i] > prices[max]) { max = i; }else { result += prices[max] - prices[min]; max = i; min = i; } } result += prices[max] - prices[min]; return result; } }
相关推荐
leetcode题目:Best Time to Buy and Sell Stock II
java lru leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring ...121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time ...II
Best Time to Buy and Sell Stock II Longest Substring Without Repeating Characters Container With Most Water Patching Array 动态规划 Triangle Maximum Subarray Maximum Product Subarray Longest ...
II (Easy) 可多次交易,找总最大收益 for i in range(1,len(prices): profit+=prices[i]-prices[i-1] if prices[i]-prices[i-1]>0 Best Time to Buy and Sell Stock III (Hard) 最多两次交易(同理固定次数的多次)...
Best Time to Buy and Sell Stock 122 买卖股票的最佳时机 Ⅱ Best Time to Buy and Sell StockⅡ 123 买卖股票的最佳时机 Ⅲ Best Time to Buy and Sell StockⅢ 188 买卖股票的最佳时机Ⅳ Best Time to Buy and ...
Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate Reverse Polish Notation #169 Majority Element #171 Excel Sheet Column Number #217 Contains Duplicate #226 Invert Binary Tree #237 ...
Best Time to Buy and Sell Stock II 6 GROUP ANAGRAMS 7 COUNTING ELEMENTS 日 问题描述 问题和解决方案链接 Git 解决方案页面 8 Middle of the Linked List 9 Backspace String Compare 10 Min Stack 11 Diameter ...
Best Time to Buy and Sell Stock II Valid Sudoku linked list Palindrome linked list Linked List Cycle trees Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic ...
了解加法过程122.Best Time to Buy and Sell Stock II -> 动态规划 -> 27.Remove Element -> 两个指针,前后,交换268.Missing Number -> 数字之和 [0,1,2,...,n] 是一个常数,所以可以计算出缺失的那个169.Majority...
63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必
leetcode 分类 LeetCode LeetCode Java Solution 2018年5月31日 更新 最近刷了一遍 ...题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客:
Time to Buy and Sell Stock #0125 - Valid Palindrome #0136 - Single Number #0167 - Two Sum - Input Array is sorted #0189 - Rotate Array #0217 - Contains Duplicate #0242 - Valid Anagram #0243 - Shortest...
公众号 coding 笔记、点滴记录,以后的文章也会...最近花时间分门别类整理了 LeetCode 的前 500 题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客:
Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than buying price. 尚未添加 HTML 和 CSS 文件。 我希望你们都自己做:) 如果您...
Best Time to Buy and Sell Stock [121]3. Climbing Stairs [70]4. Maximum Subarray [53]5. House Robber [198]6. Range Sum Query - Immutable [303]7. Counting Bits [338]8. Palindromic Substrings [647]9. ...
Best Time to Buy and Sell Stock with Transaction Fee 0713. 乘积小于K的子数组 0695. 岛屿的最大面积 0674. 最长连续递增序列 0673. 最长递增子序列的个数 0567. 字符串的排列 0563. Binary Tree Tilt 0547. 朋友...
leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。 为一年后的研究生找工作准备 ...Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]
leetcode leetcode The optimum C++ solutions for the leetcode 这是我自己写的leetcode的题解,目前已经完成了70%左右,后续部分会很快更新 这里放上来的代码都能保证是最优解(复杂度最优) 当然有些题目因为测试...
LeetCode LeetCode题解 ... Best_Time_To_Buy_And_Sell_StockII Java 简单的 125 Valid_Palindrome Java 简单的 136 单号 Java 简单的 137 Single_NumberII Java 中等的 167 Two_Sum_II_Input_
Best Time To Buy And Sell Stock [Binary Tree](Basic-Knowledge/Binary tree.md) Serialize And Deserialize Bit 1.position 2.sequence Sell Stock 区间型 矩阵坐标 Word Ladder Add Two Numbers Matrix Spiral ...