File: count-elements-with-strictly-smaller-and-greater-elements/solution.py

Date: 2026-06-06

Time: 15:57

count-elements-with-strictly-smaller-and-greater-elements/solution.py

Purpose

This 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.

Key Components

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.

Patterns

Dependencies

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.

Flow

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).

Invariants

Error Handling

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.