Solved 474: Ones and Zeroes with Java DP

🗓 Day 8 / 100 – #100DaysOfLeetCode 📌 Problem 474: Ones and Zeroes The task was to determine the largest subset of binary strings that can be formed using at most m zeros and n ones — essentially a two-dimensional 0/1 Knapsack problem. 🧠 My Approach (in Java): Counted the number of zeros and ones in each string. Used a 2D DP array dp[m][n] to store the maximum subset size possible with given zeros and ones. Iterated in reverse order to ensure each string is only used once (knapsack-style update). Updated states using dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1). ⏱ Time Complexity: O(len(S) × M × N) 💾 Space Complexity: O(M × N) 💡 Key Learning: This problem strengthened my understanding of multi-dimensional DP — especially how to translate real-world constraints into DP states. It’s a great reminder that mastering DP isn’t about memorizing patterns but about building intuition on state transitions and decisions. Consistent effort. Deep learning. Step by step 🚀 #100DaysOfLeetCode #LeetCodeChallenge #Java #DynamicProgramming #Knapsack #Algorithms #ProblemSolving #DataStructures #DSA #CodingJourney #CompetitiveProgramming #SoftwareEngineering #LearningInPublic #DeveloperJourney #TechStudent #LogicBuilding #CodingCommunity #CodeEveryday #CareerGrowth #Optimization #KeepLearning

  • graphical user interface, application

Be Consistent champ :)

Like
Reply

To view or add a comment, sign in

Explore content categories