LeetCode 每日一题 2022/12/5-2022/12/11

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




12/5 1687. 从仓库到码头运输箱子

因为箱子必须顺序处理
我们可以使用数组way来统计需要在码头转运的次数 与上一个码头不同则+1 否则不变
ws前缀和统计箱子重量
不包括从仓库出发和回到仓库
例如箱子目标码头为
编号 1 2 3 4 5
port 1,1,2,1,2
way 1,1,2,3,4
weight 1,2,1,1,1
ws 0,1,3,4,5,6
如果我们处理2~4号箱子 需要转运3-1=2次 1->2->1
动态规划 dp[i]表示运送完i个箱子需要的次数
假设之前运了j个 这次需要运送[j+1,i] j=[i-maxBoxes,i-1]
dp[i] = min(dp[i],dp[j]+way[i-1]-way[j]+2)
way[i-1]固定 要使得dp[j]-way[j]尽可能小
l数组存储dp[j]-way[j]单调递增的编号j
数组l[0]存储的即为满足条件的最小dp[j]-way[j]的编号j

ef boxDelivering(boxes, portsCount, maxBoxes, maxWeight):
    """
    :type boxes: List[List[int]]
    :type portsCount: int
    :type maxBoxes: int
    :type maxWeight: int
    :rtype: int
    """
    n = len(boxes)
    dp = [0]*(n+1)
    way = [1]*(n)
    ws =[0]+[boxes[0][1]]*(n)
    for i in range(1,n):
        ws[i+1] = boxes[i][1]+ws[i]
        if boxes[i][0]==boxes[i-1][0]:
            way[i] = way[i-1]
        else:
            way[i]=way[i-1]+1
            
    l = [0]
    for i in range(1,n+1):
        while l and (i-l[0]>maxBoxes or ws[i]-ws[l[0]]>maxWeight):
            l.pop(0)
        dp[i] = dp[l[0]]+way[i-1]-way[l[0]]+2
        if i<n:
            while l and dp[l[-1]]-way[l[-1]]>=dp[i]-way[i]:
                l.pop()
            l.append(i)
    return dp[n]



12/6 1805. 字符串中不同整数的数目

识别每一个字符是否是数字
如果是侧加入到当前数字串
如果不是判断数字串是否为空 不为空则将数字加入map中

def numDifferentIntegers(word):
    """
    :type word: str
    :rtype: int
    """
    m = {}
    cur = ""
    for c in word:
        if c.isnumeric():
            cur += c
        elif cur!="":
            m[int(cur)]=1
            cur = ""
        else:
            cur = ""
    if cur!="":
        m[int(cur)]=1
    return len(m)



12/7 1775. 通过最少操作次数使数组的和相等

假设两个数组长度为m,n m>=n 如果m>6*n 则无法完成
将两个数组内从小到大排序 将和较小的为数组1 和较大的为数组2 差值为diff
比较较小数组当前最小值nums1[l]变为6 与
较大数组当前最大值nums2[r]变为1
那个变化大并变化小于diff的 则变化那个 l+1 或者 r-1

def minOperations(nums1, nums2):
    """
    :type nums1: List[int]
    :type nums2: List[int]
    :rtype: int
    """
    n,m=len(nums1),len(nums2)
    if n>6*m or m>6*n:
        return -1
    nums1.sort()
    nums2.sort()
    s1,s2 = sum(nums1),sum(nums2)
    if s1>s2:
        s1,s2=s2,s1
        nums1,nums2=nums2,nums1
    diff = s2-s1
    ans = 0
    l,r=0,len(nums2)-1
    while diff>0:
        if l<len(nums1) and r>=0:
            tmp = max(6-nums1[l],nums2[r]-1)
            diff -= tmp
            ans+=1
            if 6-nums1[l]>nums2[r]-1:
                l+=1
            else:
                r-=1
        elif r>=0:
            diff -= nums2[r]-1
            r-=1
            ans+=1
        else:
            diff -= 6-nums1[l]
            l+=1
            ans+=1
    return ans



12/8 1812. 判断国际象棋棋盘中一个格子的颜色

将字母a对应1转换 两坐标相加为奇数则为白色

def squareIsWhite(coordinates):
    """
    :type coordinates: str
    :rtype: bool
    """
    return True if (ord(coordinates[0])-ord('a')+1+int(coordinates[1]))%2==1 else False




12/9 1780. 判断一个数字是否可以表示成三的幂的和

假设n = 3a+3b+3^c a<b<c
每一轮除以3 经过a轮后得到 1+3(b-a)+3(c-a)
此时余数为1 因为幂不同 所以在同一轮中余数只能为0或1
如果得到余数2说明不满足

def checkPowersOfThree(n):
    """
    :type n: int
    :rtype: bool
    """
    while n>0:
        if n%3==2:
            return False
        n//=3
    return True



12/10 1691. 堆叠长方体的最大高度

将长方体三条边从小到大排列w,l,h
根据题意
如果一个长方体需要放在另一个长方体上 需要满足w1<=w2,l1<=l2,h1<=h2
我们可以使用最大的h为高度
dp[i] 设为以i为最后一个长方体的最大堆叠高度
将长方体三条边之和从大到小排列 依次遍历
如果c1和大于c2 说明至少存在一个w1,l1,h1大于w2,l2,h2
此时不满足条件 c1必定不能放在c2上

def maxHeight(cuboids):
    """
    :type cuboids: List[List[int]]
    :rtype: int
    """
    n=len(cuboids)
    for c in cuboids:
        c.sort()
    cuboids.sort(key=sum,reverse=True)
    ans = 0
    dp = [0]*n
    for i in range(n):
        dp[i] = cuboids[i][2]
        for j in range(i):
            if cuboids[i][0]<=cuboids[j][0] and cuboids[i][1]<=cuboids[j][1] and cuboids[i][2]<=cuboids[j][2]:
                dp[i] = max(dp[i],dp[j]+cuboids[i][2])
        ans = max(ans,dp[i])
    return ans



12/11 1827. 最少操作使数组递增

依次遍历 如果nums[i]<=nums[i-1] 则操作nums[i-1]-nums[i]+1次

def minOperations(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    n = len(nums)
    ans = 0
    for i in range(1,n):
        if nums[i]<=nums[i-1]:
            ans += nums[i-1]-nums[i]+1
            nums[i] = nums[i-1]+1
    return ans



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值