LeetCode 每日一题 2023/7/24-2023/7/30

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


目录

      • 7/24 771. 宝石与石头
      • 7/25 2208. 将数组和减半的最少操作次数
      • 7/26 2569. 更新数组后处理求和查询
      • 7/27 2500. 删除每行中的最大值
      • 7/28 2050. 并行课程 III
      • 7/29 141. 环形链表
      • 7/30 142. 环形链表 II


7/24 771. 宝石与石头

将宝石类型放入set中
一次判断石头中宝石个数

def numJewelsInStones(jewels, stones):
    """
    :type jewels: str
    :type stones: str
    :rtype: int
    """
    j = set(jewels)
    ans = 0
    for s in stones:
        if s in j:
            ans+=1
    return ans



7/25 2208. 将数组和减半的最少操作次数

大顶堆记录当前最大值 每次取最大值减半

def halveArray(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    import heapq
    target = sum(nums)*1.0/2
    
    l = []
    for num in nums:
        heapq.heappush(l,-num)
        
    cur = 0
    ans = 0
    while True:
        v = -heapq.heappop(l)
        cur +=v*1.0/2
        ans+=1
        if cur>=target:
            break
        heapq.heappush(l,-v*1.0/2)
    return ans



7/26 2569. 更新数组后处理求和查询

操作1是将nums1中[l,r]的数反转
操作2是将nums2的和加上p*sum(nums1)
操作3是求nums2的和
只要考虑nums1的变化即可
线段树维护nums1区间
l,r左右端点 s区间和 lazy懒标记

class Node:
    def __init__(self):
        self.l=self.r=0
        self.s = 0
        self.lazy = 0
class SegmentTree:
    def __init__(self,nums):
        self.nums = nums
        n = len(nums)
        self.tr = [Node() for _ in range(n<<2)]
        self.build(1,1,n)
    
    def build(self,u,l,r):
        self.tr[u].l,self.tr[u].r=l,r
        if l==r:
            self.tr[u].s = self.nums[l-1]
            return
        mid = (l+r)>>1
        self.build(u<<1,l,mid)
        self.build(u<<1|1,mid+1,r)
        self.pushup(u)
    
    def modify(self,u,l,r):
        if self.tr[u].l>=l and self.tr[u].r<=r:
            self.tr[u].lazy^=1
            self.tr[u].s = self.tr[u].r-self.tr[u].l+1-self.tr[u].s
            return
        self.pushdown(u)
        mid = (self.tr[u].l+self.tr[u].r)>>1
        if l<=mid:
            self.modify(u<<1,l,r)
        if r>mid:
            self.modify(u<<1|1,l,r)
        self.pushup(u)
        
    def query(self,u,l,r):
        if self.tr[u].l>=l and self.tr[u].r<=r:
            return self.tr[u].s
        self.pushdown(u)
        mid = (self.tr[u].l+self.tr[u].r)>>1
        ans = 0
        if l<=mid:
            ans +=self.query(u<<1,l,r)
        if r>mid:
            ans +=self.query(u<<1|1,l,r)
        return ans
    
    def pushup(self,u):
        self.tr[u].s = self.tr[u<<1].s+self.tr[u<<1|1].s
    
    def pushdown(self,u):
        if self.tr[u].lazy:
            mid = (self.tr[u].l+self.tr[u].r)>>1
            self.tr[u<<1].s = mid-self.tr[u].l+1-self.tr[u<<1].s
            self.tr[u<<1].lazy^=1
            self.tr[u<<1|1].s = self.tr[u].r-mid-self.tr[u<<1|1].s
            self.tr[u<<1|1].lazy^=1
            self.tr[u].lazy^=1
            
def handleQuery(nums1, nums2, queries):
    """
    :type nums1: List[int]
    :type nums2: List[int]
    :type queries: List[List[int]]
    :rtype: List[int]
    """
    tr = SegmentTree(nums1)
    s = sum(nums2)
    ans = []
    for op,l,r in queries:
        if op==1:
            tr.modify(1,l+1,r+1)
        elif op==2:
            s+=l*tr.query(1,1,len(nums1))
        else:
            ans.append(s)
    return ans



7/27 2500. 删除每行中的最大值

每一行从小到大排序
再取每一列的最大值

def deleteGreatestValue(grid):
    """
    :type grid: List[List[int]]
    :rtype: int
    """
    cur =[]
    for l in grid:
        cur.append(sorted(l))
    return sum([max([cur[i][j] for i in range(len(cur))]) for j in range(len(grid[0]))])




7/28 2050. 并行课程 III

m[i]记录课程i为多少课程的前置课程
ind[i]记录课程i有几个前置课程
t[i]记录课程i的最早完成时间

def minimumTime(n, relations, time):
    """
    :type n: int
    :type relations: List[List[int]]
    :type time: List[int]
    :rtype: int
    """
    from collections import defaultdict
    m = defaultdict(list)
    ind=[0]*n
    for pre,nxt in relations:
        m[pre-1].append(nxt-1)
        ind[nxt-1]+=1
    l = []
    t=[0]*n
    ans = 0
    for i in range(n):
        if ind[i]==0:
            l.append(i)
            t[i]=time[i]
            ans = max(ans,t[i])
    while l:
        tmp = []
        for i in l:
            for j in m[i]:
                t[j] = max(t[j],t[i]+time[j])
                ans = max(ans,t[j])
                ind[j]-=1
                if ind[j]==0:
                    tmp.append(j)
        l=tmp
    return ans



7/29 141. 环形链表

快慢指针

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None
def hasCycle(head):
    """
    :type head: ListNode
    :rtype: bool
    """
    if head==None or head.next==None:
        return False
    slow = head
    fast = head.next
    
    while(slow!=fast):
        if fast==None or fast.next==None:
            return False
        slow=slow.next
        fast=fast.next.next
    return True



7/30 142. 环形链表 II

快慢指针

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None
def detectCycle(head):
    """
    :type head: ListNode
    :rtype: ListNode
    """
    if head==None:
        return head
    node = ListNode()
    node.next = head
    fast,slow=node,node
    while fast==node or fast!=slow:
        slow = slow.next
        if fast.next==None or fast.next.next==None:
            return None
        fast = fast.next.next
    fast = node
    while fast!=slow:
        fast = fast.next
        slow = slow.next
    return slow



你可能感兴趣的:(Exercise,leetcode,算法)