Python Two-Liner O(N) time O(1) space. Merry Christmas! 🎅


  • 0

    Self-explanatory code.

    - Yangshun

    class Solution(object):
        def dominantIndex(self, nums):
            # Time: O(n)
            # Space: O(1)
            largest = max(nums)
            return nums.index(largest) if all(largest == num or num * 2 <= largest for num in nums) else -1
    

  • 0
    def dominantIndex(self, nums):
        largest = max(nums)
        return -1 if any(num < largest < 2 * num for num in nums) else nums.index(largest)

Log in to reply
 

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