LeetCode 238: Product of Array Except Self — O(n) Solution in Dart

Опубликовано: 26 Сентябрь 2026
на канале: Luci Studio
18
0

Five solutions, from brute force to O(1) extra space — plus a 180ms to 3ms
lesson on why two solutions with the same Big-O can still be 30x apart.

🔗 Problem
https://leetcode.com/problems/product...

In this video:
• Why the obvious "multiply everything, then divide" trick is banned by the
problem — and why a single zero would break it anyway
• The core idea: result[i] = product of everything to its left, times the
product of everything to its right
• Building prefix and suffix products in two linear passes
• A real performance trap: using string keys in a hash map turned an O(n)
solution into 180ms and 239MB — the numbers that finally were not just noise
• Swapping the map for plain int arrays: 180ms down to 11ms, same algorithm,
same Big-O
• The final trick — store before you multiply, then reuse the output array —
for O(1) extra space, which is what the follow-up actually asks for
• Full Big-O walkthrough of all five versions

⏱️ Chapters
00:00 Intro — reading the problem and its two constraints
[TBD] Solution 1 — brute force, O(n squared)
[TBD] Why we cannot use division
[TBD] Solution 2 — prefix and suffix products
[TBD] Solution 3 — early exit on zeros
[TBD] The 180ms problem — string keys are expensive
[TBD] Solution 4 — arrays instead of a map
[TBD] Solution 5 — O(1) extra space
[TBD] Big-O recap and takeaways

💻 Code (all 5 solutions)
https://github.com/kido-luci/hardcore...

Part of the LeetCode in Dart series — every problem solved step by step,
always ending at the optimal solution.
👉 luci studio · https://luci-studio.com

#leetcode #dart #algorithms #coding #codinginterview #prefixsum
#datastructures #programming #leetcode238 #flutter