
Leetcode Sunday #4
How My “Worst” Complexity Solution Beat 100% Runtime — Binary Gap in Go When I solved the Binary Gap problem, I expected the cleaner, theoretically optimal solution to dominate. Instead, something unexpected happened. Guess which one ranked higher? My O(n log n) solution beat 100% of submissions in runtime , while my O(n), O(1) solution used more memory. That forced me to pause and rethink what I thought I understood about complexity. Let’s break it down. Problem Recap — What Is a Binary Gap? A binary gap is the distance between two consecutive 1 s in the binary representation of a number. More precisely: A binary gap is the number of consecutive 0 s between two adjacent 1 s, plus one (to include the closing 1 ). Trailing zeros do not count, because a valid gap must be bounded by 1 s on both sides. Example Given a binary list represented as: Binary: 00010001001 Index: 01234567890 Valid gaps: Between index 3 and 7 → 000 → gap = 3 + 1 = 4 Between index 7 and 10 → 00 → gap = 2 + 1 = 3 Lar
Continue reading on Dev.to
Opens in a new tab




