算法练习Day25 (Leetcode/Python-贪心算法)

贪心算法基本概念:

贪心的本质是通过选取局部最优达到全局最优。

并没有固定的套路,检验的算法正确性的办法可以是举反例。

455. Assign Cookies

Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

Example 1:

复制代码
Input: g = [1,2,3], s = [1,1]
Output: 1
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. 
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
You need to output 1.

思路:分饼干,一个饼干最多分给一个人,若饼干大小>=孩子胃口则该孩子获得满足,设计算法尽可能满足更多的孩子。

这道题用贪心算法做,先对饼干和孩子排序。可以从大饼干和胃口大的孩子开始遍历,本着不浪费的原则,大饼干优先满足大胃口的孩子,对于每一块饼干,将胃口从大到小的孩子和它一一对应,如果满足了一个孩子,则move to下一块次大的饼干。

python 复制代码
class Solution(object):
    def findContentChildren(self, g, s):
        """
        :type g: List[int] # children 
        :type s: List[int] # cookies
        :rtype: int
        """
        g.sort() # small to large 
        s.sort() # small to large 
        j = len(s) - 1  # cookies
        matched = 0
        for i in range(len(g) - 1, -1, -1):
            if j < 0:  # Check if there are any cookies left
                break
            if s[j] >= g[i]:  # If the current cookie satisfies the appetite, move to the next cookie
                matched += 1
                j -= 1
        return matched

时间复杂度的分析:

sort:O(nlogn),O(mlogm)。n和m分别为人和饼干的个数

for循环:O(n)。

Overall:O(nlogn) + O(mlogm)。对于较小的n值,O(n)和O(n log n)可能相当接近。但随着n的增大,O(n log n)的增长速度将超过O(n)。

53. Maximum Subarray

Given an integer array nums, find the

subarray

with the largest sum, and return its sum.

Example 1:

复制代码
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.

思路:求最大连续子序列之和。暴力解法O(n^2)的复杂度,做两次循环,遍历所有可能的子序列。但是也可以用一个count逐位累加,在这个过程中不断更新max_count的值。若累加值为负数,则清空重开一局,也就是以之后一个元素为新的子序列开头,防止之前子序列的负数造成拖累。

python 复制代码
class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        count = 0
        max_count = float('-inf') # 注意这里是float('-inf')而不是0,有可能最大和也是是负数。
        for i in range(len(nums)):
            count += nums[i]
            if count > max_count: # 注意这两个if的顺序不可以颠倒
                max_count = count
            if count <= 0: # 若当前子序列累加和都小于0了,对继续延长的序列的也是拖累,不如重新开始。
                           # 重新开始计数之前这个子序列的最大和已经被记录下来了。
                count = 0 
        return max_count 
        # 时间复杂度O(n)
            
相关推荐
爱编程的化学家3 分钟前
代码随想录算法训练营第十一天--二叉树2 || 226.翻转二叉树 / 101.对称二叉树 / 104.二叉树的最大深度 / 111.二叉树的最小深度
数据结构·c++·算法·leetcode·二叉树·代码随想录
强盛小灵通专卖员4 分钟前
闪电科创 SCI专业辅导
python·深度强化学习·研究生·ei会议·导师·sci期刊
tqs_123454 分钟前
redis zset 处理大规模数据分页
java·算法·哈希算法
跟橙姐学代码15 分钟前
自动化邮件发送的终极秘籍:Python库smtplib与email的完整玩法
前端·python·ipython
扯淡的闲人41 分钟前
多语言编码Agent解决方案(2)-后端服务实现
开发语言·python·深度学习
蒋星熠1 小时前
深度学习实战指南:从神经网络基础到模型优化的完整攻略
人工智能·python·深度学习·神经网络·机器学习·卷积神经网络·transformer
吃着火锅x唱着歌1 小时前
LeetCode 1446.连续字符
算法·leetcode·职场和发展
愚润求学1 小时前
【贪心算法】day10
c++·算法·leetcode·贪心算法
万粉变现经纪人1 小时前
如何解决pip安装报错ModuleNotFoundError: No module named ‘cuml’问题
python·scrapy·beautifulsoup·pandas·ai编程·pip·scipy
吴秋霖1 小时前
主流反爬虫、反作弊防护与风控对抗手段
爬虫·算法·反爬虫技术