Эх сурвалжийг харах

Adding faster solution of 1560

Vinicius Teshima 1 долоо хоног өмнө
parent
commit
9756c35338
1 өөрчлөгдсөн 11 нэмэгдсэн , 18 устгасан
  1. 11 18
      Ruby/1560.rb

+ 11 - 18
Ruby/1560.rb

@@ -5,28 +5,21 @@
 # @param {Integer[]} rounds
 # @return {Integer[]}
 def most_visited(n, rounds)
-  expanded = []
-
-  expanded.push(rounds.shift())
-  loop do
-    nxt = rounds.shift()
-    break if nxt.nil?()
-
-    i = expanded.last()
-    loop do
-      i = i+1 > n ? 1 : i+1
-      expanded.push(i)
-      break if i == nxt
+  uses = Hash.new(0)
+  i = 0
+  while i < (rounds.size()-1)
+    x = rounds[i]
+    tgt = rounds[i+1]
+    while x != tgt
+      uses[x] += 1
+      x = x+1 > n ? 1 : x+1
     end
-
-    break if rounds.last().nil?()
+    i += 1
   end
-
-  uses = Hash.new(0)
-  expanded.each {|x| uses[x] += 1}
+  uses[rounds[-1]] += 1
 
   max_amount = 0
-  uses.each_pair {|k, v| max_amount = v > max_amount ? v : max_amount}
+  uses.each_pair {|k, v| if v > max_amount then max_amount = v end}
 
   res = []
   uses.each_pair {|k, v| if v == max_amount then res.push(k) end}