"""Stage 2: Two Pointers (problems 13-20)."""
from __future__ import annotations

from typing import List


def is_palindrome(s: str) -> bool:
    """LC 125. Walk inward from both ends, skipping non-alphanumerics."""
    i, j = 0, len(s) - 1
    while i < j:
        if not s[i].isalnum():
            i += 1
        elif not s[j].isalnum():
            j -= 1
        elif s[i].lower() != s[j].lower():
            return False
        else:
            i, j = i + 1, j - 1
    return True


def two_sum_sorted(numbers: List[int], target: int) -> List[int]:
    """LC 167. Sum too small -> move left up; too big -> move right down. 1-indexed answer."""
    i, j = 0, len(numbers) - 1
    while i < j:
        total = numbers[i] + numbers[j]
        if total == target:
            return [i + 1, j + 1]
        if total < target:
            i += 1
        else:
            j -= 1
    return []


def three_sum(nums: List[int]) -> List[List[int]]:
    """LC 15. Sort, fix one value, run two-sum-sorted on the rest, skip duplicates."""
    nums = sorted(nums)
    out = []
    for i, a in enumerate(nums):
        if a > 0:
            break
        if i and a == nums[i - 1]:
            continue
        lo, hi = i + 1, len(nums) - 1
        while lo < hi:
            total = a + nums[lo] + nums[hi]
            if total < 0:
                lo += 1
            elif total > 0:
                hi -= 1
            else:
                out.append([a, nums[lo], nums[hi]])
                lo += 1
                while lo < hi and nums[lo] == nums[lo - 1]:
                    lo += 1
    return out


def max_area(height: List[int]) -> int:
    """LC 11. Always move the shorter wall: the taller one can never do better with less width."""
    i, j, best = 0, len(height) - 1, 0
    while i < j:
        best = max(best, (j - i) * min(height[i], height[j]))
        if height[i] < height[j]:
            i += 1
        else:
            j -= 1
    return best


def trap(height: List[int]) -> int:
    """LC 42. Water at i = min(maxLeft, maxRight) - h; process the side with the smaller max."""
    i, j = 0, len(height) - 1
    left_max = right_max = water = 0
    while i < j:
        if height[i] < height[j]:
            left_max = max(left_max, height[i])
            water += left_max - height[i]
            i += 1
        else:
            right_max = max(right_max, height[j])
            water += right_max - height[j]
            j -= 1
    return water


def remove_duplicates(nums: List[int]) -> int:
    """LC 26. Slow pointer marks the end of the unique prefix (in place)."""
    if not nums:
        return 0
    k = 1
    for i in range(1, len(nums)):
        if nums[i] != nums[k - 1]:
            nums[k] = nums[i]
            k += 1
    return k


def move_zeroes(nums: List[int]) -> None:
    """LC 283. Swap each non-zero into the next write slot (stable, in place)."""
    write = 0
    for read in range(len(nums)):
        if nums[read] != 0:
            nums[write], nums[read] = nums[read], nums[write]
            write += 1


def sort_colors(nums: List[int]) -> None:
    """LC 75. Dutch national flag: low/mid/high regions in one pass."""
    low, mid, high = 0, 0, len(nums) - 1
    while mid <= high:
        if nums[mid] == 0:
            nums[low], nums[mid] = nums[mid], nums[low]
            low += 1
            mid += 1
        elif nums[mid] == 1:
            mid += 1
        else:
            nums[mid], nums[high] = nums[high], nums[mid]
            high -= 1
