Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bug Report for top k frequent elements #3949

Closed
@da255

Description

@da255

Bug Report for https://neetcode.io/problems/two-integer-sum

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
h = {}

    for i in nums:        if i not in h:            h[i] = 0        h[i] += 1        heap = []    for i in h.keys():        heapq.heappush(heap, (h[i], i))        if len(heap) > k:            heapq.heappop(heap)    res = []    for i in range(k):        res.append(heapq.heappop(heap)[1])    return res

i ran my code, and it says i got the wrong answer for this test case

nums=[1,2,3,4,5,6,6,7,8]
k=2

but when i run my code on the leetcode website, it successfully submitted, and when i try that test case, it says its an invalid test case. I even tried your heap solution and sorted solution and its the same problem

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp