• Weekly Contest 309


    Weekly Contest 309

    Problem A

    Check Distances Between Same Letters

    思路

    水题,遍历数组然后check一下就行

    代码

        class Solution:
        def checkDistances(self, s: str, distance: List[int]) -> bool:
            
            for i in range(len(s)):
                print(i)
                d =  distance[ord(s[i])-ord('a')]
                # print(s[i],s[i+d+1])
                if i+d+1>=len(s) or s[i]!=s[i+d+1]:
                    return False
                distance[ord(s[i])-ord('a')] = -1
            return True
    

    Problem B

    Number of Ways to Reach a Position After Exactly k Steps

    思路

    组合数推理,做的时候想复杂了,本质上是求k步中向左走x步(或者是向右走y步,x+y=k)的组合数,组合数取模要用到费马小定理(python 有comb)

    代码

        class Solution:
          def numberOfWays(self, a, b, k):
            if (a - b - k) % 2: return 0
            return comb(k, (b - a + k) // 2) % (10 ** 9 + 7)
    

    Problem C

    Longest Nice Subarray

    思路

    求一个连续的子序列,用滑动窗口去解决,任意两个数“与”为0,就是任意两个数二进制中不能出现同一位为1的情况。

    代码

    class Solution:
        def longestNiceSubarray(self, nums: List[int]) -> int:
            self.D = 0
            i ,j=0,1
            self.check(nums[i])
            l = len(nums)
            ans = 1
            while(j<l):
                print(i)
                if self.check(nums[j]) is False:
                    # print("UPDATA")
                    ans = max(ans,j-i)
                    while(i<j and self.D & nums[j]!=0):
                        self.D-=nums[i]
                        # print(self.D)
                        i+=1
                    self.D+=nums[j]
                    
                j+=1
                # print(i,j,self.D)
            ans = max(ans,j-i)
            return ans
        def check(self,x):
            if x & self.D == 0:
                self.D = self.D+x
                return True
            else:
                
                return False
    

    Problem D

    Meeting Rooms III

    思路

    优先队列(python里面没有,只有最小堆-heap),需要特殊处理的是某些meeting到时有好几个room是空的,所以要找这些room里面最小的

    代码

    class Solution:
        def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
            L = []
            t = []
            meetings.sort()
            # print(meetings)
            for i in range(n):
                L.append([0,i])
                t.append(0)
            heapq.heapify(L)
            for item in meetings:
                temp = []
                while(len(L)!=0):
                    
                    _ = heapq.heappop(L)
                    # print(_)
                    if _[0]<=item[0]:
                        temp.append(_)
                    else:
                        heapq.heappush(L,_)
                        break
                if len(temp)==0:
                    _ = heapq.heappop(L)
                    # print(item,_[1])
                    t[_[1]]+=1
                    _[0] = max(_[0],item[0])+item[1]-item[0]
                    heapq.heappush(L,_)
                else:
                    l,_ = 0,temp[0][1]
                    for i in range(1,len(temp)):
                        if temp[i][1]<_:
                            _ = temp[i][1]
                            l = i
                    # print(item,_)
                    t[_]+=1
                    temp[l][0] = max(temp[l][0],item[0])+item[1]-item[0]
                    for item in temp:
                        heapq.heappush(L,item)
                    
            M,ans= t[0],0
            # print(t)
            for i in range(1,n):
                if t[i]>M:
                    M= t[i]
                    ans = i
            return ans
    

    总结

    真菜啊

  • 相关阅读:
    EasyUI问题小结(不定期更新·······)
    windows服务与前台交互
    C#捕获Windows窗体控件
    C#操作AD域中计算机
    远程桌面 Rdp文件的生成
    正则匹配的例子
    Nodejs中npm install 命令的问题
    Windows下使用curl命令
    关于PostmanURL中不能传递中文的问题
    MyBatis_Study_004(动态代理)
  • 原文地址:https://www.cnblogs.com/baihualiaoluan/p/16687306.html
Copyright © 2020-2023  润新知