We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Frequency Queries
  2. Discussions

Frequency Queries

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • naomi_nguyen7
    5 years ago+ 27 comments

    My Python solution. Let me know if there's a shorter way:

    `def freqQuery(queries):

    freq = Counter()
    
    cnt = Counter()
    
    arr = []
    
    for q in queries:
        if q[0]==1:
            cnt[freq[q[1]]]-=1
            freq[q[1]]+=1
            cnt[freq[q[1]]]+=1
    
        elif q[0]==2:
            if freq[q[1]]>0:
                cnt[freq[q[1]]]-=1
                freq[q[1]]-=1
                cnt[freq[q[1]]]+=1
    
        else:
            if cnt[q[1]]>0:
                arr.append(1)
            else:
                arr.append(0)
    
    return arr`
    
    60|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy