_122买卖股票的最佳时机II_贪心

原题链接:

_122买卖股票的最佳时机II_贪心

https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/submissions/540259916/

完成情况:

在这里插入图片描述

解题思路:

Sure! The given Java code solves the “Best Time to Buy and Sell Stock II” problem using a greedy algorithm. Let’s go through the code step-by-step:

Problem Description

The problem requires you to find the maximum profit from buying and selling a stock given its prices over several days. You can buy and sell multiple times, but you can only hold one share of the stock at a time.

Code Explanation

  1. Method Signature:

    public int maxProfit(int[] prices) {
    

    This is the method maxProfit which takes an array of integers prices as input and returns an integer representing the maximum profit.

  2. Initialization:

    int result = 0;
    

    Here, result is initialized to store the cumulative profit.

  3. Greedy Algorithm:

    for (int i = 1; i < prices.length; i++) {
        result += Math.max(0, prices[i] - prices[i-1]);
    }
    

    This loop iterates over the prices array starting from the second day (index 1) to the last day. For each day i, it calculates the profit made by buying the stock on day i-1 and selling it on day i:

    • prices[i] - prices[i-1] represents the profit if the stock is bought on day i-1 and sold on day i.
    • Math.max(0, prices[i] - prices[i-1]) ensures that only positive profits are added to result. If the difference is negative (meaning selling at a loss), it adds 0 instead.

    The greedy approach works because the problem allows for multiple transactions, even on the same day. By always taking advantage of every increase in stock price, we can ensure that the total profit is maximized.

  4. Return Result:

    return result;
    

    Finally, the method returns the accumulated profit stored in result.

Summary

The greedy algorithm used here is straightforward and efficient. It iterates through the price array once (O(n) time complexity), adding up all the positive differences between consecutive days. This ensures that every possible profit opportunity is taken, leading to the maximum profit by the end of the period. This approach is optimal for this problem because it takes advantage of every price increase without worrying about the specifics of when to buy and sell.

参考代码:

_122买卖股票的最佳时机II_贪心

package leetcode板块;

public class _122买卖股票的最佳时机II_贪心 {
    /**
     *
     * @param prices
     * @return
     */
    public int maxProfit(int[] prices) {
        // 这道题由于说了可以同一天卖出买入,因此只需要考虑是否能够赚到钱,即可以
        int result = 0;
        for (int i = 1;i<prices.length;i++){
            result += Math.max(0,prices[i] - prices[i-1]);
        }
        return result;
    }
}

错误经验吸取

  • 30
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值