Using greedy without proof
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.
Move from brute-force thinking to an efficient approach using greedy strategy.
You are given a 0-indexed binary string target of length n. You have another binary string s of length n that is initially set to all zeros. You want to make s equal to target.
In one operation, you can pick an index i where 0 <= i < n and flip all bits in the inclusive range [i, n - 1]. Flip means changing '0' to '1' and '1' to '0'.
Return the minimum number of operations needed to make s equal to target.
Example 1:
Input: target = "10111" Output: 3 Explanation: Initially, s = "00000". Choose index i = 2: "00000" -> "00111" Choose index i = 0: "00111" -> "11000" Choose index i = 1: "11000" -> "10111" We need at least 3 flip operations to form target.
Example 2:
Input: target = "101" Output: 3 Explanation: Initially, s = "000". Choose index i = 0: "000" -> "111" Choose index i = 1: "111" -> "100" Choose index i = 2: "100" -> "101" We need at least 3 flip operations to form target.
Example 3:
Input: target = "00000" Output: 0 Explanation: We do not need any operations since the initial s already equals target.
Constraints:
n == target.length1 <= n <= 105target[i] is either '0' or '1'.Problem summary: You are given a 0-indexed binary string target of length n. You have another binary string s of length n that is initially set to all zeros. You want to make s equal to target. In one operation, you can pick an index i where 0 <= i < n and flip all bits in the inclusive range [i, n - 1]. Flip means changing '0' to '1' and '1' to '0'. Return the minimum number of operations needed to make s equal to target.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Greedy
"10111"
"101"
"00000"
minimum-operations-to-make-binary-array-elements-equal-to-one-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1529: Minimum Suffix Flips
class Solution {
public int minFlips(String target) {
int ans = 0;
for (int i = 0; i < target.length(); ++i) {
int v = target.charAt(i) - '0';
if (((ans & 1) ^ v) != 0) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1529: Minimum Suffix Flips
func minFlips(target string) int {
ans := 0
for _, c := range target {
v := int(c - '0')
if ((ans & 1) ^ v) != 0 {
ans++
}
}
return ans
}
# Accepted solution for LeetCode #1529: Minimum Suffix Flips
class Solution:
def minFlips(self, target: str) -> int:
ans = 0
for v in target:
if (ans & 1) ^ int(v):
ans += 1
return ans
// Accepted solution for LeetCode #1529: Minimum Suffix Flips
impl Solution {
pub fn min_flips(target: String) -> i32 {
let mut ans = 0;
for c in target.chars() {
let bit = (c as u8 - b'0') as i32;
if ans % 2 != bit {
ans += 1;
}
}
ans
}
}
// Accepted solution for LeetCode #1529: Minimum Suffix Flips
function minFlips(target: string): number {
let ans = 0;
for (const c of target) {
if (ans % 2 !== +c) {
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.