← Home

1529. Bulb Switcher IV

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
152

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1529. Bulb Switcher IV, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are minFlips, state.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 64 ms, faster than 40.58% of C++ online submissions for Bulb Switcher IV.
02//Memory Usage: 10.3 MB, less than 100.00% of C++ online submissions for Bulb Switcher IV.
03class Solution {
04public:
05    int minFlips(string target) {
06        int n = target.size();
07        
08        string state(n, '0');
09        int count = 0;
10        bool flip = false;
11        
12        for(int i = 0; i < n; ++i){
13            if(!flip && state[i] != target[i] || flip && state[i] == target[i]){
14                ++count;
15                flip = !flip;
16            }
17        }
18        
19        return count;
20    }
21};
22
23//we don't need an array for state, because the initial state for all positions are the same
24//https://leetcode.com/problems/bulb-switcher-iv/discuss/755939/C%2B%2B-Python-Java%3A-Readable-easy-code-with-explanation-and-code-comments
25//Runtime: 48 ms, faster than 81.67% of C++ online submissions for Bulb Switcher IV.
26//Memory Usage: 9.4 MB, less than 100.00% of C++ online submissions for Bulb Switcher IV.
27class Solution {
28public:
29    int minFlips(string target) {
30        int n = target.size();
31        
32        char state = '0';
33        int count = 0;
34        bool flip = false;
35        
36        for(int i = 0; i < n; ++i){
37            if(state != target[i]){
38                ++count;
39                flip = !flip;
40                state = (state == '0') ? '1' : '0';
41            }
42        }
43        
44        return count;
45    }
46};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.