Skip to content
LC-3471 Easy LeetCode

3471. Find the Largest Almost Missing Integer

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 37% Topics: Array, Hash Table
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

import collections


# freq table
class Solution(object):
    def largestInteger(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        if k == len(nums):
            return max(nums)
        cnt = collections.defaultdict(int)
        for x in nums:
            cnt[x] += 1
        if k == 1:
            return max(x for x, v in cnt.iteritems() if v == 1) if any(v == 1 for v in cnt.itervalues()) else -1
        result = -1
        if cnt[nums[0]] == 1:
            result = max(result, nums[0])
        if cnt[nums[-1]] == 1:
            result = max(result, nums[-1])
        return result

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions