LeetCode 122 Best Time to Buy and Sell Stock II
LeetCode to and II Time Best Buy Stock
2023-09-14 09:10:09 时间
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) { if(prices.length==0||prices.length==1) return 0; int result=0; for(int i=1;i<prices.length;i++){ if(prices[i]>prices[i-1]) result+=(prices[i]-prices[i-1]); } return result; } }
相关文章
- Leetcode 之Convert Sorted List to Binary Search Tree(55)
- Leetcode 之Convert Sorted Array to Binary Search Tree(54)
- Java实现 LeetCode 688 “马”在棋盘上的概率(DFS+记忆化搜索)
- Java实现 LeetCode 面试题62. 圆圈中最后剩下的数字(约瑟夫环)
- Java实现 LeetCode 572 另一个树的子树(遍历树)
- Java实现 LeetCode 124 二叉树中的最大路径和
- Java实现 LeetCode 72 编辑距离
- Java实现 LeetCode_0038_CountandSay
- Java实现LeetCode_0041_FirstMissingPositive
- 【LeetCode算法-13】Roman to Integer
- LeetCode(94):二叉树的中序遍历
- LeetCode:114_Flatten Binary Tree to Linked List | 将一棵二叉树变成链表的形式 | Medium
- [LeetCode] Different Ways to Add Parentheses
- [LeetCode] Best Time to Buy and Sell Stock III
- [LeetCode] Fraction to Recurring Decimal
- LeetCode(41):缺失的第一个正数
- LeetCode-1590. 使数组和能被 P 整除【前缀和,哈希表】
- 【LeetCode Python实现】38. 外观数列(中等)
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)
- [LeetCode] Flatten Binary Tree to Linked List
- Best Time to Buy and Sell Stock I,II,III [leetcode]
- Leetcode:best_time_to_buy_and_sell_stock_II题解
- leetcode 849. Maximize Distance to Closest Person
- leetcode 671. Second Minimum Node In a Binary Tree
- leetcode 720. Longest Word in Dictionary
- leetcode 122. Best Time to Buy and Sell Stock II