|
| 1 | +""" |
| 2 | +URL: https://leetcode.com/problems/maximum-number-of-operations-to-move-ones-to-the-end/description/?envType=problem-list-v2&envId=vn57k9wr |
| 3 | +
|
| 4 | +3228. Maximum Number of Operations to Move Ones to the End |
| 5 | +
|
| 6 | +You are given a binary string s. |
| 7 | +
|
| 8 | +You can perform the following operation on the string any number of times: |
| 9 | +
|
| 10 | +- Choose any index i from the string where i + 1 < s.length such that s[i] == '1' and s[i + 1] == '0'. |
| 11 | +- Move the character s[i] to the right until it reaches the end of the string or another '1'. For example, for s = "010010", if we choose i = 1, the resulting string will be s = "000110". |
| 12 | +
|
| 13 | +Return the maximum number of operations that you can perform. |
| 14 | +
|
| 15 | +Example 1: |
| 16 | +
|
| 17 | +Input: s = "1001101" |
| 18 | +Output: 4 |
| 19 | +Explanation: |
| 20 | +We can perform the following operations: |
| 21 | +- Choose index i = 0. The resulting string is "0011101". |
| 22 | +- Choose index i = 4. The resulting string is "0011011". |
| 23 | +- Choose index i = 3. The resulting string is "0010111". |
| 24 | +- Choose index i = 2. The resulting string is "0001111". |
| 25 | +
|
| 26 | +Example 2: |
| 27 | +
|
| 28 | +Input: s = "00111" |
| 29 | +Output: 0 |
| 30 | +
|
| 31 | +Constraints: |
| 32 | +- 1 <= s.length <= 10^5 |
| 33 | +- s[i] is either '0' or '1'. |
| 34 | +
|
| 35 | +--- |
| 36 | +
|
| 37 | +10 -> 01 == 1 |
| 38 | +100 -> 001 == 1 |
| 39 | +110 -> 011 == 2 |
| 40 | +
|
| 41 | +
|
| 42 | +
|
| 43 | +0011101 |
| 44 | +
|
| 45 | +Find the right most zero, and count the number of 1s to its left, + 1. |
| 46 | +
|
| 47 | +""" |
| 48 | + |
| 49 | + |
| 50 | +class Solution: |
| 51 | + def maxOperations(self, s: str) -> int: |
| 52 | + s = s[::-1] |
| 53 | + g = [[key, len(list(val))] for key, val in groupby(s)] |
| 54 | + mult = 0 |
| 55 | + count = 0 |
| 56 | + for c, num in g: |
| 57 | + if c == "1" and mult == 0: |
| 58 | + continue |
| 59 | + if c == "0": |
| 60 | + mult += 1 |
| 61 | + elif c == "1": |
| 62 | + count += mult * num |
| 63 | + return count |
| 64 | + |
| 65 | + |
| 66 | +sol = Solution() |
| 67 | + |
| 68 | +# print(sol.maxOperations("1001101")) # 4 |
| 69 | + |
| 70 | +assert sol.maxOperations("1001101") == 4 |
| 71 | +assert sol.maxOperations("00111") == 0 |
| 72 | +assert sol.maxOperations("0") == 0 |
| 73 | +assert sol.maxOperations("1") == 0 |
| 74 | +assert sol.maxOperations("1010") == 3 |
| 75 | +assert sol.maxOperations("01") == 0 |
| 76 | +assert sol.maxOperations("010101") == 3 |
| 77 | +assert sol.maxOperations("000") == 0 |
| 78 | +assert sol.maxOperations("1111") == 0 |
| 79 | +assert sol.maxOperations("10100") == 3 |
| 80 | +# assert sol.maxOperations("1001") == 2 |
| 81 | + |
| 82 | +assert sol.maxOperations("10") == 1 |
| 83 | +assert sol.maxOperations("100") == 1 |
| 84 | +assert sol.maxOperations("110") == 2 |
| 85 | +assert sol.maxOperations("1110") == 3 |
| 86 | +assert sol.maxOperations("101") == 1 |
| 87 | +assert sol.maxOperations("00101") == 1 |
| 88 | +assert sol.maxOperations("100000") == 1 |
| 89 | +assert sol.maxOperations("01110") == 3 |
| 90 | +assert sol.maxOperations("1100") == 2 |
0 commit comments