Leetcode[692] and[347] Heapq problem

本质上是一样的解决方案,利用python的heapq 进行解题

[347] Top K Frequent Elements

class Solution(object):
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        heap=[]
        dic={}
        ret=[]
        for num in nums:
            if num not in dic:
                dic[num]=1
            else:
                dic[num]+=1

        for key,val in dic.items():
            heapq.heappush(heap,(-val,key))
        
        for i in range(k):
            count,key=heapq.heappop(heap)
            ret.append(key)

        return ret

and use bucket sort

class Solution(object):
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        bucket=[[] for i in range(len(nums))]
        dic={}
        ret=[]
        for num in nums:
            if num not in dic:
                dic[num]=1
            else:
                dic[num]+=1

        for key in dic:
            bucket[dic[key]-1].append(key)


        for i in range(len(nums)-1,-1,-1):
            for j in range(len(bucket[i])):
                ret.append(bucket[i][j])
                if len(ret)==k:
                    return ret

[692] Top K Frequent Words

class Solution(object):
    def topKFrequent(self, words, k):
        """
        :type words: List[str]
        :type k: int
        :rtype: List[str]
        """
        
        dic={}
        for i in words:
            if i not in dic:
                dic[i]=1
            else:
                dic[i]+=1
            
        maxheap=[]
        for val, count in dic.items():
            heapq.heappush(maxheap,(-count,val))
            
        ret=[]
        for i in range(k):
            cout,word=heapq.heappop(maxheap)
            ret.append(word)
            
        return ret

659. Split Array into Consecutive Subsequences

Example 1:
Input: [1,2,3,3,4,5]
Output: True
Explanation:
You can split them into two consecutive subsequences :
1, 2, 3
3, 4, 5
Example 2:
Input: [1,2,3,3,4,4,5,5]
Output: True
Explanation:
You can split them into two consecutive subsequences :
1, 2, 3, 4, 5
3, 4, 5

class Solution(object):
    def isPossible(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        dic=collections.defaultdict(list)
        
        #general idea is use heapq to store the ending element and the length
        for n in nums:
            if n-1 not in dic:
                heapq.heappush(dic[n],1)
                
            else:
                l=heapq.heappop(dic[n-1])
                if not dic[n-1]:
                    del dic[n-1]
                heapq.heappush(dic[n],l+1)
                
        for key,val in dic.items():
            
            if heapq.heappop(val)<3:
                return False
            
        return True
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值