🔥 Day 95 of my 100 Days of Code Problem: Container With Most Water (LeetCode #11) Problem Description: You are given an integer array height of length n. Each element represents a vertical line on the x-axis. Find two lines that together with the x-axis form a container that holds the most water. Return the maximum amount of water that can be contained. Java Solution: class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; int maxWater = 0; while (left < right) { int h = Math.min(height[left], height[right]); int w = right - left; maxWater = Math.max(maxWater, h * w); // Move the pointer pointing to the shorter line if (height[left] < height[right]) { left++; } else { right--; } } return maxWater; } } Time Complexity: O(n) Space Complexity: O(1) #Day95 #LeetCode #100DaysOfCode #Java #TwoPointers #Array
Container With Most Water Problem Solved in Java
More Relevant Posts
-
🔹 Day 28: Reverse Vowels of a String (LeetCode #345) 📌 Problem Statement: Given a string s, reverse only the vowels in the string and return the result. The vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ (both uppercase and lowercase). ✅ My Approach: Used the two-pointer technique — one pointer starting from the beginning and the other from the end. Moved both pointers toward each other, swapping vowels when found. This approach efficiently handles the reversal while maintaining other characters’ positions. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(n) (due to the character array conversion) ⚡ Submission Stats: Runtime: 3 ms (Beats 83.43%) Memory: 44.93 MB (Beats 75.31%) 💡 Reflection: This problem reinforced the use of two-pointer logic in string manipulation. It’s a simple yet elegant example of how to efficiently handle conditional swaps in strings. ✨ #LeetCode #Java #TwoPointers #StringManipulation #DSA #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🔹 Day 46: Is Subsequence (LeetCode #392) 📌 Problem Statement: Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence is formed by deleting some (possibly none) characters from the original string without disturbing the relative order of the remaining characters. ✅ My Approach: I used a two-pointer technique — one pointer iterates through string t, and the other tracks progress through string s. Each time a matching character is found, the pointer for s moves forward. If we reach the end of s, it means all its characters appeared in sequence within t. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 68.53%) Memory: 41.32 MB (Beats 87.28%) 💡 Reflection: A simple yet elegant problem that highlights how pointer movement can efficiently handle string comparisons without extra memory usage. ✨ #LeetCode #Java #Strings #TwoPointers #100DaysOfCode #Day46
To view or add a comment, sign in
-
-
✨ Day 58 of 100: Partition List ✨ Today’s challenge was LeetCode 86 – Partition List 🧩 Problem Statement: Given the head of a linked list and a value x, partition it such that all nodes with values less than x come before nodes with values greater than or equal to x. The relative order of nodes in each partition should remain the same. 💡 Key Takeaways: 🔹 Practiced two-pointer technique to maintain separate lists for nodes < x and ≥ x. 🔹 Improved understanding of in-place linked list manipulation. 🔹 Strengthened grasp of dummy node usage to simplify edge cases. 🧠 Approach: 1️⃣ Create two dummy lists – one for smaller nodes and one for greater/equal nodes. 2️⃣ Traverse the original list and link nodes accordingly. 3️⃣ Finally, connect the smaller list’s tail to the head of the greater list. 🔥 Learning: This problem reinforced how clean list partitioning can be done efficiently with dummy nodes and careful pointer handling — all in O(n) time and O(1) extra space! #100DaysOfCode #Day58 #LeetCode #Java #LinkedList #CodingChallenge #ProblemSolving #DSA #PartitionList
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfLeetCode Problem: 125. Valid Palindrome 🧩 Goal: Check if a given string is a palindrome — ignoring spaces, punctuation, and case. 💻 Approach: 1️⃣ Remove all non-alphanumeric characters. 2️⃣ Convert to lowercase. 3️⃣ Compare the cleaned string with its reverse. class Solution { public boolean isPalindrome(String s) { StringBuilder cleaned = new StringBuilder(); for (char c : s.toCharArray()) { if (Character.isLetterOrDigit(c)) { cleaned.append(Character.toLowerCase(c)); } } String str = cleaned.toString(); String rev = cleaned.reverse().toString(); return str.equals(rev); } } 🕒 Time Complexity: O(n) 💡 Concepts Used: String Manipulation, Two-Pointer Logic ✅ A simple yet powerful problem that helps strengthen string handling fundamentals in Java. #Day39 #LeetCode #Java #CodingChallenge #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfLeetCode Problem: 125. Valid Palindrome 🧩 Goal: Check if a given string is a palindrome — ignoring spaces, punctuation, and case. 💻 Approach: 1️⃣ Remove all non-alphanumeric characters. 2️⃣ Convert to lowercase. 3️⃣ Compare the cleaned string with its reverse. class Solution { public boolean isPalindrome(String s) { StringBuilder cleaned = new StringBuilder(); for (char c : s.toCharArray()) { if (Character.isLetterOrDigit(c)) { cleaned.append(Character.toLowerCase(c)); } } String str = cleaned.toString(); String rev = cleaned.reverse().toString(); return str.equals(rev); } } 🕒 Time Complexity: O(n) 💡 Concepts Used: String Manipulation, Two-Pointer Logic ✅ A simple yet powerful problem that helps strengthen string handling fundamentals in Java. #Day39 #LeetCode #Java #CodingChallenge #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🔥 Day 97 of my 100 Days of Code Problem: Binary Tree Inorder Traversal (LeetCode #94) Problem Description: Given the root of a binary tree, return the inorder traversal of its nodes’ values. Inorder means visiting nodes in this order: Left -> Node -> Right Java Solution: class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode current = root; while (current != null || !stack.isEmpty()) { // Reach the leftmost node while (current != null) { stack.push(current); current = current.left; } // Process node current = stack.pop(); result.add(current.val); // Visit right subtree current = current.right; } return result; } } Time Complexity: O(n) Space Complexity: O(n) (for stack in the worst case) #Day97 #LeetCode #100DaysOfCode #Java #BinaryTree #DFS
To view or add a comment, sign in
-
-
Solved "Sort Colors" on LeetCode I remember solving this before as “Sort 0/1/2”. This time I tried from memory, then checked my old notes to fix a missing detail. Problem : Sort an array of 0s, 1s, and 2s in-place in a single pass & O(1) space. Approach — Dutch National Flag Algorithm We use 3 pointers: • low → for 0s which starts from 0th index. • mid → for current element • high → for 2s which starts from last index. Logic: If Element is 0 : • It belongs to the front → swap with `low` • We increase both `low` and `mid` Because we are sure the swapped element is 1 (already checked earlier) If Element is 2 : • It belongs to the end → swap with `high` • Only decrease `high` Because the swapped element might be 0 or 1, so we must recheck this index → don’t move mid • If Element is 1 : just move mid ahead This gradually pushes all 0s to the left and 2s to the right #LearnInPublic #LeetCode #Java #PlacementPrep #DSA Code (Java):
To view or add a comment, sign in
-
-
🔹 Day 31: Add Binary (LeetCode #67) 📌 Problem Statement: Given two binary strings a and b, return their sum as a binary string. ✅ My Approach: Simulated binary addition manually using a carry-based approach. Started from the end of both strings, added corresponding bits along with the carry, appended the remainder (sum % 2) to the result, and updated the carry (sum / 2). Finally, reversed the result string to get the correct binary sum. 📊 Complexity: Time Complexity: O(max(n, m)) Space Complexity: O(max(n, m)) ⚡ Submission Stats: Runtime: 1 ms (Beats 99.71%) Memory: 42.45 MB 💡 Reflection: This problem strengthened my understanding of bitwise addition and string manipulation — a perfect mix of logic and implementation precision! ⚙️ #LeetCode #Java #String #Binary #100DaysOfCode #Day31
To view or add a comment, sign in
-
-
🌟 Day 73 of 100 Days of Challenge 🌟 📌 LeetCode 220 — Contains Duplicate III Difficulty: 🟥 Hard | Language: Java ☕ Today’s problem was a tricky combination of index range and value range checks. The task was to determine if there exists a pair (i, j) such that: i ≠ j |i - j| ≤ indexDiff |nums[i] - nums[j]| ≤ valueDiff 🧠 Key Insight Instead of brute force (which is O(n²) ❌), we can use a sliding window + TreeSet approach: Maintain a window of size indexDiff. For each nums[i], find if there’s a number within [nums[i] - valueDiff, nums[i] + valueDiff]. TreeSet’s ceiling() function helps in efficient range searching. Slide the window forward by removing older elements. ⏱ Time Complexity: O(n log k) 💾 Space Complexity: O(k) where k = indexDiff ✨ Takeaway: This problem beautifully combines data structures (TreeSet) with sliding window, making it a great practice for hard-level problems. It sharpened my ability to handle both value-based and index-based constraints efficiently. #100DaysOfCode #Day71 #DSA #LeetCode #Java #ProblemSolving #TreeSet #SlidingWindow #CodingChallenge #HardProblem #LearningEveryday
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