算法练习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)
            
相关推荐
_深海凉_6 分钟前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
2301_800976938 分钟前
正则表达式
开发语言·python·正则表达式
码界奇点21 分钟前
基于Python的新浪微博数据爬虫系统设计与实现
数据库·爬虫·python·毕业设计·新浪微博·源代码管理
AI木马人43 分钟前
1.人工智能实战:大模型推理接口响应慢?从模型加载到 FastAPI 部署的完整优化方案
人工智能·python·fastapi
青少儿编程课堂1 小时前
2026青少儿信息素养大赛备赛指南!Python/Scratch/C++备考要点
开发语言·c++·python
踩坑记录1 小时前
leetcode hot100 寻找两个正序数组的中位数 hard 二分查找 双指针
leetcode
旖-旎1 小时前
深搜练习(电话号码字母组合)(3)
c++·算法·力扣·深度优先遍历
谭欣辰1 小时前
C++快速幂完整实战讲解
算法·决策树·机器学习
Mr_pyx1 小时前
【LeetHOT100】随机链表的复制——Java多解法详解
算法·深度优先
用户8356290780511 小时前
使用 Python 设置 Excel 数据验证
后端·python