Topic describes
The best time to buy or sell stocks II // Given an array prices, where prices[I] is the price of a given stock on day I. // Design an algorithm to calculate the maximum profit you can make. You can complete as many trades (multiple times // buying and selling a stock) as possible. // Note: you can't participate in more than one transaction at a time (you must sell your previous shares before buying again).Copy the code
Answer key
[Leetcode] [Leetcode] [Leetcode] [Leetcode] // Greed in this question is not necessarily the case with actual transactions. // If temp is positive, add it to the final res, and so on. // Violent multiple trades are the most profitable. // // execution time: 1 ms, beating 99.63% user // memory consumption in all Java commits: Class Solution {public int maxProfit(int[] prices) {int res = 0; for (int i = 1; i < prices.length; i++) { int temp = prices[i] - prices[i - 1]; if (temp > 0) res += temp; } return res; }}Copy the code