【数据结构与算法】贪心算法及面试高频题

目录

贪心算法

面试题leetcode

122. 买卖股票的最佳时机 II - 力扣(LeetCode) (leetcode-cn.com)

455. 分发饼干 - 力扣(LeetCode) (leetcode-cn.com)

874. 模拟行走机器人 - 力扣(LeetCode) (leetcode-cn.com)


贪心算法

贪心算法-对问题求解时,会做出当前看来最好的选择;

适用场景:问题可以分解为子问题,子问题的最优解能够递推到问题的最优解。这种子问题的最优解能够成为最优子结构。

总结:当局部最优解可以推导出全局最优解时,可以使用贪心算法。

贪心——>局部最优——>全局最优

贪心算法和动态规划算法的辨别:

贪心算法可以对每个子问题都的能做出选择,不能回退;

动态规划算法可以保存以前的结果,并根据以前的结果来对当前进行选择,有回退的功能。

面试题leetcode

122. 买卖股票的最佳时机 II - 力扣(LeetCode) (leetcode-cn.com)

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        ans = 0
        for i in range(1,len(prices)):
            ans += max(0,prices[i] - prices[i-1])
        return ans
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int len = prices.size();
        int ans = 0;
        for (int i = 1;i < len; i++){
            ans += max(0,(prices[i]-prices[i-1]));
        }
        return ans;
    }
};

455. 分发饼干 - 力扣(LeetCode) (leetcode-cn.com)

方法:使用排序+贪心算法,将g,s进行排列,之后比对 。

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        n, m = len(g), len(s)
        i = j = count = 0
        while i < n and j < m:
            while j < m and g[i] > s[j]:
                j += 1
            if j < m:
                count += 1
            i += 1
            j += 1        
        return count

        

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());
        int glen = g.size(),slen = s.size();
        int i = 0, j = 0,count = 0;
        while (i < glen && j < slen){
            while (j < slen && g[i] > s[j]){
                j++;
            }
            if (j < slen){
                count++;
            }
            ++i;
            ++j;
        }
        return count;
    }
};

874. 模拟行走机器人 - 力扣(LeetCode) (leetcode-cn.com)

 (看不懂啊~)

 860. 柠檬水找零 - 力扣(LeetCode) (leetcode-cn.com)

 方法:因为纸币的面值只有5,10,20 这三种,所以我们可以分情况进行讨论:

class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        five = ten = 0
        for bill in bills:
            if (bill == 5):
                five += 1
            elif (bill == 10):
                if (five > 0):
                    five -= 1
                    ten += 1
                else:
                    return False
            else:
                if (five > 0 and ten > 0):
                    ten -= 1
                    five -=1
                elif (five > 2):
                    five -= 3
                else:
                    return False
        return True


class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int five = 0,ten = 0;
        for (auto& bill : bills){
            if (bill == 5){
                five++;
            }
            else if (bill == 10){
                if (five > 0){
                    five--;
                    ten++;
                }
                else{
                    return false;
                }               
            }
            else{
                if (five > 0 && ten > 0){
                    five--;
                    ten--;
                }
                else if (five > 2){
                    five -= 3;
                }
                else{
                    return false;
                }
             
            }
        }
        return true;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值