Date: 2026-06-06
Time: 15:57
count-elements-with-strictly-smaller-and-greater-elements/solution.pyThis file solves LeetCode 2148: Count Elements With Strictly Smaller and Greater Elements. It's a standalone solution module following the repo's convention of one problem per directory, each with a solution.py exporting a single function.
min_moves(nums: list[int]) -> int — The sole public function. Despite the name (which matches the LeetCode class method naming), it counts how many elements in nums are strictly between the global minimum and maximum. An element qualifies if and only if min(nums) < x < max(nums).
The contract is simple: pass a list of integers, get back an integer count. The function is pure — no mutation, no side effects.
minval and maxval once (O(n) each), then makes a single pass with a generator expression to count qualifying elements. This avoids recomputing min/max per element.sum(): A standard Python idiom that avoids materializing an intermediate list. Each 1 is yielded lazily and accumulated.Solution class as LeetCode does. Test files import the function directly.Imports: None — pure stdlib, no external packages.
Imported by: The testsolution.py in the same directory, plus hundreds of other test files across the repo. That sprawling importedby list is an artifact of the test harness structure (likely a shared conftest or test runner importing all solution modules), not a sign that other solutions depend on this logic.
1. min(nums) scans the list → min_val
2. max(nums) scans the list → max_val
3. Generator iterates nums, yields 1 for each x where minval < x < maxval
4. sum() accumulates the count
5. Returns the integer count
Total: three linear passes over nums. Time complexity is O(n), space complexity is O(1) (the generator doesn't allocate a list).
minval == maxval, so the strict inequality minval < x < maxval is never satisfied → returns 0. This correctly handles the edge case.< operators.None. Calling min() or max() on an empty list raises ValueError. The function does not guard against this — the LeetCode constraint guarantees nums has at least 1 element, so this is by design, not an oversight.