LeetCode 每日一题 2023/7/3-2023/7/9

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




7/3 445. 两数相加 II

将链表中的数值存入数组
倒序相加

class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
def addTwoNumbers(l1, l2):
    """
    :type l1: ListNode
    :type l2: ListNode
    :rtype: ListNode
    """
    li1,li2=[],[]
    while l1:
        li1.append(l1.val)
        l1=l1.next
    while l2:
        li2.append(l2.val)
        l2=l2.next
    n1,n2=len(li1)-1,len(li2)-1
    pre = ListNode()
    v,ad = 0,0
    while n1>=0 or n2>=0:
        if n1>=0 and n2>=0:
            v = li1[n1]+li2[n2]+ad
            ad = v//10
            v = v%10
            n1-=1
            n2-=1
        elif n1>=0:
            v = li1[n1]+ad
            ad = v//10
            v = v%10
            n1-=1
        else:
            v = li2[n2]+ad
            ad = v//10
            v = v%10
            n2-=1
        cur = ListNode(v)
        cur.next = pre.next
        pre.next = cur
    if ad>0:
        cur = ListNode(ad)
        cur.next = pre.next
        pre.next = cur
    return pre.next



7/4 2679. 矩阵中的和

将每一行排序
取每一列的最大值 求和

def matrixSum(nums):
    """
    :type nums: List[List[int]]
    :rtype: int
    """
    cur = []
    for l in nums:
        cur.append(sorted(l))
    n = len(cur[0])
    ans = 0
    for i in range(n):
        v = max([cur[j][i] for j in range(len(nums))])
        ans += v
    return ans




7/5 2600. K 件物品的最大和

先取1 再取0 最后取-1

def kItemsWithMaximumSum(numOnes, numZeros, numNegOnes, k):
    """
    :type numOnes: int
    :type numZeros: int
    :type numNegOnes: int
    :type k: int
    :rtype: int
    """
    if k<=numOnes+numZeros:
        return min(k,numOnes)
    else:
        return numOnes-(k-numOnes-numZeros)




7/6 2178. 拆分成最多数目的正偶数之和

如果是奇数必定不可以
为了使得尽可能多 尽量使用小的偶数 从2开始


def maximumEvenSplit(finalSum):
    """
    :type finalSum: int
    :rtype: List[int]
    """
    if finalSum%2==1:
        return []
    cur = 2
    ans = []
    while cur<=finalSum:
        ans.append(cur)
        finalSum-=cur
        cur+=2
    ans[-1]+=finalSum
    return ans


7/7 2532. 过桥的时间

按工人效率从高到低排序 编号越大效率越低
waitl,waitr 大根堆 记录左右岸等待工人的编号
workl,workr 小根堆 记录左右岸工人处理箱子时间及编号
初始所有工人在waitl cur记录当前时间
模拟
判断workl是否有工人放好箱子 有将工人放入waitl中等待
同理workr有工人放好箱子 将工人放入waitr中
判断当前是否有工人在左右两岸等待 如果不存在 则将cur更新为下一个工人放好箱子的时间
如果右岸有人等待,取出一个工人更新cur时间 如果所有工人都已过了右岸 则返回cur
如果左岸有人等待,取出一个工人更新cur时间,放入workr 箱子数量减一

def findCrossingTime(n, k, time):
    """
    :type n: int
    :type k: int
    :type time: List[List[int]]
    :rtype: int
    """
    import heapq
    time.sort(key=lambda x:x[0]+x[2])
    cur = 0
    waitl,waitr=[],[]
    workl,workr=[],[]
    for i in range(k):
        heapq.heappush(waitl,-i)
    while True:
        while workl:
            t,i = workl[0]
            if t>cur:
                break
            heapq.heappop(workl)
            heapq.heappush(waitl,-i)
        while workr:
            t,i = workr[0]
            if t>cur:
                break
            heapq.heappop(workr)
            heapq.heappush(waitr,-i)
        
        ltr = n>0 and waitl
        rtl = len(waitr)>0
        if not ltr and not rtl:
            nxt = float("inf")
            if workl:
                nxt = min(nxt,workl[0][0])
            if workr:
                nxt = min(nxt,workr[0][0])
            cur = nxt
            continue
        if rtl:
            i = -heapq.heappop(waitr)
            cur += time[i][2]
            if n==0 and not waitr and not workr:
                return cur
            heapq.heappush(workl,(cur+time[i][3],i))
        else:
            i = -heapq.heappop(waitl)
            cur+=time[i][0]
            n-=1
            heapq.heappush(workr,(cur+time[i][1],i))



7/8 167. 两数之和 II - 输入有序数组

双指针 数组为非递减
将数组中最大值最小值相加 及最后一个数和第一个数
如果小于目标值 则将右边指针左移
如果大于目标值 则将左边指针右移
直到等于目标值

def twoSum(numbers, target):
    """
    :type numbers: List[int]
    :type target: int
    :rtype: List[int]
    """
    l,r=0,len(numbers)-1
    while l<r:
        cur = numbers[l]+numbers[r]
        if cur==target:
            return [l+1,r+1]
        elif cur<target:
            l+=1
        else:
            r-=1



7/9 15. 三数之和

neg,pos统计负数和正数
选两个数 确定第三个数

def threeSum(nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    ret = []
    neg = []
    pos = []
    dic = {}
    zeronum = 0
    for i in nums:
        dic[i] = dic.get(i,0) + 1
        if i == 0:
            zeronum += 1
        elif i<0:
            neg.append(i)
        else:
            pos.append(i)
    
    neg = list(set(neg))
    pos = list(set(pos))
    if zeronum > 2:
        ret.append([0,0,0])
        
    for num1 in neg:
        for num2 in pos:
            s = -(num1+num2)
            if s in dic:
                if s in (num1,num2) and dic[s]>1:
                    ret.append([num1,s,num2])
                elif s>num1 and s<num2:
                    ret.append([num1,s,num2])
    return ret



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值