[Leetcode] 每日两题 1001 470 -day90

1001. 网格照明

在这里插入图片描述

哈希表

对于每个灯的位置使用记录一下,同时记录该灯所在的行、列、对角、反对角 的值都+1,应对重复照亮

对于每个查找,查找其行,列、对角、反对角是否大于0 来判断是否点亮

同时对该查找的领域进行 是否有灯判定,然后 减去相应的 行、列、对角、反对角值

class Solution:
    def gridIllumination(self, n: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
        
        row, col, gle, ope = defaultdict(int),defaultdict(int),defaultdict(int),defaultdict(int)
        on  =defaultdict(int)
        light = []
        res = []

        mat =[[-1,1],[0,1],[1,1],
               [-1,0],[0,0],[1,0],
               [-1,-1],[0,-1],[1,-1], 
                ]
        
        for i,j in lamps:
            if on[i,j] ==0:
                on[i,j] = 1
                row[i] +=1
                col[j] +=1
                gle[j-i+n-1] +=1
                ope[i+j] +=1
        for i ,j in queries:
            if row[i] or col[j] or gle[j-i+n-1] or ope[i+j]:
                res.append(1)
            else :
                res.append(0)
                continue

            for [dix,diy] in mat:
                x,y = i+dix,j+diy
                if on[x,y] :
                    on[x,y] = 0
                    row[x] -=1
                    col[y] -=1
                    gle[y-x+n-1] -=1
                    ope[x+y] -=1
           
        return res
470. 用 Rand7() 实现 Rand10()

在这里插入图片描述

构造 两个值分别模拟 1/2 和1/5 的概率

然后组成1/10

# The rand7() API is already defined for you.
# def rand7():
# @return a random integer in the range 1 to 7

class Solution:
    def rand10(self):
        """
        :rtype: int
        """
        
        while True:
            tmp = rand7()
            tmpb =rand7()
            if tmp ==7 or tmpb>5:
                continue
            else :
                res = 0
                if tmp%2:
                    res =5
                return  res+tmpb
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值