Given an array A of size n and an integer x:
x.Example:
A = [1, 3, -1, 5, 3, 6]
x = 3
Windows:
[1, 3, -1] → min = -1
[3, -1, 5] → min = -1
[-1, 5, 3] → min = -1
[5, 3, 6] → min = 3
Answer = 3Core pattern: Sliding Window + Monotonic Deque.
The tricky part is recognizing that we need to maintain the minimum of the current window efficiently rather than recomputing it for every window.
Given a table:
id pid
1 NULL
2 1
3 1
4 2Where:
id = node IDpid = parent IDClassify every node as:
Expected result:
1 → Root
2 → Inner
3 → Leaf
4 → LeafThe concept itself was straightforward.
The tricky part was writing the SQL correctly — especially determining whether a node appears as someone else's pid and combining that with the NULL parent condition.
A typical approach is to use:
CASE
WHEN pid IS NULL THEN 'Root'
WHEN id IN (SELECT pid FROM Tree WHERE pid IS NOT NULL) THEN 'Inner'
ELSE 'Leaf'
END