Student Survey Results

This is a question with a coding question:

Your school has choices, and you are tasked with coding a program that counts results.

You are given a set of votes, each of which contains a candidate and a time stamp. Given the timestamp, return the top N candidates with the most votes to this timestamp. (each vote count must occur before or at the specified time stamp)

+4
source share
3 answers

Create a Min Heap and HashMap Data data structure to solve this problem.

1. Cast each vote in the HashMap (Candidate, Votes).

2. At any time we want to find a candidate for the upper trend N, add all the HashMap keys (candidate votes) to the minimum heap with a size limit of N.

3. return the entire item from the min heap, which will return the top candidate N with votes. (since the minimum heap filters the candidate with a size limit of N).

+3
source

This is probably far from the most efficient way, but I would:

  • Create a listList containing each candidate and their corresponding number of votes (initially 0)
  • , , 1 .
  • , , n- ( ), , , .
0

, , : , 9 2016 , l, , . :

array ->0->date: 09/08/2016
         ->John Doe: 1

Since I assume that all names are known in the elections, we can simply store all the names of the candidates in another array, which we can use when we scroll through it.

The inclusion of a new vote for John Doe on another date will be registered, your array will look like this

array ->0->date: 09/08/2016
         ->John Doe: 1
      ->1->date: 11/08/2016
         ->John Doe: 1

If someone votes for another guest on an already known date, he should look like this

array ->0->date: 09/08/2016
         ->John Doe: 1
         ->Jane Doe: 1

Hope this helps. If you want help getting through this thingy array structure, don't be afraid to ask :)

0
source

Source: https://habr.com/ru/post/1650488/


All Articles