栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

鍙屾墸姣旇禌瑙嗛(鍔涙墸鍛ㄨ禌鏃堕棿)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

鍙屾墸姣旇禌瑙嗛(鍔涙墸鍛ㄨ禌鏃堕棿)

第一次AK,心情十分预约!

T1

class Solution:
    def divideArray(self, nums: List[int]) -> bool:
        dic={}
        for i in range(len(nums)):
            if nums[i] in dic:
                dic[nums[i]]+=1
            else:
                dic[nums[i]]=1
        for key in dic.keys():
            if dic[key]%2!=0:
                return False
        return True
        

T2

class Solution:
    def maximumSubsequenceCount(self, text: str, pattern: str) -> int:
        if pattern[0]==pattern[1]:
            count=1
            for t in text:
                if t==pattern[0]:
                    count+=1
            return count*(count-1)//2
        else:
            count1,count2=0,0
            ans=0
            for t in text:
                if t==pattern[0]:
                    count1+=1
                if t==pattern[1]:
                    count2+=1
                    ans+=count1
            ans+=max(count1,count2)
            return ans
                
                

T3

from heapq import *
class Solution:
    def halveArray(self, nums: List[int]) -> int:
        suma=sum(nums)
        half=suma/2
        nums.sort(reverse=True)
        
        heap,lth=[],len(nums)
        for i in range(lth):
            heappush(heap,-nums[i])
        cnt,total=0,0
        while total 

T4

class Solution:
    def minimumWhiteTiles(self, floor: str, numCarpets: int, carpetLen: int) -> int:
        #暴力
        # def count(sta,res):
        #     ans=0
        #     for i in range(sta,sta+carpetLen):
        #         if res[i]=="1":
        #             ans+=1
        #     return ans
        # lth=len(floor)
        # res=[c for c in floor]
        # while numCarpets>0:
        #     index=0
        #     maxnum=0
        #     for i in range(lth-carpetLen+1):
        #         tmp=count(i,res)
        #         if tmp>maxnum:
        #             maxnum=tmp
        #             index=i
        #     for i in range(index,index+carpetLen):
        #         if res[i]=="1":
        #             res[i]="0"
        #     numCarpets-=1
        # ans=0
        # for i in range(lth):
        #     if res[i]=="1":
        #         ans+=1
        # return ans
        
        n=len(floor)
        tree=[0]*(n+1)
        res=[c for c in floor]
        res.insert(0,0)
        if numCarpets*carpetLen>=n:
            return 0
       #树状数组优化
        def lowbit(x):
            return x&(-x)
        def add(index,val):
            while index<=n:
                tree[index]+=val
                index+=lowbit(index)
        def query(index):
            ans=0
            while index:
                ans+=tree[index]
                index-=lowbit(index)
            return ans
        #建树
        
        for i in range(1,n+1):
            if res[i]=="1":
                add(i,1)
        while numCarpets>0:
            maxnum,sta=0,0
            for i in range(carpetLen,n+1):
                if res[i]=="1":
                    t=query(i)-query(i-carpetLen)
                    if t>maxnum:
                        maxnum=t
                        sta=i
            if sta!=0:
                for i in range(sta-carpetLen+1,sta+1):
                    if res[i]=="1":
                        res[i]="0"
                        add(i,-1)
            else:
                return 0
            numCarpets-=1
        ans=0
        for i in range(1,n+1):
            if res[i]=="1":
                ans+=1
        return ans
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号