Skip to content

Commit

Permalink
New Problem Solution - "1529. Bulb Switcher IV"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Mar 29, 2021
1 parent c150b17 commit 6f16a68
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ LeetCode
|1550|[Three Consecutive Odds](https://leetcode.com/problems/three-consecutive-odds/) | [C++](./algorithms/cpp/threeConsecutiveOdds/ThreeConsecutiveOdds.cpp)|Easy|
|1541|[Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/) | [C++](./algorithms/cpp/minimumInsertionsToBalanceAParenthesesString/MinimumInsertionsToBalanceAParenthesesString.cpp)|Medium|
|1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/) | [C++](./algorithms/cpp/findTheWinnerOfAnArrayGame/FindTheWinnerOfAnArrayGame.cpp)|Medium|
|1529|[Bulb Switcher IV](https://leetcode.com/problems/bulb-switcher-iv/) | [C++](./algorithms/cpp/bulbSwitcher/BulbSwitcher.IV.cpp)|Medium|
|1528|[Shuffle String](https://leetcode.com/problems/shuffle-string/submissions/) | [C++](./algorithms/cpp/shuffleString/ShuffleString.cpp)|Easy|
|1525|[Number of Good Ways to Split a String](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/) | [C++](./algorithms/cpp/numberOfGoodWaysToSplitAString/NumberOfGoodWaysToSplitAString.cpp)|Medium|
|1524|[Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | [C++](./algorithms/cpp/numberOfSubArraysWithOddSum/NumberOfSubArraysWithOddSum.cpp)|Medium|
Expand Down
65 changes: 65 additions & 0 deletions algorithms/cpp/bulbSwitcher/BulbSwitcher.IV.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Source : https://leetcode.com/problems/bulb-switcher-iv/
// Author : Hao Chen
// Date : 2021-03-29

/*****************************************************************************************************
*
* There is a room with n bulbs, numbered from 0 to n-1, arranged in a row from left to right.
* Initially all the bulbs are turned off.
*
* Your task is to obtain the configuration represented by target where target[i] is '1' if the i-th
* bulb is turned on and is '0' if it is turned off.
*
* You have a switch to flip the state of the bulb, a flip operation is defined as follows:
*
* Choose any bulb (index i) of your current configuration.
* Flip each bulb from index i to n-1.
*
* When any bulb is flipped it means that if it is 0 it changes to 1 and if it is 1 it changes to 0.
*
* Return the minimum number of flips required to form target.
*
* Example 1:
*
* Input: target = "10111"
* Output: 3
* Explanation: Initial configuration "00000".
* flip from the third bulb: "00000" -> "00111"
* flip from the first bulb: "00111" -> "11000"
* flip from the second bulb: "11000" -> "10111"
* We need at least 3 flip operations to form target.
*
* Example 2:
*
* Input: target = "101"
* Output: 3
* Explanation: "000" -> "111" -> "100" -> "101".
*
* Example 3:
*
* Input: target = "00000"
* Output: 0
*
* Example 4:
*
* Input: target = "001011101"
* Output: 5
*
* Constraints:
*
* 1 <= target.length <= 10^5
* target[i] == '0' or target[i] == '1'
******************************************************************************************************/

class Solution {
public:
int minFlips(string target) {
//flip the target to initalization
int flip = 0;
for(auto state : target) {
if (state == '0' && flip % 2 == 1 ) flip++;
if (state == '1' && flip % 2 == 0 ) flip++;
}
return flip;
}
};

0 comments on commit 6f16a68

Please sign in to comment.