You are given three independent coding tasks (as in an online assessment). Implement each as a separate function.
1) Uppercase–lowercase difference
Input: A list of characters chars (each element is a 1-length string).
Output: An integer equal to:
(#uppercase letters)−(#lowercase letters)
Notes:
-
Use typical letter checks (e.g.,
isupper()
,
islower()
semantics).
-
Non-letters (digits, punctuation, whitespace, etc.) do not affect the result.
2) Repartition numbers into two lists, then merge
Input: An integer array nums.
Process: Iterate nums from left to right and build two lists A and B.
For each number x:
-
Let
maxA
be the maximum value currently in
A
(or
−∞
if
A
is empty). Let
maxB
be the maximum value currently in
B
(or
−∞
if
B
is empty).
-
If
maxA > x
or
maxB > x
, append
x
to the list whose current maximum is larger:
-
if
maxA >= maxB
, append to
A
; else append to
B
.
-
Otherwise (i.e.,
x
is at least as large as both current maxima), append
x
to the
shorter
list (if tied, append to
A
).
Output: Return the concatenation A + B.
3) Balloon explosion in an n×n grid with gravity
Input: An n × n integer matrix grid where each positive integer represents a balloon color/label, and 0 represents an empty cell.
Explosion rule (4-neighborhood): For any cell (r, c) with value v > 0, count how many of its up/down/left/right neighbors also have value v. If that count is ≥ 2, then the balloon at (r, c) explodes.
-
All explosions in a round happen
simultaneously
(decide based on the grid state at the start of the round).
-
Exploded cells become
0
.
Gravity rule: After explosions, apply gravity column by column: within each column, all non-zero values fall downward to fill empty spaces, preserving their relative order (stable fall).
Repeat: Repeat “explosion round → gravity” until a full round causes no explosions.
Output: Return the final stabilized grid.
Clarifications:
-
Only 4-direction adjacency counts (no diagonals).
-
Edge cells have fewer neighbors.