Day 4 of 30-day Coding Sprint Today was a deep dive into Maximum Product Subarray—a classic problem that looks simple until you hit zeros and negative numbers. Today's progress on Leetcode: 152. Maximum Product Subarray - The Brute Force: Nesting loops to check every single subarray. At O(N^3) (or O(N^2) with optimization), it's too much for large datasets. - The Observation: Unlike sums, products are volatile. A single zero resets everything, and a negative number can turn a tiny product into a maximum (or vice versa). - The Optimal Strategy (Prefix & Suffix): By calculating products from both the front (Prefix) and the back (Suffix), you naturally handle the "even vs. odd" negative number count. The Zero Reset: Whenever a product hits 0, I reset to 1 to start a fresh subarray. Result: Clean O(N) time complexity with O(1) space. #30DaysOfCode #DSASprint #LeetCode #JavaScript #Algorithms #Optimization #Consistency
30-Day Coding Sprint: Maximum Product Subarray Optimization
More Relevant Posts
-
Day 25 of 30-days Coding Sprint Today was about finding Distinct Pairs with Difference K, a problem that forces you to handle duplicates carefully to avoid overcounting. The Goal: Find unique pairs (a, b) such that b - a = k. The keyword here is distinct; if we have multiple [1, 5] pairs, we only count them once. Approach 1: The Hash Map (Frequency Logic) - The Strategy: Count the frequency of every number. - The Check: 1. If k > 0, check if map[element + k] exists. - If k = 0, check if map[element] > 1 (since a number minus itself is 0). Pros: Great for O(N) time, but requires extra space for the map. Approach 2: Two-Pointer (Sorted Space) The Strategy: Sort the array first to use the "Expand/Shrink" logic. The Decision Logic: If diff == k: Found a pair! Increment count and skip duplicates using a while loop to maintain uniqueness. If diff < k: Need a larger difference, so move the right pointer. If diff > k: Difference is too big, move the left pointer. Complexity: O(N log N) for sorting, but O(1) auxiliary space. #30DaysOfCode #DSASprint #LeetCode #JavaScript #TwoPointers #Algorithms #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 8 of 30-day Coding Sprint Today’s focus was on a classic problem that teaches you why linear time isn't always good enough when dealing with exponents. Today's progress on LeetCode: 50. Pow(x, n) The Simple Recursive Approach: Multiplying x by itself n times. - Complexity: O(n) time. - The Issue: For large values of n, this hits the stack limit or simply takes too long. The Optimal Strategy: Binary Exponentiation (Divide & Conquer) - The Logic: Use the property (x^n) = (x^n/2)^2. By halving n at each recursive step, we drastically reduce the number of multiplications. The Result: O(log n) time. This turns a billion operations into roughly 30. #30DaysOfCode #DSASprint #LeetCode #JavaScript #Recursion #Math #Consistency
To view or add a comment, sign in
-
-
Day 17 of 30-day Coding Sprint Today I’ve shifted focus to one of the most efficient techniques in a developer's toolkit: The Sliding Window. 3. Longest Substring Without Repeating Characters - The Challenge: Finding the length of the longest substring where every character is unique. A naive O(n^2) approach would be too slow for large strings. - The Strategy: Optimized Sliding Window Used a Frequency Hash Array (represented as a 256-element array) to store the last-seen index of each character. Two pointers, l (left) and r (right), define the current window. - The Optimization: Instead of moving the left pointer l one by one when a duplicate is found, I "jump" 1 directly to hash[s[r]] + 1. This ensures that the window always contains unique characters in the most efficient way possible. Result: A clean O(n) time complexity with a single pass through the string. Note: The sliding window isn't just about moving pointers; it's about maintaining a state (in this case, uniqueness) and shrinking or expanding the boundaries to satisfy that state. Using a hash array to store indices turns an O(n) "shrink" into an O(1) "jump." #30DaysOfCode #DSASprint #LeetCode #JavaScript #SlidingWindow #Optimization #Consistency
To view or add a comment, sign in
-
-
Day 184: Moving Nodes and Counting Words I am now on Day 184! Today, I practiced more Linked List logic and started working with Strings. Here is what I did today in very simple steps: 1. Rotate List (LeetCode 61) 🔄 I learned how to take the end of a list and move it to the front. How? First, I find the length of the list. Then, I find the right place to "cut" the list and connect the end back to the start. 2. Swap Nodes in Pairs (LeetCode 24) 🤝 I learned how to swap every two nodes. For example, 1 -> 2 -> 3 -> 4 becomes 2 -> 1 -> 4 -> 3. The Trick: I used a "Sentinel" node to keep track of the head. I also tried a Recursive version, which made the code very short and clean! 3. Length of Last Word (LeetCode 58) 📏 I moved to Strings! I had to find the length of the very last word in a sentence. Manual Way: Instead of using easy shortcuts, I started from the end of the string, skipped the empty spaces, and counted the letters until I hit another space. It is a great way to practice loops. My takeaway: Whether it is cutting a list or counting letters backward, logic is all about finding the right starting point! #JavaScript #Coding #Programming #WebDevelopment #DataStructures #Algorithms #SoftwareEngineer #Logic #SimpleLearning #StringManipulation #LinkedList #TechCommunity #DailyCoding #ProblemSolving #CareerGrowth #CodeNewbie
To view or add a comment, sign in
-
Sorting, searching, arrays, and linked lists are not just academic topics — they are essential for writing efficient code. Trainitec Magic teaches these topics with visual explanations and coding challenges so you understand how they work in real applications. Practice JS algorithms with guided exercises today. #JavaScript #Algorithms #DataStructures #TrainitecMagic #CodingLogic
To view or add a comment, sign in
-
-
Day 27 of 30-day Coding Sprint 76. Minimum Window Substring - The Goal: Find the smallest substring in s that contains all characters of t (including duplicates). - The Strategy: Expand & Contract - Preprocessing: Build a frequency map (using a 256-size array) for all characters in t. - Expansion (r pointer): Move the right pointer to expand the window. If the current character is part of t and we still need more of it, increment our count. - Contraction (l pointer): Once count == m (meaning the window is "valid"), try to shrink it from the left to find the minimum possible length. - The "Squeeze": As we move l, we update our frequency map. If moving l causes us to lose a required character from t, the window becomes invalid, and we go back to expanding. Result: Efficient O(N + M) time complexity and O(256) space. #30DaysOfCode #DSASprint #LeetCode #JavaScript #SlidingWindow #HardProblem #Algorithms #Consistency
To view or add a comment, sign in
-
-
Day 181: Of Loops, Laps, and Mirror Images Day 181, and the complexity is ramping up! Today was all about optimising Linked List patterns—moving away from "easy-but-heavy" memory usage toward "lean-and-mean" $O(1)$ space complexity. 🕵️♂️ Detecting the Infinite Loop (LeetCode 141) How do you know if a list goes on forever? The "Set" Approach: Carry a notebook (Set) and write down every node you visit. If you see a name twice, you’re in a loop. Simple, but it costs $O(n)$ space. Floyd’s Cycle-Finding Algorithm: The "Tortoise and the Hare." You put two pointers on the track. If there’s a loop, the fast pointer (the Hare) will eventually "lap" the slow pointer (the Tortoise) and they’ll meet. It’s $O(1)$ space magic. 🪞 The Palindrome Puzzle (LeetCode 234) Checking if a list is the same forward and backward (like 1 -> 2 -> 2 -> 1) is trickier than it looks without an array. The Pro Move: 1. Use Slow/Fast pointers to find the exact middle. 2. Reverse the entire second half of the list. 3. Walk through both halves simultaneously and compare values. By "re-wiring" the second half, I can check for symmetry without creating a single extra piece of data. This brings the space complexity down from $O(n)$ to $O(1)$. 💡 Key Takeaway Coding isn't just about solving the problem; it's about solving it efficiently. Today taught me that the most elegant solutions often involve manipulating the structure you already have rather than building new ones. #JavaScript #DSA #LeetCode #SoftwareEngineering #WebDev #Algorithms #DataStructures #CodingJourney #Optimization #CleanCode #LinkedList #ProblemSolving #TechSkills #CareerGrowth #ProgrammerLife #EngineeringExcellence
To view or add a comment, sign in
-
🚀 Major CODEZY Update: Completely moved to standard Problem stucture While building CODEZY, I hit a problem that seemed simple at first but turned out to be deeply architectural. I tried multiple ways to execute user code, tweaking inputs, hardcoding drivers, adjusting schemas, but something always felt off. It worked on desktop, broke on edge cases, and clearly wouldn’t scale. That’s when I studied how platforms like LeetCode and HackerRank structure their problems. The key insight was simple but powerful: users never write the full program. They only write the function. The rest is handled by a driver. We refactored CODEZY’s schema into startCode and driverCode, further splitting the driver into languageId, headerCode, and footerCode. Now a single, reusable driver per language can handle any number of test cases, bringing CODEZY in line with standard competitive coding platforms. Keep Learning. Keep Exploring. CODEZY 🚀 www.codezy.space #SystemDesign #DSA #CompetitiveProgramming #FullStack #LearningInPublic
To view or add a comment, sign in
-
-
Object.freeze() VS Object.seal() 👇 - In freeze, object's structure and values are totally locked. existing properties can't be changed or deleted. new properties can't be added. - In seal, only object's structure is locked. existing properties can be changed but not deleted. new properties can't be added. Important Points 👇 - Both freeze and seal are shallow which means they only affect the outer main object and not the nested objects. - The structure and properties of nested objects can still be changed. - To make seal or freeze deep, we have to apply it recursively on all nested objects through functions or loops. Hitesh Choudhary Piyush Garg Chai Aur Code Akash Kadlag Jay Kadlag #JavaScript #WebDevelopment #FrontendDevelopment #WebDev #Coding #Programming #Developer #LearnJavaScript #LearningInPublic #100DaysOfCode #TechCommunity #SoftwareDevelopment #ObjectOrientedProgramming #ChaiAurCode #ReactJS
To view or add a comment, sign in
-
🚀 LeetCode: Subarray Sum The goal is to find a contiguous subarray that adds up to a specific target sum and return its 1-indexed positions. This is a classic problem that tests your ability to manage a dynamic range within an array. 🛠️ My Approach 1. Sliding Window Initialization: I initialized two pointers, s (start) and e (end), both starting at the beginning of the array to represent a dynamic window. 🔡 2. Window Expansion: I used a for loop to move the end pointer e forward, adding each element to a running sum to expand the window. 🧹 3. Dynamic Shrinking: If the current sum exceeded the target, I used a while loop to move the start pointer s forward, subtracting elements from the sum until it was no longer greater than the target. 📍 ❌ If the sum exactly matches the target, we return the 1-indexed positions [s + 1, e + 1] immediately. 📊 Efficiency Analysis ⏱️ Time Complexity: O(n) 💾 Space Complexity: O(1) #LeetCode #JavaScript #CodingLife #Algorithms #WebDevelopment #ProblemSolving #SoftwareEngineering #TechCommunity
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