diff --git a/CLAUDE.md b/CLAUDE.md index 801d2c7..0d2b9db 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -104,7 +104,7 @@ Follow: - LeetCode link - Link(s) to each `solution*.md` variant (`main` for `solution.md`, or the suffix for `solution-.md`) -The index currently lists **198** problems. Regenerate the tables from the repo if many entries change at once. +The index currently lists **199** problems. Regenerate the tables from the repo if many entries change at once. ## Common patterns in this repo diff --git a/Easy/3471.Find-the-Largest-Almost-Missing-Integer/description.md b/Easy/3471.Find-the-Largest-Almost-Missing-Integer/description.md new file mode 100644 index 0000000..595d7d3 --- /dev/null +++ b/Easy/3471.Find-the-Largest-Almost-Missing-Integer/description.md @@ -0,0 +1,54 @@ +# 3471. Find the Largest Almost Missing Integer + +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. + +## Example 1 + +```text +Input: nums = [3,9,2,1,7], k = 3 +Output: 7 +Explanation: +- 1 appears in 2 subarrays of size 3: [9, 2, 1] and [2, 1, 7]. +- 2 appears in 3 subarrays of size 3: [3, 9, 2], [9, 2, 1], [2, 1, 7]. +- 3 appears in 1 subarray of size 3: [3, 9, 2]. +- 7 appears in 1 subarray of size 3: [2, 1, 7]. +- 9 appears in 2 subarrays of size 3: [3, 9, 2], and [9, 2, 1]. +We return 7 since it is the largest integer that appears in exactly one subarray of size k. +``` + +## Example 2 + +```text +Input: nums = [3,9,7,2,1,7], k = 4 +Output: 3 +Explanation: +- 1 appears in 2 subarrays of size 4: [9, 7, 2, 1], [7, 2, 1, 7]. +- 2 appears in 3 subarrays of size 4: [3, 9, 7, 2], [9, 7, 2, 1], [7, 2, 1, 7]. +- 3 appears in 1 subarray of size 4: [3, 9, 7, 2]. +- 7 appears in 3 subarrays of size 4: [3, 9, 7, 2], [9, 7, 2, 1], [7, 2, 1, 7]. +- 9 appears in 2 subarrays of size 4: [3, 9, 7, 2], [9, 7, 2, 1]. +We return 3 since it is the largest and only integer that appears in exactly one subarray of size k. +``` + +## Example 3 + +```text +Input: nums = [0,0], k = 1 +Output: -1 +Explanation: +There is no integer that appears in only one subarray of size 1. +``` + +## Constraints + +- `1 <= nums.length <= 50` +- `0 <= nums[i] <= 50` +- `1 <= k <= nums.length` diff --git a/Easy/3471.Find-the-Largest-Almost-Missing-Integer/solution.md b/Easy/3471.Find-the-Largest-Almost-Missing-Integer/solution.md new file mode 100644 index 0000000..6560f90 --- /dev/null +++ b/Easy/3471.Find-the-Largest-Almost-Missing-Integer/solution.md @@ -0,0 +1,101 @@ +# Intuition + +An integer `x` is "almost missing" if it lies in **exactly one** window of size `k`. +How many size-`k` windows a position belongs to depends only on where it sits, so we +can reason by cases instead of enumerating every window: + +- If `k == n`, there is a single window (the whole array), so *every* value appears + in exactly one window — the answer is just the maximum element. +- If `k == 1`, each element is its own window, so `x` appears in exactly one window + iff it is globally unique (frequency 1). Return the largest such value. +- If `1 < k < n`, only the two endpoints `nums[0]` and `nums[n-1]` are covered by + exactly one window; every interior position is covered by at least two. An + endpoint qualifies only if its value is globally unique. Return the larger + qualifying endpoint, or `-1`. + +# Approach: Frequency Count + Case Analysis + +1. Handle `k == n` directly by returning the maximum element. +2. Otherwise build a frequency table (`nums[i] <= 50`, so a fixed 51-size array + works). +3. For `k == 1`, scan values high to low and return the first with frequency 1. +4. For `1 < k < n`, consider `nums[0]` and `nums[n-1]`; keep whichever is larger + among those with frequency 1, else `-1`. + +# Complexity + +- Time complexity: $$O(n + M)$$, where `n` is the array length and `M = 51` is the + value range scanned — effectively $$O(n)$$. +- Space complexity: $$O(1)$$ — a fixed 51-element frequency array. + +# Code + +## Go + +```go +func largestInteger(nums []int, k int) int { + n := len(nums) + if k == n { + res := -1 + for _, num := range nums { + res = max(res, num) + } + return res + } + freq := [51]int{} + for _, num := range nums { + freq[num]++ + } + if k == 1 { + for i := 50; i >= 0; i-- { + if freq[i] == 1 { + return i + } + } + return -1 + } + res := -1 + if freq[nums[0]] == 1 { + res = max(res, nums[0]) + } + if freq[nums[n-1]] == 1 { + res = max(res, nums[n-1]) + } + return res +} +``` + +## Rust + +```rust +impl Solution { + pub fn largest_integer(nums: Vec, k: i32) -> i32 { + let n = nums.len(); + let k = k as usize; + if k == n { + return nums.into_iter().max().unwrap_or(-1); + }; + let mut freq = [0; 51]; + for &num in &nums { + freq[num as usize] += 1; + } + if k == 1 { + return freq + .iter() + .enumerate() + .rev() + .find_map(|(i, &c)| if c == 1 { Some(i as i32) } else { None }) + .unwrap_or(-1); + } + let mut res = -1; + let (first, last) = (nums[0], nums[n - 1]); + if freq[first as usize] == 1 { + res = res.max(first); + } + if freq[last as usize] == 1 { + res = res.max(last); + } + res + } +} +``` diff --git a/README.md b/README.md index a506cd6..ce99462 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ Easy/350.Intersection-of-Two-Arrays-II/ ## Solutions index -Total: **198** problems with at least one solution file. +Total: **199** problems with at least one solution file. Solution links use variant names when multiple approaches or languages exist (`main` = `solution.md`, others = `solution-.md`). -### Easy (49) +### Easy (50) | Problem | LeetCode | Solution | | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | @@ -71,6 +71,7 @@ Solution links use variant names when multiple approaches or languages exist (`m | 3216. Lexicographically Smallest String After a Swap | [Link](https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap/) | [main](Easy/3216.Lexicographically-Smallest-String-After-a-Swap/solution.md) | | 3314. Construct the Minimum Bitwise Array I | [Link](https://leetcode.com/problems/construct-the-minimum-bitwise-array-i/) | [main](Easy/3314.Construct-the-Minimum-Bitwise-Array-I/solution.md) | | 3375. Minimum Operations to Make Array Values Equal to k | [Link](https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k/) | [main](Easy/3375.Minimum-Operations-to-Make-Array-Values-Equal-to-k/solution.md) | +| 3471. Find the Largest Almost Missing Integer | [Link](https://leetcode.com/problems/find-the-largest-almost-missing-integer/) | [main](Easy/3471.Find-the-Largest-Almost-Missing-Integer/solution.md) | | 3536. Maximum Product of Two Digits | [Link](https://leetcode.com/problems/maximum-product-of-two-digits/) | [main](Easy/3536.Maximum-Product-of-Two-Digits/solution.md) | | 3637. Trionic Array I | [Link](https://leetcode.com/problems/trionic-array-i/) | [main](Easy/3637.Trionic-Array-I/solution.md) | | 3658. GCD of Odd and Even Sums | [Link](https://leetcode.com/problems/gcd-of-odd-and-even-sums/) | [main](Easy/3658.GCD-of-Odd-and-Even-Sums/solution.md) | diff --git a/SUMMARY.md b/SUMMARY.md index 87720cf..ab2a67a 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -53,6 +53,7 @@ * [3216. Lexicographically Smallest String After a Swap](Easy/3216.Lexicographically-Smallest-String-After-a-Swap/solution.md) * [3314. Construct the Minimum Bitwise Array I](Easy/3314.Construct-the-Minimum-Bitwise-Array-I/solution.md) * [3375. Minimum Operations to Make Array Values Equal to k](Easy/3375.Minimum-Operations-to-Make-Array-Values-Equal-to-k/solution.md) +* [3471. Find the Largest Almost Missing Integer](Easy/3471.Find-the-Largest-Almost-Missing-Integer/solution.md) * [3536. Maximum Product of Two Digits](Easy/3536.Maximum-Product-of-Two-Digits/solution.md) * [3637. Trionic Array I](Easy/3637.Trionic-Array-I/solution.md) * [3658. GCD of Odd and Even Sums](Easy/3658.GCD-of-Odd-and-Even-Sums/solution.md)