🚀 Day 77 of #100DaysOfCode Today’s challenge was a classic two-pointer optimization problem — LeetCode: Container With Most Water 💧 📌 Problem Summary You’re given an array where each element represents the height of a vertical line. The task is to find two lines that together with the x-axis form a container that holds the maximum amount of water. 🧠 Approach Used: Two Pointers Instead of checking all possible pairs (which would be inefficient), I used a two-pointer strategy: Start with pointers at both ends of the array Calculate the area using: area = min(height[left], height[right]) × (right − left) Move the pointer pointing to the smaller height, because that’s the only way to possibly increase the area This greedy logic drastically reduces unnecessary comparisons. ⚙️ Complexity ⏱ Time: O(n) 💾 Space: O(1) 🔥 Key Learnings Brute force isn’t always the answer — patterns like two pointers can cut complexity instantly Greedy decisions work well when backed by solid reasoning Pointer movement logic is just as important as the formula itself Another strong problem solved 💪 On to Day 78 🚀 #100DaysOfCode #LeetCode #TwoPointers #Java #DSA #ProblemSolving #CodingJourney
Maximizing Container Area with Two Pointers
More Relevant Posts
-
Day 117 🚀 | LeetCode Progress Solved 3 problems today and strengthened array manipulation skills 💪 📌 Problem 1: Sort Array By Parity (#905) – 🟢 Easy 👉 Two-pointer / index swapping technique 👉 Place even numbers at the front while iterating 👉 In-place solution with O(1) extra space 👉 Time Complexity: O(n) Efficient way to partition arrays without extra memory ⚡ 📌 Problem 2: Find All Numbers Disappeared in an Array (#448) – 🟢 Easy 👉 Index marking technique using negative values 👉 Mark visited indices by flipping the sign 👉 Positive indices at the end represent missing numbers 👉 Time: O(n) | Space: O(1) (excluding result list) A clever trick to use the array itself as a hash map 🧠 📌 Problem 3: Minimum Changes to Make an Alternating Binary String (#1758) – 🟢 Easy 👉 Compare with two possible patterns: "010101..." and "101010..." 👉 Count mismatches for both patterns 👉 Return the minimum operations needed 👉 Time Complexity: O(n) Simple logic but great practice for pattern-based string problems 🔍 Small improvements every day lead to big progress over time 📈 #Day117 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #GeekStreak #100DaysOfCod
To view or add a comment, sign in
-
🚀 Day 89 of #100DaysofLeetCode Problem: Minimum Size Subarray Sum Difficulty: Medium Today’s challenge was about finding the smallest length subarray whose sum is greater than or equal to a given target. Since all elements are positive, this problem is a perfect fit for the Sliding Window technique. Instead of checking all possible subarrays (which would be inefficient), I used two pointers: Expand the window by moving the right pointer and adding to the current sum. Once the sum becomes ≥ target, shrink the window from the left to find the minimum possible length. Keep updating the minimum length during this process. Key takeaways: Positive integers allow efficient window shrinking. Two pointers help reduce time complexity from O(n²) to O(n). Maintain a running sum and dynamically adjust window size. Return 0 if no valid subarray exists. Time Complexity: O(n) Space Complexity: O(1) Another solid sliding window problem completed 💪 #100DaysOfCode #100DaysOfLeetCode #LeetCode #DSA #SlidingWindow #TwoPointers #Java #ProblemSolving #CodingJourney #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 36 of #100DaysOfLeetCode ✅ Solved: Plus One (LeetCode 66) Difficulty: Easy Status: Accepted (114/114 Testcases Passed) Runtime: 0 ms 💯 Today’s problem looked simple but reinforced an important concept — handling carry in arrays. 🧠 Problem Summary: We are given a large integer represented as an array of digits. We need to increment the number by one and return the updated array. 🔎 Key Insight: Start from the last digit (right to left): If digit < 9 → increment and return. If digit == 9 → set it to 0 and carry over. If all digits are 9 → create a new array with an extra digit at the beginning. 💡 Example: Input: [9,9,9] Output: [1,0,0,0] 🎯 What I Practiced Today: Reverse traversal of arrays Carry-forward logic Edge case handling (all 9s case) Writing optimized O(n) solution Even easy problems strengthen fundamentals when you focus on edge cases. Consistency > Complexity. 🔥 #Day36 #100DaysOfCode #LeetCode #Java #DataStructures #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
📌 LeetCode Daily Challenge — Day 4 Problem: 1582. Special Positions in a Binary Matrix Topic: Array, Matrix 🧠 Approach (Simple Thinking): 🔹 A position is special only if it holds a 1 that is alone in its entire row AND its entire column 🔹 Checking row and column for every cell separately is slow and repetitive 🔹 So we pre-compute rowSum and colSum in one pass before making any decisions 🔹 rowSum[i] == 1 means no other 1 exists in that row 🔹 colSum[j] == 1 means no other 1 exists in that column 🔹 If mat[i][j] == 1 and both sums equal 1 — that's your special position 🔹 Preprocessing once and reusing is the real trick here ⏱️ Time Complexity: Two passes through the full matrix → O(m × n) Every cell is visited exactly twice, nothing more 📦 Space Complexity: Two small arrays for row and column sums → O(m + n) No recursion, no extra grid, just two lightweight arrays doing all the work I wrote a full breakdown with dry run, analogy and step by step code walkthrough here: https://lnkd.in/gFgQxQRP If you approached this differently or have a cleaner way to think about it, drop it in the comments — always curious to see different perspectives 💬 See you in the next problem 👋 #LeetCode #Java #SoftwareEngineer #ProblemSolving #BackendDeveloper
To view or add a comment, sign in
-
-
#Day32 of #365DaysOfCode Today’s LeetCode Practice: 🔹 Container With Most Water (LeetCode 11) Solved a classic two-pointer optimization problem where the goal is to find two lines that together form a container holding the maximum water. 💡 Key Insight: Start with two pointers at both ends. Calculate area using: width × min(height[left], height[right]) Move the pointer with the smaller height inward, because the smaller height limits the water capacity. This guarantees exploring better possibilities efficiently. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Consistency > Motivation. Day by day, improving problem-solving and logical thinking skills #LeetCode #ProblemSolving #Java #CodingJourney #FutureEngineer #Consistency #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 37 — LeetCode Practice 🚀 Today’s focus was on strengthening linked list fundamentals and mastering pointer manipulation. ✅ Problem solved today: 🔹 Merge Two Sorted Lists 💡 Key learnings from today: • Strengthened understanding of the two-pointer technique in linked lists • Learned how using a dummy node simplifies head handling • Practiced merging nodes without creating a new list • Improved clarity on pointer movement and reference updates • Handled edge cases like one list being null Initially, I was carefully thinking about how to manage the head of the merged list. Then I realized that using a dummy node removes unnecessary conditional checks and makes the logic much cleaner. That small structural decision made the implementation simpler and more readable — achieving O(n + m) time complexity with O(1) extra space. This problem reminded me: Sometimes clean structure matters more than complex logic. Better pointer control. Cleaner implementation. Stronger linked list foundation. 💪 On to Day 38 🚀 #Day37 #DSA #LeetCode #ProblemSolving #Java #LinkedList #CodingJourney #Consistency #FutureEngineer
To view or add a comment, sign in
-
-
#100DaysOfCode 🚀 👉Solved: LeetCode #16 – 3Sum Closest After solving the classic 3Sum problem earlier, this variation pushed me to think slightly differently. Instead of finding an exact triplet sum equal to 0, this time the goal was to find the sum *closest* to a given target. 🔹 Approach: • Sorted the array • Fixed one element at a time • Applied the Two Pointer technique • Continuously tracked the minimum absolute difference Key Insight: Sometimes optimization problems are not about exact matches, but about minimizing deviation. Time Complexity: O(n²) Space Complexity: O(1) This problem strengthened my understanding of: ✔ Two Pointers ✔ Greedy comparison logic ✔ Edge case handling What I learned: Small variations in problem statements can completely change the thinking approach. Classic 3Sum focuses on exact zero. 3Sum Closest focuses on minimizing |sum - target|. #LearnInPublic #DSA #Java #LeetCode #TwoPointers #ProblemSolving #CodingJourney #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 44 Out of #365DayOfCode - LeetCode Today I worked on the Set Matrix Zeroes problem, where the goal is to modify a 2D matrix such that if any element is 0, its entire row and column are set to 0. I implemented an efficient solution with O(m × n) time complexity and optimized space usage by carefully handling matrix traversal and edge cases. This problem helped me strengthen my understanding of: 2D array manipulation In-place updates Optimizing space complexity Handling edge cases in algorithms Consistent practice is helping me improve my problem-solving skills step by step. 🚀 #DSA #Java #ProblemSolving #CodingPractice #365DaysOfCode Github link: https://lnkd.in/gGUy_MKZ
To view or add a comment, sign in
-
-
🚀 Day 521 of #750DaysOfCode 🚀 🔎 1582. Special Positions in a Binary Matrix (LeetCode - Easy) Today I solved a simple yet interesting matrix traversal problem. 🧠 Problem Summary We are given an m × n binary matrix. A position (i, j) is called special if: ✔ mat[i][j] == 1 ✔ All other elements in row i are 0 ✔ All other elements in column j are 0 Our task is to count how many such special positions exist. 💡 Key Idea Instead of checking the entire row and column every time, we can: 1️⃣ Count the number of 1s in every row 2️⃣ Count the number of 1s in every column Then a cell (i, j) is special if: mat[i][j] == 1 rowCount[i] == 1 colCount[j] == 1 ⏱ Time Complexity O(m × n) We traverse the matrix twice. 🧩 What I Learned ✔ Efficiently using row and column counting ✔ Simplifying matrix conditions with precomputation ✔ Writing cleaner solutions for matrix-based problems Step by step progress every day. Consistency is the real key. 🚀 #LeetCode #Java #MatrixProblems #ProblemSolving #DataStructures #CodingJourney #750DaysOfCode #Day521
To view or add a comment, sign in
-
-
Day 24/100 🚀 | #100DaysOfDSA Solved Array Partition (LC 561) This one looked extremely straightforward — sort the array and pair adjacent elements. But the real insight was understanding why sorting guarantees the maximum possible sum. Problems I Faced: • Initially overthought it — tried to reason about greedy pairing without sorting. • Took a moment to understand why pairing smallest with next smallest maximizes the overall minimum sum. • Resisted the urge to complicate it with extra logic. Final Approach: • Sorted the array. • Iterated in steps of 2. • Added every alternate element (minimum of each pair after sorting). • Kept the solution clean and simple. Key Insight: After sorting, pairing adjacent elements ensures the smaller value in each pair is as large as possible — maximizing the total sum. Time Complexity: O(n log n) Space Complexity: O(1) (ignoring sorting overhead) Not every problem needs complexity. Sometimes clarity is the real skill. Consistency continues. 💪 #100DaysOfCode #LeetCode #DSA #Java #Arrays #Greedy #ProblemSolving
To view or add a comment, sign in
-
Explore related topics
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