leetcode Day8----array.easy

这篇博客探讨了LeetCode中的股票交易问题,包括'Best Time to Buy and Sell Stock'的一次交易策略和'Best Time to Buy and Sell Stock II'的多次交易策略。作者通过C语言和Python3的实现,讲解如何找到最大利润,提供了不同的解决方案,并分享了代码运行的时间和内存使用情况。

Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: 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.
Example 2:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

找到一串数字的最小值和最大值,并相减得到收益最大。

C语言(1)

int maxProfit(int* prices, int pricesSize) {
    int profit=0;
    for(int i=0;i<pricesSize-1;i++){
        for(int j=i+1;j<pricesSize;j++){
           if(prices[j]-prices[i]>profit){
               profit=prices[j]-prices[i];
           } 
        }
    }
    return profit;
}

Success
Details
Runtime: 792 ms, faster than 21.31% of C online submissions for Best Time to Buy and Sell Stock.
Memory Usage: 7.9 MB, less than 5.17% of C online submissions for Best Time to Buy and Sell Stock.

C语言(2)

正在思考更快的方法,以后更新。。。

python3

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        max_profit, min_price = 0, float('inf')
        for price in prices:
            min_price = min(min_price, price)
            profit = price - min_price
            max_profit = max(max_profit, profit)
        return max_profit
        

Success
Details
Runtime: 52 ms, faster than 30.87% of Python3 online submissions for Best Time to Buy and Sell Stock.
Memory Usage: 13.8 MB, less than 5.08% of Python3 online submissions for Best Time to Buy and Sell Stock.

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 (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:

Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.

对于这道题我开始提交了一份傻瓜式的for循环代码,得到如下结果:
在这里插入图片描述
由此可知,并不是限制买两次的(我好傻啊。),所以,改用以下方法:
思路是用temp储存数列中每两个数的差值,如果大于零,则是赚钱的,那么就算进总收益里。

C语言

int maxProfit(int* prices, int pricesSize) {
    int profit = 0;
    for(int i=0;i<pricesSize-1;i++){
        int temp = prices[i+1]-prices[i];
        if(temp>0)
            profit += temp;
    }
    return profit; 
}

Success
Details
Runtime: 4 ms, faster than 87.85% of C online submissions for Best Time to Buy and Sell Stock II.
Memory Usage: 7.9 MB, less than 11.90% of C online submissions for Best Time to Buy and Sell Stock II.

python3

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        profit = 0
        for i in range(len(prices)-1):
            temp=prices[i+1]-prices[i]
            if(temp>0):
                profit+=temp
        return profit
        

Success
Details
Runtime: 52 ms, faster than 22.44% of Python3 online submissions for Best Time to Buy and Sell Stock II.
Memory Usage: 14 MB, less than 5.06% of Python3 online submissions for Best Time to Buy and Sell Stock II.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值