"""Blind 75, Array section (10 problems)."""
from typing import List


def two_sum(nums: List[int], target: int) -> List[int]:
    """LC 1. Remember each value's index; look up the complement before storing."""
    seen = {}
    for i, n in enumerate(nums):
        if target - n in seen:
            return [seen[target - n], i]
        seen[n] = i
    return []


def max_profit(prices: List[int]) -> int:
    """LC 121. Track the cheapest price so far; best sale is today minus that."""
    lowest, best = float("inf"), 0
    for p in prices:
        lowest = min(lowest, p)
        best = max(best, p - lowest)
    return best


def contains_duplicate(nums: List[int]) -> bool:
    """LC 217. A set drops duplicates, so a size change means a repeat."""
    return len(set(nums)) != len(nums)


def product_except_self(nums: List[int]) -> List[int]:
    """LC 238. Answer = product of everything left times everything right; two passes, no division."""
    n = len(nums)
    out = [1] * n
    left = 1
    for i in range(n):
        out[i] = left
        left *= nums[i]
    right = 1
    for i in range(n - 1, -1, -1):
        out[i] *= right
        right *= nums[i]
    return out


def max_subarray(nums: List[int]) -> int:
    """LC 53 (Kadane). Extend the running sum or restart at the current value, whichever is larger."""
    best = cur = nums[0]
    for n in nums[1:]:
        cur = max(n, cur + n)
        best = max(best, cur)
    return best


def max_product(nums: List[int]) -> int:
    """LC 152. Keep both the max and min product ending here; a negative swaps them."""
    best = hi = lo = nums[0]
    for n in nums[1:]:
        candidates = (n, hi * n, lo * n)
        hi, lo = max(candidates), min(candidates)
        best = max(best, hi)
    return best


def find_min_rotated(nums: List[int]) -> int:
    """LC 153. Binary search: if mid > right end, the minimum is to the right of mid."""
    lo, hi = 0, len(nums) - 1
    while lo < hi:
        mid = (lo + hi) // 2
        if nums[mid] > nums[hi]:
            lo = mid + 1
        else:
            hi = mid
    return nums[lo]


def search_rotated(nums: List[int], target: int) -> int:
    """LC 33. One half around mid is always sorted; check if target lies in that half."""
    lo, hi = 0, len(nums) - 1
    while lo <= hi:
        mid = (lo + hi) // 2
        if nums[mid] == target:
            return mid
        if nums[lo] <= nums[mid]:
            if nums[lo] <= target < nums[mid]:
                hi = mid - 1
            else:
                lo = mid + 1
        else:
            if nums[mid] < target <= nums[hi]:
                lo = mid + 1
            else:
                hi = mid - 1
    return -1


def three_sum(nums: List[int]) -> List[List[int]]:
    """LC 15. Sort, fix one number, two-pointer the rest; skip equal values to avoid duplicates."""
    nums = sorted(nums)
    out = []
    for i, a in enumerate(nums):
        if a > 0:
            break
        if i and a == nums[i - 1]:
            continue
        l, r = i + 1, len(nums) - 1
        while l < r:
            s = a + nums[l] + nums[r]
            if s < 0:
                l += 1
            elif s > 0:
                r -= 1
            else:
                out.append([a, nums[l], nums[r]])
                l += 1
                while l < r and nums[l] == nums[l - 1]:
                    l += 1
    return out


def max_area(height: List[int]) -> int:
    """LC 11. Start wide; always move the shorter wall, since the taller one cannot help it."""
    l, r, best = 0, len(height) - 1, 0
    while l < r:
        best = max(best, (r - l) * min(height[l], height[r]))
        if height[l] < height[r]:
            l += 1
        else:
            r -= 1
    return best
