LeetCode 每日一题 2023/10/23-2023/10/29

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步


目录

      • 10/23 2678. 老人的数目
      • 10/24 1155. 掷骰子等于目标和的方法数
      • 10/25 2698. 求一个整数的惩罚数
      • 10/26 2520. 统计能整除数字的位数
      • 10/27 1465. 切割后面积最大的蛋糕
      • 10/28 2558. 从数量最多的堆取走礼物
      • 10/29


10/23 2678. 老人的数目

取第12-13位数值

def countSeniors(details):
    """
    :type details: List[str]
    :rtype: int
    """
    ans = 0
    for d in details:
        age = int(d[11:13])
        if age>60:
            ans +=1
    return ans



10/24 1155. 掷骰子等于目标和的方法数

递归 mem记录已有过的情况

def numRollsToTarget(n, k, target):
    """
    :type n: int
    :type k: int
    :type target: int
    :rtype: int
    """
    if n>target or target>n*k:
        return 0
    MOD = 10**9+7
    mem = {(1,x):1 for x in range(1,k+1)}
    def check(n,target):
        if target<=0:
            return 0
        if (n,target) in mem:
            return mem[(n,target)]
        ans = 0
        if n==0:
            ans = target==0
        else:
            for i in range(1,k+1):
                ans += check(n-1,target-i)
        mem[(n,target)] = ans
        return ans%MOD
    return check(n,target)
            



10/25 2698. 求一个整数的惩罚数

遍历每个数 判断是否满足条件

def punishmentNumber(n):
    """
    :type n: int
    :rtype: int
    """
    def check(s,i,x):
        m = len(s)
        if i>=m:
            return x==0
        y = 0
        for j in range(i,m):
            y = y*10+int(s[j])
            if y>x:
                break
            if check(s,j+1,x-y):
                return True
        return False
    ans = 0
    for i in range(1,n+1):
        if check(str(i*i),0,i):
            ans+=i*i
    return ans



10/26 2520. 统计能整除数字的位数

按每一位数字判断

def countDigits(num):
    """
    :type num: int
    :rtype: int
    """
    ans = 0
    n = num
    while n:
        v = n%10
        if v>0 and num%v==0:
            ans +=1
        n//=10
    return ans




10/27 1465. 切割后面积最大的蛋糕

将切口位置从小到大排序 计算两个切口间的距离
寻找最大距离 相乘

def maxArea(h, w, horizontalCuts, verticalCuts):
    """
    :type h: int
    :type w: int
    :type horizontalCuts: List[int]
    :type verticalCuts: List[int]
    :rtype: int
    """
    horizontalCuts.sort()
    verticalCuts.sort()
    if len(horizontalCuts)>0:
        maxh = max(horizontalCuts[0],h-horizontalCuts[-1])
    else:
        maxh = h
    if len(verticalCuts)>0:
        maxv = max(verticalCuts[0],w-verticalCuts[-1])
    else:
        maxv = w
    for i in range(1,len(horizontalCuts)):
        maxh = max(maxh,horizontalCuts[i]-horizontalCuts[i-1])
    for i in range(1,len(verticalCuts)):
        maxv = max(maxv,verticalCuts[i]-verticalCuts[i-1])
    return (maxv*maxh)%(10**9+7)



10/28 2558. 从数量最多的堆取走礼物

大顶堆 每次取最大的礼物 进行处理

def pickGifts(gifts, k):
    """
    :type gifts: List[int]
    :type k: int
    :rtype: int
    """
    import heapq,math
    ans = sum(gifts)
    l = [-x for x in gifts]
    heapq.heapify(l)
    for i in range(k):
        print(l)
        v = -heapq.heappop(l)
        num = int(math.sqrt(v))
        ans -= (v-num)
        heapq.heappush(l, -num)
        
    return ans



10/29





你可能感兴趣的:(Exercise,leetcode,算法,职场和发展)