代码随想录第30+31天|51.N皇后 455.分发饼干 376.摆动序列 53.最大子数组和

51.N皇后

题目链接:https://leetcode.cn/problems/n-queens/

这道题目得了解皇后站位规则,规划好边界后就好做题了

class Solution:
    def solveNQueens(self, n: int) -> List[List[str]]:
        if not n:
            return[]
        chessboard = [['.']*n for _ in range(n)]
        res = []
    
        def isValid(chessboard, row, col):
            #check row
            for i in range(len(chessboard)):
                if chessboard[i][col] == 'Q':
                    return False
            #check top left corner
            i = row-1
            j = col-1
            while i>=0 and j>=0:
                if chessboard[i][j] == 'Q':
                    return False
                i -= 1
                j -= 1
            #check top right corner
            i = row-1
            j = col+1
            while i>=0 and j<len(chessboard):
                if chessboard[i][j] == 'Q':
                    return False
                i -= 1
                j += 1
            return True
        
        def backtracking(chessboard, row, n):
            if row == n:
                temp_res = []
                for temp in chessboard:
                    tempstr = ''.join(temp)
                    temp_res.append(tempstr)
                res.append(temp_res)

            for col in range(n):
                if not isValid(chessboard, row, col):
                    continue
                chessboard[row][col] = 'Q'
                backtracking(chessboard, row+1, n)
                chessboard[row][col] = '.'
        backtracking(chessboard, 0, n)
        return res

455.分发饼干

题目链接:https://leetcode.cn/problems/assign-cookies/

很典型的贪心问题,从局部最优 推全局最优

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        s.sort()
        g.sort()
        count = 0

        for i in range(len(s)):
            if count < len(g) and s[i] >= g[count]:
                count += 1

        return count

376.摆动序列

题目链接:https://leetcode.cn/problems/wiggle-subsequence/

当加和为0或者负数时,就毫无意义。

class Solution:
    def wiggleMaxLength(self, nums: List[int]) -> int:
        pre_s, cur_s, count = 0, 0, 1

        for i in range(len(nums)-1):
            cur_s = nums[i+1] - nums[i]
            if cur_s * pre_s <= 0 and cur_s != 0:
                count += 1
                pre_s = cur_s
        return count

53.最大子数组和

题目链接:https://leetcode.cn/problems/maximum-subarray/

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        result = -float('inf')
        count = 0

        for i in range(len(nums)):
            count += nums[i]
            if count > result:
                result = count
            if count <= 0:
                count = 0
        return result

今天的三道贪心题目 都是很典型的贪心应用

还有复习任务 先复习了 溜了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值