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
Solved #100DaysOfCode problem 3228 in Java using prefix accumulation logic
More Relevant Posts
-
#Day_28 Today’s problem was quite an interesting one — “Reach a Target Number” using minimal moves. 💡 Problem Summary: Starting from 0, on each move i, you can go either left or right by i steps. The goal is to find the minimum number of moves required to reach a given target. At first glance, it looked like a simple math problem, but the trick was to notice the parity condition — once the cumulative sum goes beyond the target, the difference (sum - target) must be even to allow flipping directions and still land exactly on target. Here’s the optimized logic I implemented in Java Key Takeaway: Sometimes, problems that look complex are just about observing patterns in numbers rather than brute force. A touch of math can simplify the entire logic! #100DaysOfCode #LeetCode #CodingChallenge #Java #ProblemSolving #DSA #LearnEveryday #CodingJourney
To view or add a comment, sign in
-
-
🔥 LeetCode Day--- 4 | “Median of Two Sorted Arrays” (Hard, Java) Today’s challenge was one of those that really test your logic, patience, and understanding of binary search. This problem wasn’t about just merging two sorted arrays — it was about thinking smarter 🧠. Instead of brute-forcing through both arrays (O(m+n)), I implemented a binary partition approach to achieve O(log(min(m, n))) efficiency 💡 What I learned today: Always choose the smaller array for binary search — it makes the partition logic simpler. Handle boundaries carefully with Integer.MIN_VALUE and Integer.MAX_VALUE. The goal is to find the perfect partition where: Left half ≤ Right half Elements are balanced across both arrays Once that’s done → median can be easily calculated! ✅ Result: Accepted | Runtime: 0 ms 🚀 Hard problem turned into a logic puzzle that was actually fun to solve! 🧩 Concepts Strengthened: Binary Search Partitioning Logic Edge Case Handling Mathematical Thinking #LeetCode #Day4 #Java #BinarySearch #ProblemSolving #CodingChallenge #DataStructures #Algorithms #CodeEveryday #DeveloperJourney #TechLearning #LeetCodeHard #CodingCommunity
To view or add a comment, sign in
-
-
Winter may be coming ❄️, but the code still runs fast ⚔️ Cracked one of the toughest LeetCode Hard problems – Median of Two Sorted Arrays 🔥 Hit 100% runtime efficiency (1 ms) with clean and optimized binary search logic 🧠 This one tested everything — edge cases, math logic, and patience 😅 But as they say… “You win or you debug again.” 🐉 #LeetCode #DSA #CodingJourney #BinarySearch #Java #ProblemSolving #WinterIsComing #GameOfCodes
To view or add a comment, sign in
-
-
📅 Day 81 of #100DaysOfLeetCode Problem: Insert into a Binary Search Tree (LeetCode #701) Approach: The task is to insert a new node with a given value into a Binary Search Tree (BST). Start from the root and recursively find the correct position: If the new value is smaller than the current node’s value, go to the left subtree. Otherwise, go to the right subtree. When a null spot is found, insert a new node there. The BST property is preserved throughout this process. Complexity: ⏱️ Time: O(h) — where h is the height of the tree. 💾 Space: O(h) — recursive call stack. 🔗 Problem Link: https://lnkd.in/dCS7zxVG 🔗 Solution Link: https://lnkd.in/dxB4ZNtV #LeetCode #100DaysOfCode #BinarySearchTree #Recursion #Java #TreeTraversal #DSA #Algorithms #CodingChallenge #ProblemSolving #CodeNewbie #StudyWithMe #BuildInPublic #LearnToCode #DailyCoding
To view or add a comment, sign in
-
-
📌 Day 16/100 - Reverse String (LeetCode 344) 🔹 Problem: Reverse a given string in-place — meaning you must modify the original array of characters without using extra space. 🔹 Approach: Used the two-pointer technique — one starting at the beginning and one at the end of the array. Swap characters at both pointers, then move them closer until they meet. Efficient, clean, and runs in linear time without additional memory allocation. 🔹 Key Learnings: In-place algorithms optimize space complexity significantly. The two-pointer pattern is a versatile tool for many array and string problems. Understanding mutable vs immutable structures in Java is crucial for memory efficiency. Sometimes, the simplest logic beats the most complex one. 🧠 “True efficiency lies in simplicity, not complexity.” #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #TwoPointers
To view or add a comment, sign in
-
-
📌 Day 14/100 - Group Anagrams (LeetCode 49) 🔹 Problem: Given an array of strings, group the anagrams together. An anagram is a word formed by rearranging the letters of another — like “eat”, “tea”, and “ate”. 🔹 Approach: Traverse through each word in the array. Sort its characters alphabetically to form a key. Use a HashMap to group words with the same key. Return all grouped lists as the final result. 🔹 Key Learning: ✅ Sorting helps identify similar string patterns. ✅ HashMaps make grouping efficient and clean. ✅ Small logical steps lead to elegant solutions. Consistent effort turns complexity into clarity 💪 #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney #LearningEveryday
To view or add a comment, sign in
-
-
Day 38 of #100daysOfCode Problem: 3542. Minimum Operations to Convert All Elements to Zero Difficulty: Medium Language: Java Status: Solved Problem Summary: You are given an integer array nums. In one operation, you can select a subarray [i, j] and set all occurrences of the minimum non-zero integer in that subarray to 0. Your goal is to make all elements zero in the minimum number of operations. Key Insight: This problem can be reduced to tracking how many times the current subarray’s minimum changes. A monotonic stack efficiently tracks the increasing sequence of elements. Each time a smaller element is encountered, it indicates that a set of operations can be completed (popped from stack). What I Learned: Monotonic stacks aren’t just for “next greater” or “next smaller” problems—they can also represent layers of operations or intervals. Elegant one-pass logic using stack simulation can drastically simplify range operation problems. #Day38 #100DaysOfCode #LeetCode #DSA #MonotonicStack #Java #Algorithms #ProblemSolving #CodingChallenge #SoftwareEngineering #LearnByCoding
To view or add a comment, sign in
-
-
🚀 Day 398 of #500DaysOfCode 🔹 Problem: 914. X of a Kind in a Deck of Cards 🔹 Difficulty: Easy 🔹 Language Used: Java ☕ 🧩 Problem Summary: Given a deck of cards where each card has an integer, the goal is to check if we can divide the deck into groups such that: Each group has exactly X cards, where X > 1, and All cards in a group have the same integer. If such a partition is possible, return true — otherwise, return false. 💡 Key Idea: The solution relies on finding the greatest common divisor (GCD) of all card frequencies. If the GCD of all counts is greater than 1, we can form valid groups; otherwise, we can’t. ⚙️ Approach: 1️⃣ Count the frequency of each card using a HashMap. 2️⃣ Compute the GCD of all frequency values. 3️⃣ If GCD > 1, return true; else, false. 🧠 Concepts Used: HashMap GCD (Euclidean Algorithm) Frequency Counting ✅ Example: Input: [1,2,3,4,4,3,2,1] → Output: true Input: [1,1,1,2,2,2,3,3] → Output: false 📘 Lesson Learned: Even simple-looking problems can hide elegant mathematical patterns. Understanding GCD turned out to be the key! 💪 #Day398 #Java #LeetCode #ProblemSolving #CodingChallenge #LearnEveryday #Programming #Developer #100DaysOfCode #500DaysOfCode
To view or add a comment, sign in
-
-
🔢 Today I worked on a classic matrix problem in Java — Diagonal Sum. The goal was simple: Calculate the sum of both the primary and secondary diagonals of a square matrix. Initially, I used a nested loop approach with O(n²) complexity. But then I optimized it to a clean O(n) solution by directly accessing diagonal indexes. While optimizing, I made an interesting mistake: ❌ I wrote if (i != matrix[i][matrix.length - 1 - i]) Here I accidentally compared an index with an element value. ✔ Correct approach: if (i != matrix.length - 1 - i) This ensures the center element in odd-sized matrices isn’t counted twice. 🧠 Key Learning: Indexes and values may look similar, but mixing them can break logic silently. Optimization is not just about speed — it’s about accuracy. #Java #leetcode #Coding #DSA #ProblemSolving #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 89 of #100DaysOfCode Solved Find Numbers with Even Number of Digits in Java 🔢 Approach The goal of this problem was to count how many integers in a given array have an even number of digits. I implemented two methods: findNumbers(int[] nums): This is the main function that iterates through every number in the input array nums. isEvenOrOdd(int n): This helper function takes an integer and determines if its digit count is even or odd. Inside the helper function, I used a while loop to count the digits: I initialized a count to 0. The loop continues as long as $n > 0$. In each iteration, I increment count and then perform integer division by 10 (n = n / 10) to remove the least significant digit. Finally, I return true if the total count of digits is even (count \% 2 == 0). This efficient, digit-by-digit checking approach resulted in a strong performance, beating 98.90% of other submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Array #ProblemSolving #Optimization
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development