1560.rb 768 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # 1560. Most Visited Sector in a Circular Track
  2. # @param {Integer} n
  3. # @param {Integer[]} rounds
  4. # @return {Integer[]}
  5. def most_visited(n, rounds)
  6. uses = Hash.new(0)
  7. i = 0
  8. while i < (rounds.size()-1)
  9. x = rounds[i]
  10. tgt = rounds[i+1]
  11. while x != tgt
  12. uses[x] += 1
  13. x = x+1 > n ? 1 : x+1
  14. end
  15. i += 1
  16. end
  17. uses[rounds[-1]] += 1
  18. max_amount = 0
  19. uses.each_pair {|k, v| if v > max_amount then max_amount = v end}
  20. res = []
  21. uses.each_pair {|k, v| if v == max_amount then res.push(k) end}
  22. res.sort()
  23. end
  24. def main()
  25. puts String(most_visited(4, [1, 3, 1, 2]) == [1,2])
  26. puts String(most_visited(2, [2,1,2,1,2,1,2,1,2]) == [2])
  27. puts String(most_visited(7, [1,3,5,7]) == [1,2,3,4,5,6,7])
  28. end
  29. if __FILE__ == $0
  30. main()
  31. end