Leetcode Weekly Contest 465 | My Experience of attempting 2/4.

Competitive programming contests are always a mix of adrenaline, logic, and reality checks. In Weekly Contest 465, I managed to attempt 2 out of 4 problems. One was a smooth ride, while the other gave me a tough reality check - despite multiple attempts, I couldn’t get it fully accepted.
When I Saw the Problems…
Opening the contest page is always exciting - you scroll through the problems, quickly skim inputs/outputs, and immediately judge which one looks approachable.
Q1 (Restore Finishing Order) looked fairly straightforward: arrays, order, and filtering. A good warm-up.
Q2 (Balanced K-Factor Decomposition)? Oh boy. It felt tricky from the get-go. The kind of problem that you know has some number theory lurking behind it.
I decided to lock in Q1 quickly and then give Q2 my best shot.
Q1 - Restore Finishing Order
Problem Recap
We’re given:
An array
order
that represents the finishing order of all participants.An array
friends
(sorted) containing IDs of my friends.
We need to restore the finishing order of just my friends.
My Approach
The idea was simple:
Iterate through
order
.If the current element belongs to
friends
, add it to the result.Stop when all friends are accounted for.
Code
class Solution:
def recoverOrder(self, order: List[int], friends: List[int]) -> List[int]:
res = []
n = len(friends)
for i in range(len(order)):
if n == 0: break
if order[i] in friends:
res.append(order[i])
n -= 1
return res
Example
Input:
order = [3,1,2,5,4]
friends = [1,3,4]
Output:
[3,1,4]
Passed all test cases.
Complexity
Time:
O(n*m)
(sincein friends
is a linear lookup)Space:
O(m)
for the result
Q2 - Balanced K-Factor Decomposition
Problem Recap
We’re given n
and k
.
We must split n
into exactly k
integers such that:
Their product = n.
The maximum difference between any two numbers is minimized.
Sounds neat, right? But execution was painful.
Attempt 1 - My Own Solution (Passed ~400 cases)
I thought:
Factorize
n
.Distribute prime factors into
k
buckets.Multiply them out to get the final split.
from math import sqrt
class Solution:
def minDifference(self, n: int, k: int) -> List[int]:
factors = []
x = n
for i in range(2, int(sqrt(n)) + 1):
while x % i == 0:
factors.append(i)
x //= i
if x > 1:
factors.append(x)
res = [1] * k
idx = 0
for f in factors:
res[idx] *= f
idx = (idx + 1) % k
return res
Where It Failed ❌
For input:
n = 180, k = 2
My output:
[10, 18]
Expected:
[12, 15]
👉 Distribution wasn’t “balanced enough.”
Attempt 2 - With Heap Balancing (Passed ~800 cases)
I then tried using a min-heap to always assign the next factor to the smallest current product.
import heapq
from math import sqrt
class Solution:
def minDifference(self, n: int, k: int) -> List[int]:
factors = []
x = n
for i in range(2, int(sqrt(n))+1):
while x % i == 0:
factors.append(i)
x //= i
if x > 1:
factors.append(x)
factors.sort(reverse=True)
heap = [(1,i) for i in range(k)]
heapq.heapify(heap)
res = [0]*k
for f in factors:
val, idx = heapq.heappop(heap)
val *= f
heapq.heappush(heap, (val, idx))
while heap:
val, idx = heapq.heappop(heap)
res[idx] = val
return res
Where It Failed ❌
For input:
n = 360, k = 4
My output:
[6,10,2,3]
Expected:
[3,4,5,6]
👉 The heap logic gave uneven distributions.
Time & Space Complexity
Factorization Step:
O(√n)
Distribution Step:
Attempt 1:
O(#factors)
Attempt 2:
O(#factors log k)
Space:
O(#factors + k)
5. Final Thoughts
This contest was a reality check.
Q1 boosted my confidence - clean logic, straightforward implementation.
Q2 humbled me - despite two approaches, I couldn’t crack it fully.
But that’s what contests are all about. Sometimes you win, sometimes you debug endlessly, and sometimes you walk away with new insights.
For me, Contest 465 was a reminder: understanding factor distribution is harder than it looks, and balancing is key.
On to the next one.
Subscribe to my newsletter
Read articles from Prashant Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
