Solved #100DaysOfCode problem 3228 in Java using prefix accumulation logic

Day 41 of #100DaysOfCode Problem: 3228. Maximum Number of Operations to Move Ones to the End Difficulty: Medium Language: Java Status: Solved Problem Summary: Given a binary string s, you can repeatedly choose an index i such that: s[i] == '1' and s[i + 1] == '0', and move that '1' to the right until it either reaches the end of the string or sits just before another '1'. You must find the maximum number of such operations possible. Key Idea: Every '0' that appears after a '1' contributes to new operations. Each '0' can “absorb” all previous '1's — since each of them can eventually be moved right past it. Hence, for every '0' that immediately follows a '1', we can add the total count of '1's so far to our result. Algorithm Steps: Initialize counters: ones = 0, res = 0. Traverse the string: When you see '1', increment ones. When you see '0' after a '1', add ones to res. Return res. Time Complexity: O(n) Space Complexity: O(1) Learning Takeaway: Great example of prefix accumulation logic — counting how many prior elements influence the current one. Efficiently transforms a greedy movement problem into a simple linear counting one. #Day41 #100DaysOfCode #LeetCode #StringManipulation #Greedy #Java #Algorithms #CodingChallenge #ProblemSolving #DSA

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories