力扣第三十四天(DP topic)

本文探讨了两个编程问题:如何找到价值最大化的景点配对组合,以及何时能实现股票交易的最大利润。第一个问题是关于计算景点游览的最优得分,第二个是经典股票买卖问题的最佳时机选择。给出了错误和正确解决方案的对比,展示了高效算法设计的重要性。
摘要由CSDN通过智能技术生成

problem Ⅰ

1014. Best Sightseeing Pair
You are given an integer array values where values[i] represents the value of the ith sightseeing spot. Two sightseeing spots i and j have a distance j - i between them.

The score of a pair (i < j) of sightseeing spots is values[i] + values[j] + i - j: the sum of the values of the sightseeing spots, minus the distance between them.

Return the maximum score of a pair of sightseeing spots.

Example 1:

Input: values = [8,1,5,2,6]
Output: 11
Explanation: i = 0, j = 2, values[i] + values[j] + i - j = 8 + 5 + 0 - 2 = 11

Example 2:

Input: values = [1,2]
Output: 2

solution 1

class Solution {
public:
    int maxScoreSightseeingPair(vector<int>& values) {
        int cur = values[0]-1, maxs = INT_MIN;
        for(int i=1; i<values.size(); i++){
            maxs = max(maxs, cur + values[i]);
            cur = max(cur, values[i])-1;
        }
        return maxs;
    }
};

problem Ⅱ

121. Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Example 1:

Input: prices = [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.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.

wrong solution

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size()==1)return 0;
        int mins = prices[0], maxs = prices[1];
        int pmin = 0, pmax = 1;
        for(; pmax < prices.size(); pmax++){
            if(prices[pmax] >= maxs){
                maxs = prices[pmax];
                for(; pmin < pmax; pmin++){
                    if(prices[pmin] < mins)
                        mins = prices[pmin];
                }
            }
        }
        if(maxs - mins > 0) return maxs - mins;
        else return 0;
    }
};

correct solution

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int left = 0, right = 1, maxs = 0;
        for(; right < prices.size(); right++){
            if(prices[right] > prices[left]){
                maxs = max(maxs, prices[right]-prices[left]);
            }else left = right;
        }
        return maxs;
    }
};


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值