You are given an integer array nums and an integer k.
An integer x is almost missing from nums if x appears in exactly one subarray of size k within nums.
Return the largest almost missing integer from nums. If no such integer exists, return -1.
A subarray is a contiguous sequence of elements within an array.

-> 여기서 `almost missing`이라는 말이 나오는데, 뒤 문장을 보면 배열 nums의 크기가 k인 부분 배열 중에 딱 한 번만 나오는 수를 x라고 한다.

-> 부분 배열은 연속적이다.

 

class Solution {
    public int largestInteger(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        Set<Integer> set;
        int max = -1;

        for (int i = 0; i <= nums.length - k; i++) {
            set = new HashSet<>();
            for (int j = i; j < i + k; j++) set.add(nums[j]);
            for (int num : set) map.put(num, map.getOrDefault(num, 0) + 1);
        }

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) max = Math.max(max, entry.getKey());
        }

        return max;
    }
}

+ Recent posts