【算法】力扣第 264 场周赛

5906. 句子中的有效单词数

写的比较啰嗦了点,其实用正则一行就搞定了

return sum(bool(re.match(r'[a-z]*([a-z]-[a-z]+)?[!.,]?$', w)) for w in sc.split())

class Solution:
    def countValidWords(self, sentence: str) -> int:
        nums=[str(i) for i in range(10)]
        def func(x):
            n=len(x)
            if x.count('-')>=2:return False
            if x.count('!')>=2:return False
            if x.count('.')>=2:return False
            if x.count(',')>=2:return False
            flag1,flag2,flag3=False,False,False
            for idx,val in enumerate(x):
                if val.isalpha():
                    flag1=True
                    if flag2:
                        flag3=True
                if val=='-':
                    flag2=True
                    if not flag1:
                        return False

                if val in ('!','.', ',') and idx!=n-1:
                    return False
                if val in nums:
                    return False
            if flag2 and not flag3:return False
            return True
            
        
        res=0
        for x in sentence.split():
            if func(x):
                print(x)
                res+=1
        return res

5907. 下一个更大的数值平衡数

打表也能过,这里写的啰嗦了点,属于是没想到

  • 0

    <

    =

    n

    <

    =

    1

    0

    6

    0 <= n <= 10^6

    0<=n<=106,n实际上可以取到999999

搜的时候用一下二分

class Solution:
    def nextBeautifulNumber(self, n: int) -> int:
        def func(x):
            s=set(x)
            for k in s:
                if x.count(k)!=int(k):
                    return False
            return True
        res={1,22,122,212,221,333,1333,3133,3313,3331,4444,55555,666666,1224444}
        seq1='22333'
        seq2='14444'
        for x in permutations(seq1, 5):
            tmp=''.join(x)
            res.add(int(tmp))
        for x in permutations(seq2, 5):
            tmp=''.join(x)
            res.add(int(tmp))
        
        seq3='122333'
        seq4='224444'
        seq5='155555'
        for x in permutations(seq3,6):
            tmp=''.join(x)
            res.add(int(tmp))
        for x in permutations(seq4, 6):
            tmp=''.join(x)
            res.add(int(tmp))
        for x in permutations(seq5, 6):
            tmp=''.join(x)
            res.add(int(tmp))
                
        res=sorted(list(res))
        idx=bisect.bisect_left(res,n)
        if res[idx]==n:
            return res[idx+1]
        else:
            return res[idx]

5908. 统计最高分的节点数目

dfs轻松搞定,不过建树的过程值得注意

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def countHighestScoreNodes(self, parents: List[int]) -> int:
        n=len(parents)
        tree=[TreeNode(i) for i in range(n)]
        
        for i in range(1,n):
            a,b=tree[i],tree[parents[i]]
            if not b.left:
                b.left=a
            else:
                b.right=a
        
        nums=[None]*n
        def dfs(root):
            if not root:return 0
            l,r=0,0
            if root.left:l+=1
            if root.right:r+=1
            l+=dfs(root.left)
            r+=dfs(root.right)
            nums[root.val]=(l,r)
            return l+r
        
        dfs(tree[0])
        res=[0]*n
        for i in range(n):
            a,b=nums[i][0],nums[i][1]
            c=n-a-b-1
            if a==0:a=1
            if b==0:b=1
            if c==0:c=1
            res[i]=a*b*c
        # print(res)
        return res.count(max(res))

5909. 并行课程 III

拓扑排序,没码出来,以后得总结一下板子

这里分享一个大佬的代码👇,一起学习

class Solution:
    def minimumTime(self, n: int, relations: List[List[int]], time: List[int]) -> int:
        p = collections.defaultdict(list)
        for pc, c in relations: p[c].append(pc)
        @cache
        def get_res(c): return time[c - 1] + max([0] + [get_res(pc) for pc in p[c]])
        return max(get_res(c) for c in range(1, n + 1))

轻松一刻

刷力扣的时候,群友分享了一个测试反应速度的小游戏

当我还在老老实实地提高注意力的时候,群友已经通过预判打出非人操作:
在这里插入图片描述

难以忍受!因此我搬出来了刚学不久的selenium,开始一通操作!

from time import sleep

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


driver = webdriver.Chrome()
driver.get('https://www.arealme.com/reaction-test/cn/')
wait=WebDriverWait(driver,10)
start=wait.until(EC.presence_of_element_located((By.XPATH,'//button[@class="progress-button pure-button pure-button-primary"]')))
start.click()

sleep(3)
ActionChains(driver).move_by_offset(200, 100).click().perform()

for _ in range(5):
    green=wait.until(EC.presence_of_element_located((By.XPATH,'//div[@class="tfny-circleGreen"]')))
    green.click()

# res=wait.until(EC.presence_of_element_located((By.XPATH,'//div[@id="reactNumber"]')))
# print(res.text)

一阵骚乱之后,整个群聊又归于平静。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>