Browse Source

Adding even faster solution for 1560

Vinicius Teshima 1 tuần trước cách đây
mục cha
commit
836b2ab82b
1 tập tin đã thay đổi với 18 bổ sung4 xóa
  1. 18 4
      Ruby/1560.rb

+ 18 - 4
Ruby/1560.rb

@@ -5,7 +5,7 @@
 # @param {Integer[]} rounds
 # @return {Integer[]}
 def most_visited(n, rounds)
-  uses = Hash.new(0)
+  uses = Array.new(n+1, 0)
   i = 0
   while i < (rounds.size()-1)
     x = rounds[i]
@@ -19,11 +19,25 @@ def most_visited(n, rounds)
   uses[rounds[-1]] += 1
 
   max_amount = 0
-  uses.each_pair {|k, v| if v > max_amount then max_amount = v end}
+
+  i = 1
+  while i < uses.size()
+    if uses[i] > max_amount
+      max_amount = uses[i]
+    end
+    i += 1
+  end
 
   res = []
-  uses.each_pair {|k, v| if v == max_amount then res.push(k) end}
-  res.sort()
+
+  i = 1
+  while i < uses.size()
+    if uses[i] == max_amount
+      res.push(i)
+    end
+    i += 1
+  end
+  res
 end