#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
Maximizing Container Water with Two-Pointer Optimization
More Relevant Posts
-
Day 82 - LeetCode Journey 🚀 Solved LeetCode 92: Reverse Linked List II (Medium) — a powerful problem that takes basic reversal to the next level. We already know how to reverse an entire linked list. But here, the challenge is to reverse only a specific portion — between positions left and right — while keeping the rest intact. 💡 Core Idea (Partial Reversal + Pointer Rewiring): Use a dummy node to simplify edge cases Move a pointer (prev) to the node just before position left Start reversing nodes one by one within the given range Reconnect the reversed sublist back to the original list This is done using in-place pointer manipulation. 🤯 Why it works? Because instead of reversing the entire list, we carefully rewire only the required segment, preserving connections before and after the range. ⚡ Key Learning Points: • Partial reversal of linked list • Advanced pointer manipulation • Importance of dummy node in edge cases • In-place modification without extra space • Maintaining O(n) time and O(1) space This problem is a big step up from basic linked list reversal. Also, this pattern connects with: Reverse Linked List (full reversal) Reverse Nodes in k-Group Reorder List Palindrome Linked List ✅ Better control over pointer operations ✅ Strong understanding of in-place transformations ✅ Confidence with medium-level linked list problems From full reversal to selective reversal — this is real progress 🚀 #LeetCode #DSA #Java #LinkedList #Algorithms #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #InterviewPreparation #DeveloperGrowth #KeepCoding
To view or add a comment, sign in
-
-
🚀 Day 81 / 100 – LeetCode Daily Challenge 📌 Problem: Minimum Partitions to Decompose a Number (1689) 🧠 Concept: Greedy | String Manipulation Today’s problem was deceptively simple but conceptually rich — finding the minimum number of deci-binary numbers needed to sum up to a given string n. The key insight? 🔍 The answer is simply the maximum digit in the string! Why? Because in any addition of deci-binary numbers (each made of only 0s and 1s), the largest digit in the target number dictates how many numbers are needed in the worst-case scenario. So if the number is "82734", we need at least 8 numbers (one for each unit at the position of '8'). ✅ One pass. One max check. Clean and greedy. 📊 Runtime: 6 ms | Beats 75.56% 💾 Memory: 47.43 MB ✨ Key takeaway: Sometimes the most efficient solution is hiding in plain sight — just read the problem carefully and think about the underlying constraints. #LeetCode #CodingJourney #100DaysOfCode #Day81 #GreedyAlgorithm #Java #ProblemSolving #TechCommunity #DevLife #CodeNewbie
To view or add a comment, sign in
-
-
🚀 Day 50 / 100 – LeetCode Challenge Solved 108. Convert Sorted Array to Binary Search Tree 🌳 Today’s problem was all about building a height-balanced BST from a sorted array using a smart approach. 💡 Key Insight: Instead of inserting elements one by one, pick the middle element as root. This ensures the tree remains balanced. 🔁 Approach: Choose middle element → Root Left half → Left subtree Right half → Right subtree Apply recursively (Divide & Conquer) 🧠 Why it works? Because the array is sorted, the middle element naturally divides values into left < root < right. ⚡ Complexity: Time: O(n) Space: O(log n) ✅ Result: Accepted | 0 ms runtime | 100% performance Consistency is the real win here 💪 Halfway through the challenge! #Day50 #LeetCode #100DaysOfCode #Java #CodingJourney #DSA #BinaryTree #TechGrowth
To view or add a comment, sign in
-
-
🚀 Day 30of LeetCode Odd Even Linked List — Clean Pointer Manipulation! Just solved a really interesting linked list problem that tests how well you understand pointer manipulation 👇 🔹 Problem: Rearrange a linked list such that all nodes at odd indices come first, followed by nodes at even indices — while maintaining their relative order. 👉 Example: Input: 1 → 2 → 3 → 4 → 5 Output: 1 → 3 → 5 → 2 → 4 💡 Key Insight: Instead of using extra space, we can split the list into: an odd-index list an even-index list Then simply connect them at the end! 🧠 Approach: Maintain two pointers: odd and even Keep track of evenHead Rearrange links in one traversal Attach even list after odd list ⚡ Complexity: Time: O(n) Space: O(1) (in-place) 🎯 What I learned: Mastering linked lists is all about pointer control Problems that look tricky often have simple in-place solutions Always think in terms of structure, not values #LeetCode #DataStructures #LinkedList #Java #CodingInterview #100DaysOfCode #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
Day 32 of #75DaysofLeetCode 🚀 LeetCode 2130 – Maximum Twin Sum of a Linked List Another day, another clean Linked List trick 💡 🔍 Problem Insight: In a linked list of even length, each node has a twin: First ↔ Last Second ↔ Second Last …and so on We need to find the maximum twin sum. 🧠 Key Idea (Interview-Ready Approach): Instead of using extra space, we: 1️⃣ Find the middle of the list (slow & fast pointers) 2️⃣ Reverse the second half 3️⃣ Traverse both halves and compute the twin sums ⚡ Why this approach? ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (No extra memory!) 💯 Clean and optimal solution 📌 Example: Input: 1 → 2 → 3 → 4 Twin sums: (1+4)=5, (2+3)=5 👉 Output: 5 🔥 Takeaway: This problem is a perfect example of combining: Two pointers In-place reversal Space optimization 💬 Have you solved this using a different approach? Let’s discuss below 👇 #LeetCode #DataStructures #LinkedList #Java #CodingInterview #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
-
-
🚀 Day 21 of my #100DaysOfCode Journey Today, I solved the LeetCode question Contains Duplicate II. The task is to check if there are two equal elements in the array such that their index difference is at most k. ✅ Steps: First, iterate through each element in the array For every element, check only the next k elements Compare values within this limited range If a match is found → return true 💻 My Approach: I used a sliding window + brute force approach (without using HashMap/HashSet). Instead of checking the whole array, I optimized it by limiting the inner loop to k distance only. 🌟 Learning Takeaways: Optimizing brute force can avoid TLE Understanding constraints (like k distance) helps reduce complexity Multiple approaches exist — choosing based on space/time is key #DSA #Java #LeetCode #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 54 of #100DaysOfCode 🌱 Topic: Stack / Two Pointers ✅ Problem Solved: LeetCode 42 – Trapping Rain Water 🛠 Approach: Used a Monotonic Stack to calculate trapped water. Traversed the array and maintained a stack of indices. When current height is greater than stack top: Popped the top → this forms a “valley”. If stack becomes empty → break (no left boundary). Calculated: Height = min(left boundary, right boundary) − current height Width = distance between boundaries Added height × width to total water. Pushed current index into stack. This effectively finds water trapped between bars. #100DaysOfCode #Day54 #DSA #Stack #TwoPointers #LeetCode #HardProblems #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 8 of #100DaysOfCode Solved LeetCode Problem 69 – Sqrt(x) today using a simple loop approach! 🔍 Problem Summary: Given a non-negative integer "x", return the square root of "x" rounded down to the nearest integer (without using built-in functions). 💡 What I practiced: - Basic iteration using loops - Handling overflow using "long" - Understanding how brute force works before optimizing ⚡ Approach (Brute Force): Start from "i = 0" and keep checking: "i * i <= x" Stop when it exceeds "x", and return "i - 1" 💻 Java Code: public static int mySqrt(int x) { long i = 0; while (i * i <= x) { i++; } return (int)(i - 1); } 📌 Takeaway: Starting with a simple loop helps build clarity. Optimization (like Binary Search) can come next! #LeetCode #Java #100DaysOfCode #CodingJourney #BasicsFirst #ProblemSolving
To view or add a comment, sign in
-
-
🔥 Day 59/100 – LeetCode Challenge 📌 Problem Solved: Remove Duplicates from Sorted Array II (Medium) Today’s problem was a great exercise in in-place array manipulation and two-pointer technique. 💡 Key Idea: Since the array is already sorted, duplicates are adjacent. Instead of removing all duplicates, we allow each element to appear at most twice. 👉 The trick is to compare the current element with the element at index k-2. If they are the same → skip ❌ If different → keep it ✅ ⚙️ Approach: Initialize pointer k = 2 Traverse from index 2 Copy valid elements forward Maintain order without extra space 🧠 What I learned: How to efficiently handle constraints like “at most twice” Importance of thinking in terms of index relationships (k-2) Writing optimal O(n) solutions with O(1) space 📊 Performance: ⚡ Runtime: 0 ms (100%) 💾 Memory: 48.46 MB 💻 Tech Used: Java Consistency is key 🔑 — 59 days done, 41 more to go! #100DaysOfCode #LeetCode #Java #DataStructures #CodingChallenge #ProblemSolving #TechJourney
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