1. Problem Summary
-
Task: Given integers
num
andt
, a numberx
is achievable if you can apply at mostt
operations that changex
by ±1 and simultaneously changenum
by ±1 so that after these operationsx == num
. Return the maximum achievablex
. - Category: Math / Invariants & Reachability.
- Difficulty: Easy.
-
Key constraints impacting method:
a. Each operation adjusts both numbers by 1 in some directions.
b. The distance
|x - num|
can be reduced by at most 2 per operation (increasex
while decreasingnum
, or vice versa). c. With at mostt
operations, the largest reachablex
must satisfyx - num ≤ 2t
.
2. Approaches & Strategies
-
Distance (invariant) reasoning: Track the difference
d = x - num
. One operation can changed
by-2, 0, or +2
, so in at mostt
steps you can bridge any initial gap whose magnitude is ≤2t
. -
Maximization logic: To maximize
x
, take the casex ≥ num
. The largestx
that can still meetnum
withint
steps satisfiesx - num = 2t
, hencex = num + 2t
. -
Why this is optimal and sufficient:
a. Sufficiency: If
x = num + 2t
, performt
steps ofx -= 1
andnum += 1
to meet in the middle. b. Optimality: Ifx > num + 2t
, even witht
best-possible steps (shrinking distance by 2 each), the gap cannot close to 0.
3. My Solution & Code
-
Strategy used: Convert the problem to a simple closed-form using the “distance shrinks by ≤2 per move” invariant; directly return
num + 2 * t
. -
Comparison to alternatives: Any brute-force simulation over operations or candidate
x
is unnecessary; the invariant yields anO(1)
formula.
def maximumAchievableX(num: int, t: int) -> int:
# Max x such that |x - num| <= 2t ⇒ x_max = num + 2t
return num + 2 * t
4. Time & Space Complexity
-
Time:
O(1)
due to the direct formula. -
Space:
O(1)
with no auxiliary storage. -
Trade-offs: A simulation approach would be
O(t)
and error-prone; the invariant gives the optimal constant-time solution.
5. Mistakes & Insights
-
Misreading the operation: Thinking only one of
x
ornum
changes per step; in fact both change each time. -
Underestimating distance shrink: Each best move can reduce the gap by 2, not 1; missing this leads to
num + t
instead ofnum + 2t
. -
Parity overthinking: Since both change by ±1 each step, parity mismatches are irrelevant; you can always meet in
≤ t
steps if|x - num| ≤ 2t
. -
Off-by-one errors: “At most
t
” includes using fewer thant
steps; equality at exactlyt
steps is not required.
6. What’s Next
- Focus areas: a. Practice identifying invariants and meeting-in-the-middle style arguments. b. Convert operation-based problems into distance bounds or closed forms when possible.
- Related LeetCode problems: a. 1539. Kth Missing Positive Number (Easy) — Uses count/bounds reasoning to derive a direct answer without simulating all numbers. b. 2520. Count the Digits That Divide a Number (Easy) — Simple numerical reasoning, good for practicing careful constraints and invariants on digits. c. 2869. Minimum Operations to Collect Elements (Easy) — Reason about operations and minimal steps to reach a target condition, reinforcing operation-to-bound transformations.
I created this study note using LeetCopilot, an AI-powered chrome extension for LeetCode study and interview prep. LeetCopilot generate high-quality, personalized and structured study note for each problem, based on your learning history. Create you own study note with LeetCopilot today :)
Top comments (0)