Day 45 of LeetCode grind — “Make Array Elements Equal to Zero” (Problem #3354) This one looked like a simulation problem at first glance, but it’s actually an elegant math trick hiding under a messy description. Instead of simulating all that left-right bouncing chaos, you just compare prefix sums — figuring out where the array can be “balanced” around a zero. Key takeaway: Sometimes the shortest code comes from refusing to believe the problem is as complicated as it sounds. ✅ Accepted 🧠 Concepts: Prefix Sum, Balance Points 💬 Lesson: Elegant logic > brute force chaos Slowly crawling my way through 100 days of consistency — still standing, still debugging. #LeetCode #100DaysOfCode #ProblemSolving #Cplusplus #CodingJourney
Solved LeetCode 3354 with elegant math trick, not simulation.
More Relevant Posts
-
I recently tackled LeetCode 2654 – Minimum Number of Operations to Make All Array Elements Equal to 1. Most solutions simulate every operation. The approach that worked for me came from stepping back and thinking mathematically: 1️⃣ If there’s already a 1 in the array, it can be spread to all other elements efficiently. 2️⃣ If no 1 exists, find the shortest subarray whose GCD is 1 – creating a 1 from that subarray is the minimal first step. 3️⃣ Once a 1 exists, the rest follows in a straightforward way. It’s a reminder that sometimes understanding the structure of a problem beats brute-force coding. And here’s a little validation: ✅ Runtime: 1ms, beats 100% of submissions ✅ Memory: 56.1MB, beats 100% of submissions For anyone doing coding challenges or preparing for interviews, focus on insights like these – they often save more time than writing extra lines of code. #ProblemSolving #Algorithms #CodingMindset #LeetCode #SoftwareEngineering
To view or add a comment, sign in
-
-
💯 Day 7/100: Dynamic Programming — LeetCode 474 Today’s challenge was a classic optimization puzzle: selecting the largest subset of binary strings under strict limits on the number of 0s and 1s. It’s essentially a two-dimensional knapsack problem, where each string consumes a portion of your limited resources. The key breakthrough was realizing that you need to traverse the DP table in reverse to preserve state dependencies. Each update reflects the best outcome when including a new string, without overwriting previous possibilities. This problem sharpened my understanding of multi-dimensional constraints and reinforced the importance of state reuse in dynamic programming. It’s a great reminder that even medium-difficulty problems can hide deep algorithmic insights. Onward to Day 8 — the grind continues. #100DaysOfCode #LeetCode #DynamicProgramming #TechJourney #ScarCodes #ProblemSolving #CodeChallenge #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 69 of #100DaysOfLeetcodeHard — LeetCode 2167: Minimum Time to Remove All Cars Containing Illegal Goods (Hard) My Submission: https://lnkd.in/gi65MdhU Today’s problem was a really interesting dynamic programming and greedy optimization mix — determining the minimum time needed to remove all cars containing illegal goods. 🧩 Problem Summary: We’re given a binary string s where '1' represents a car with illegal goods. We can: 1️⃣ Remove a car from the left (cost = 1) 2️⃣ Remove a car from the right (cost = 1) 3️⃣ Remove a car from anywhere (cost = 2) 💡 Observation: If we think greedily, the left operation should be performed only once. Once we’ve started removing from the left, every subsequent decision should only consider either the cost-2 (middle) operation or right-side removals. Similarly, once a cost-2 operation begins, performing a left removal afterward would only increase the total cost — so it’s never optimal. 🧠 Approach: The problem reduces to balancing these three operations efficiently using DP — minimizing the total cost while following the above greedy principle. We iterate through the string while maintaining the minimum cost up to each index. 📈 Complexity: Time: O(n) Space: O(n) A solid problem that forces you to reason about operation optimality and transition design — blending greedy logic with dynamic programming. #LeetCode #DynamicProgramming #Greedy #ProblemSolving #Algorithms #CodingChallenge #Cplusplus #100DaysOfCode #LearningEveryday
To view or add a comment, sign in
-
-
💡 LeetCode Daily Practice — Problem #283: Move Zeroes Back again with another solved LeetCode problem! Today’s challenge was “Move Zeroes”, a simple yet logical question that focuses on array manipulation and in-place operations. 🧩 Problem statement: Given an integer array, move all the zeros to the end while maintaining the relative order of the non-zero elements — without creating a copy of the array. ⚙️ Approach: I used a single-pass solution that keeps track of the position where the next non-zero element should be inserted. Traverse the array once and move all non-zero elements to the front. After all non-zero elements are placed, fill the remaining positions with zeros. These kinds of problems are great for strengthening in-place array manipulation skills and improving problem-solving speed. #LeetCode #ProblemSolving #Coding #C++ #DSA #Arrays #ProgrammingJourney
To view or add a comment, sign in
-
-
It's Easy To Recognise Patterns 💪 Yes it's True But we unknowingly make some mistakes We'll talk about it some other day 🙃 Today let's focus on the patterns I found in the LeetCode weekly contest 476 For in detail explain watch this yt video https://lnkd.in/gcNhp6ET Q1 : Check The expression and Find out what to be done to get the max value possible Q2 : 2 strategies 1- Look at the root level and you will find that removing adjacent values works So use stack for adjacent 2- look what's the change to the rest of the string if we remove the current substring and this will lead you to abs(cnta-cntb) formula although it has an intuition as well, which I have shared in video Q3 : It's a direct digit dp question why ?? 1- we have to make numbers by choosing digits at every position 2- the constraints are high although you can use math as well which I have talked about in the video 😅 Q4 : We have to use prefix sum but it will cause some extra stable arrays so remove them as well how we thought about the prefix and all?? 🤣 I have talked in the video as well #leetcode #contest #cf #cp #dsa #coding
To view or add a comment, sign in
-
-
On Day 285 of my #300daysofcode challenge, I'm sharing my solution to LeetCode Problem 2048, "Next Greater Numerically Balanced Number." This problem requires a focused iterative search and a helper function to verify the unique "balanced" property (where each digit $d$ appears $d$ times). My video provides a clear walkthrough of this logical method, a valuable skill for any developer and a great way to practice writing clean, reusable validation code. #DataStructures #Algorithms #LeetCode #CodingInterview #Programming #ProblemSolving #300daysofcode
To view or add a comment, sign in
-
🟩 Day 56 of Leetcode 150 days challenge – Add Two Numbers (LeetCode #2) #LeetCode150 #Day56 #AddTwoNumbers #LinkedList #DataStructures #Algorithms #ProblemSolving #LeetCode #CodingChallenge #CPP #Programming #150DaysOfCode #LearnDSA #SDEPrep #CodingJourney 🔹 Problem: Given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807 🧠 Approach: We perform the addition just like we do on paper: Initialize a carry as 0. Traverse both lists simultaneously. Add the digits from both lists + carry. Store the last digit in a new node. Update carry = sum / 10. Continue until both lists are done and carry is 0. 🕒 Time Complexity: O(max(M, N)) 💾 Space Complexity: O(max(M, N)) 🏁 Key Takeaway: Linked list addition is a classic problem that strengthens your understanding of pointer manipulation, carry handling, and edge cases like unequal list lengths. 💡
To view or add a comment, sign in
-
-
🚀 Day 63 of #100DaysOfCode Today I solved a C++ problem: 👉 Find K Pairs with Smallest Sums 🧩 Problem Summary: Given two sorted arrays arr1 and arr2, and an integer k, the task is to find k pairs (a, b) where a belongs to arr1 and b belongs to arr2, such that their sum is among the smallest possible. 🧠 Approach: I used a min-heap (priority_queue with greater comparator) to always extract the smallest pair sum efficiently. Start by pushing the smallest possible pairs from arr1 and the first element of arr2. Each time we pop the smallest pair, we add the next possible pair from the same row. Continue until we find k pairs. ⚙️ Key Concepts: Priority Queue (Min-Heap) Pair Sum Optimization Efficient use of STL 💡 Complexity: Time: O(k log k) Space: O(k) 👨💻 Language: C++ (STL-based) #100DaysOfCode #Day63 #CPlusPlus #STL #CodingChallenge #DSA #ProblemSolving #LeetCode #CodeNewbie #Programmer #LearnCoding #CompetitiveProgramming #cpp #DataStructures #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 62 of #100daysChallenge 🚀 📌 Today’s Problem: Add Two Numbers (Linked List) ✅ Results: Runtime: 0 ms (Beats 💯%) Memory: 12.66 MB (Beats 74.14%) All 1563 test cases passed successfully! Key Takeaway: Today’s problem reinforced how crucial it is to understand linked list traversal and pointer manipulation. By reversing the lists first and performing digit-wise addition, we can elegantly handle carry propagation. This challenge truly strengthens both logic and precision — two pillars of clean problem-solving. 🚀 #100DaysOfLeetCode #CProgramming #LinkedList #ProblemSolving #CodingChallenge #DSA #DeveloperJourney #Consistency #KeepLearning #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 18 of my 120 Days of LeetCode Challenge 📘 Problem 18: 4Sum Difficulty: Medium 🧩 Problem Statement: Given an integer array nums of length n, return all unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that: 0 <= a, b, c, d < n a, b, c, and d are distinct nums[a] + nums[b] + nums[c] + nums[d] == target You may return the answer in any order. Example: Input: nums = [1, 0, -1, 0, -2, 2], target = 0 Output: [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]] 💡 Approach Used: To solve this problem efficiently, I used a combination of sorting and the two-pointer technique (an optimized version of the 3Sum approach): Sort the array to handle duplicates and simplify traversal. Fix two numbers using two nested loops. For the remaining two numbers, apply the two-pointer approach (left and right) to find pairs that make up the target sum. Skip duplicates for all four numbers to ensure only unique quadruplets are added to the result. Use long long to avoid integer overflow when numbers are large. This structured and optimized method ensures that all valid quadruplets are found without redundancy. ⏱️ Time Complexity: O(n³) → Sorting takes O(n log n), and the nested loops with two-pointers make it cubic overall. 💾 Space Complexity: O(1) (excluding the result storage) since only pointers and a few variables are used. ✨ Key Takeaway: This problem builds upon the logic of 3Sum and extends it to a higher combination level, helping improve understanding of multi-pointer techniques and nested search optimizations. #LeetCode #Day18 #4Sum #C++ #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #TwoPointer #Sorting #Programming
To view or add a comment, sign in
-
More from this author
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