Short Ruby


  • 0

    Just check the largest two elements, which Ruby conveniently offers with nums.max(2).

    Version 1:

    def dominant_index(nums)
      a, b = nums.max(2)
      !b || a >= 2 * b ? nums.index(a) : -1
    end
    

    Version 2:

    def dominant_index(nums)
      a, b = nums.max(2) + [0]
      a >= 2 * b ? nums.index(a) : -1
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.