commit 47a601b11c9e02232e12ac7f90b04ea7a1a259bd
parent 7c20f971e0ccd3988117d056d4f565c936f7a7b0
Author: Jack Sangdahl <jack@pngu.org>
Date: Wed, 12 Nov 2025 22:15:35 -0700
3228. maximum number of operations to move ones to the end
Diffstat:
2 files changed, 57 insertions(+), 0 deletions(-)
diff --git a/max_num_ops_move_one_end.c b/max_num_ops_move_one_end.c
@@ -0,0 +1,26 @@
+// 3228. Maximum Number of Operations to Move Ones to the End
+#include "leetcode.h"
+
+/*
+ * you are given a binary string 's'. you can perform the followiong operation
+ * on the string any number of timees: choose any index 'i' from the string
+ * where 'i + 1 < s.length' such that 's[i] == 1' and 's[i + 1] == 0'. move the
+ * character 's[i]' to the right until it reaches the end of the string or
+ * another 1. return the maximum number of operations that you can perform.
+ */
+
+int maxOperations(char *s) {
+ int ans = 0, ones = 0, n = strlen(s);
+ for (int i = 0; i < n; ++i) {
+ ones += s[i] - '0';
+ if (i > 0 && s[i] < s[i - 1])
+ ans += ones;
+ }
+ return ans;
+}
+
+int main() {
+ char *s1 = "1001101", *s2 = "00111";
+ printf("%d\n", maxOperations(s1)); // expect: 4
+ printf("%d\n", maxOperations(s2)); // expect: 0
+}
diff --git a/max_num_ops_move_one_end.py b/max_num_ops_move_one_end.py
@@ -0,0 +1,31 @@
+# 3228. Maximum Number of Operations to Move Ones to the End
+from itertools import groupby
+
+"""
+you are given a binary string 's'. you can perform the followiong operation
+on the string any number of timees: choose any index 'i' from the string
+where 'i + 1 < s.length' such that 's[i] == 1' and 's[i + 1] == 0'. move the
+character 's[i]' to the right until it reaches the end of the string or
+another 1. return the maximum number of operations that you can perform.
+"""
+
+
+class Solution(object):
+ def maxOperations(self, s):
+ """
+ :type s: str
+ :rtype: int
+ """
+ ans, ones = 0, 0
+ for i, j in groupby(s):
+ if i == "1":
+ ones += len(list(j))
+ else:
+ ans += ones
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.maxOperations(s="1001101"))
+ print(obj.maxOperations(s="00111"))