LeetCode
LeetCode 1437: Check If All 1's Are at Least K Apart (ACERS Guide)
Subtitle / Summary A classic event-spacing validation model. This ACERS guide explains the one-pass logic, engineering use cases, and runnable multi-language solutions. Reading time: 10–12 min Tags: array, two pointers, event spacing SEO keywords: LeetCode 1437, event spacing, O(n) Meta description: One-pass validation for minimum spacing between 1s, with engineering use cases and multi-language code. Target Readers LeetCode learners building stable templates Engineers working on monitoring / risk control / behavior analytics Developers who need spacing or rate-limit validations Background / Motivation Many systems require events to be spaced apart: login failures, alarms, sensitive actions, API calls, etc. This problem maps directly to event spacing validation. A one-pass, O(1)-memory solution is ideal for real-time systems. ...
LeetCode 231: Power of Two (Bit Trick O(1) ACERS Guide)
Subtitle / Summary A classic bit-manipulation template: determine if a number is a power of two in O(1). This ACERS guide covers the core insight, practical uses, and runnable multi-language implementations. Reading time: 8–12 min Tags: bit manipulation, binary, math SEO keywords: Power of Two, bit manipulation, binary, O(1), LeetCode 231 Meta description: O(1) power-of-two check using bit tricks, with engineering scenarios and multi-language code. Target Readers LeetCode learners building a bit-manipulation toolkit Backend / systems engineers who need alignment or capacity checks Anyone who wants stable O(1) integer tests Background / Motivation Power-of-two checks show up everywhere: hash table capacities, memory alignment, sharding, FFT window sizes. Looping or using floating-point logs is slower and prone to corner-case bugs. The bitwise method is fast, simple, and reliable. ...
LeetCode 1456: Maximum Number of Vowels in a Substring of Given Length (ACERS Guide)
Subtitle / Summary A standard fixed-window counting problem. This ACERS guide explains the sliding-window model, engineering use cases, and runnable multi-language solutions. Reading time: 10–12 min Tags: sliding window, string, fixed window SEO keywords: Maximum Number of Vowels, Sliding Window, Fixed Window Meta description: Fixed-window sliding count for maximum vowels with engineering applications. Target Readers LeetCode learners who want stable templates Engineers working on windowed metrics Anyone building real-time counters Background / Motivation Many engineering tasks ask: “What is the maximum count in any fixed-length window?” Recomputing every window is O(nk). Sliding window updates in O(1) per step, giving O(n). ...
LeetCode 239: Sliding Window Maximum (Monotonic Queue ACERS Guide)
Title LeetCode 239: Sliding Window Maximum (Monotonic Queue ACERS Guide) Subtitle / Summary Sliding Window Maximum is the classic combo of sliding window + monotonic queue. This article follows the ACERS template with reusable engineering patterns and multi-language implementations. Estimated reading time: 12–15 minutes Tags: sliding window, monotonic queue, array SEO keywords: Sliding Window Maximum, monotonic queue, deque, O(n) Meta description: Monotonic-queue solution for Sliding Window Maximum with engineering practice and multi-language implementations. Target Readers People practicing LeetCode / Hot100 Mid-level developers who want a reusable “sliding window + monotonic queue” template Engineers working on real-time monitoring, log analytics, or risk control Background / Motivation Rolling-window maximum appears everywhere: latency monitoring, price spikes, temperature alerts, real-time smoothing, and many more. The brute-force approach recomputes max per window in O(nk), which is unacceptable for large n. The monotonic queue reduces it to O(n), making it the most practical engineering choice. ...
LeetCode 1512: Number of Good Pairs (Hash Counting ACERS Guide)
Subtitle / Abstract A basic counting problem: use frequency + combinations to drop O(n^2) to O(n). Includes engineering use cases and portable implementations. Reading time: 8-10 minutes Tags: hash-table, counting, array SEO keywords: Good Pairs, hash map, frequency Meta description: Hash counting solution for Good Pairs with complexity and code. Target readers Beginners learning hash tables and counting Engineers who want to map interview patterns to real stats tasks Interview prep for basic counting models Background / Motivation Counting equal pairs is a classic problem. A double loop is O(n^2). With frequency counting, you can solve it in linear time and scale to large data. ...
LeetCode 1: Two Sum (Hash Map ACERS Summary)
LeetCode 1: Two Sum Summary Find two indices such that nums[i] + nums[j] = target. Use a hash map for O(n) time. Approach Iterate and store value -> index. For each number x, check if target - x exists. Complexity Time: O(n) Space: O(n) Python reference implementation def two_sum(nums, target): seen = {} for i, x in enumerate(nums): y = target - x if y in seen: return [seen[y], i] seen[x] = i
LeetCode 2300: Successful Pairs of Spells and Potions
LeetCode 2300: Successful Pairs of Spells and Potions Summary For each spell, count how many potions make spell * potion >= success. Sort potions and binary search the threshold. Approach Sort potions. For each spell, compute need = ceil(success / spell). Use binary search to find the first potion >= need. Complexity Time: O(n log n) Space: O(1) extra (or O(n) if sorting a copy) Python reference implementation import bisect import math def successful_pairs(spells, potions, success): potions = sorted(potions) n = len(potions) res = [] for s in spells: need = (success + s - 1) // s idx = bisect.bisect_left(potions, need) res.append(n - idx) return res
LeetCode 2379: Minimum Recolors to Get K Consecutive Black Blocks
LeetCode 2379: Minimum Recolors to Get K Consecutive Black Blocks Summary Given a string of ‘B’ and ‘W’, find the minimum recolors to make a substring of length k all black. Approach Use a sliding window of length k and count the number of whites in the window. The minimum whites across all windows is the answer. Complexity Time: O(n) Space: O(1) Python reference implementation def minimum_recolors(blocks, k): whites = sum(1 for c in blocks[:k] if c == 'W') ans = whites for i in range(k, len(blocks)): if blocks[i-k] == 'W': whites -= 1 if blocks[i] == 'W': whites += 1 ans = min(ans, whites) return ans
LeetCode 2841: Maximum Sum of Almost Unique Subarray
LeetCode 2841: Maximum Sum of Almost Unique Subarray Summary Given an array, window size k, and threshold m, find the maximum sum of any length-k subarray that contains at least m distinct elements. Approach Use a sliding window with a frequency map, track window sum and number of distinct values. Complexity Time: O(n) Space: O(n) for frequency map Python reference implementation def max_sum_almost_unique(nums, m, k): from collections import defaultdict count = defaultdict(int) distinct = 0 window_sum = 0 ans = 0 for i, x in enumerate(nums): window_sum += x if count[x] == 0: distinct += 1 count[x] += 1 if i >= k: y = nums[i - k] window_sum -= y count[y] -= 1 if count[y] == 0: distinct -= 1 if i >= k - 1 and distinct >= m: ans = max(ans, window_sum) return ans