↓Skip to main content
  1. LeetCode/

LeetCode 747: Largest Number At Least Twice of Others

·2 mins· ·
LeetCode Easy Array Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-03

  • Submissions: Runtime Error → Wrong Answer → Runtime Error ×2 → Wrong Answer → Accepted → Runtime Error ×2 → Wrong Answer → Runtime Error → Accepted → Runtime Error → Wrong Answer ×2 → Runtime Error → Accepted ×2

Approach
#

If the largest is at least twice the second largest, it is at least twice every other value (Greedy Template 1). In one pass track the largest, its index and the second largest: a new maximum demotes the old one to second; otherwise compare with the second. Test max >= 2 * second without division, so a second largest of 0 is fine.

My Attempts
#

This took 17 submissions:

  • At first I divided, max_ / max(nums), raising ZeroDivisionError when the others were 0, and special-cased set(nums) == {0} to pass.
  • Rewriting as one pass for the largest and second largest, the second check was an if rather than an elif, so a freshly updated maximum also became the second largest: [7, 0] returned -1 instead of 0. Other versions returned the value instead of the index or still divided. elif with max_ >= 2 * sec_max passed.

Solution
#

class Solution:
    def dominantIndex(self, nums: List[int]) -> int:
        first = second = -1
        idx = -1
        for i, x in enumerate(nums):
            if x > first:
                first, second, idx = x, first, i
            elif x > second:
                second = x
        return idx if first >= 2 * second else -1

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Compare by multiplying; division fails on 0.
  • Update the second largest in an elif, or the maximum becomes its own runner-up.
  • Return the index, not the value.

Interview Explanation
#

If the largest is at least twice the runner-up it beats everyone, so one pass records the largest, its index and the runner-up, and I compare by multiplication to avoid dividing by zero. Time O(n), space O(1).

Related

LeetCode 628: Maximum Product of Three Numbers
·1 min
LeetCode Easy Array Math Sorting
LeetCode 628 note: after sorting there are only two candidates: the three largest, or the two smallest (negatives) times the largest
LeetCode 645: Set Mismatch
·2 mins
LeetCode Easy Array Hash-Table Bit-Manipulation Sorting
LeetCode 645 note: use sums: missing = sum of 1..n - sum of the set, duplicate = array sum - sum of the set
LeetCode 561: Array Partition
·1 min
LeetCode Easy Array Greedy Sorting Counting-Sort
LeetCode 561 note: sort and pair neighbors; each pair’s minimum is the element at an even index, so sum those
LeetCode 414: Third Maximum Number
·1 min
LeetCode Easy Array Sorting
LeetCode 414 note: track the three largest distinct values, skipping repeats, and return the maximum if fewer than three distinct values exist
LeetCode 455: Assign Cookies
·2 mins
LeetCode Easy Array Two-Pointers Greedy Sorting
LeetCode 455 note: sort children and cookies, satisfying the least greedy child with the smallest cookie that suffices; each pointer walks once
LeetCode 217: Contains Duplicate
·1 min
LeetCode Easy Array Hash-Table Sorting
LeetCode 217 note: if converting to a set shortens the array, there is a duplicate