1560.rb 851 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 = Array.new(n+1, 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. i = 1
  20. while i < uses.size()
  21. if uses[i] > max_amount
  22. max_amount = uses[i]
  23. end
  24. i += 1
  25. end
  26. res = []
  27. i = 1
  28. while i < uses.size()
  29. if uses[i] == max_amount
  30. res.push(i)
  31. end
  32. i += 1
  33. end
  34. res
  35. end
  36. def main()
  37. puts String(most_visited(4, [1, 3, 1, 2]) == [1,2])
  38. puts String(most_visited(2, [2,1,2,1,2,1,2,1,2]) == [2])
  39. puts String(most_visited(7, [1,3,5,7]) == [1,2,3,4,5,6,7])
  40. end
  41. if __FILE__ == $0
  42. main()
  43. end