Day 11/100 Day Coding Challenge Today, I solved the LeetCode problem: Container With Most Water using Java. Here’s a detailed breakdown of my approach and learning: Problem Statement: Given an array of non-negative integers representing heights of vertical lines on a coordinate plane, find two lines that, along with the x-axis, form a container that holds the maximum water. Challenges: A brute-force approach would check all possible pairs of lines (O(n²)), which is inefficient for large arrays. I aimed for an optimized solution using the two-pointer technique. Approach (Two-Pointer Technique): Initialize two pointers: l at the start, r at the end of the array. Compute the current area: curWater = (r - l) * min(height[l], height[r]). Update maximum area found so far. Move the pointer pointing to the shorter line inward: If height[l] < height[r], increment l. Else, decrement r. Repeat until the pointers meet. #100DaysOfCode #Java #LeetCode #TwoPointerTechnique #ProblemSolving #Algorithms #SoftwareEngineering #Day11
Java Solution: Container With Most Water LeetCode Challenge
More Relevant Posts
-
100 Days of Coding Challenge – Day 27 📌 Problem: Maximum Average Subarray I 💻 Language: Java 🧠 Concept Used: Sliding Window Technique 🔍 Platform: LeetCode Today’s challenge was to find a contiguous subarray of length k that has the maximum average value. Example: Input: [1,12,-5,-6,50,3], k = 4 Output: 12.75 Approach: ✔ Calculate the sum of the first window of size k ✔ Slide the window by removing the left element and adding the next element ✔ Keep track of the maximum sum ✔ Divide by k to get the maximum average Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/ghU4zrpt 🔗 Code: https://lnkd.in/g7qXeHx6 #100DaysOfCode #Day27 #Java #DSA #LeetCode #SlidingWindow #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 13 of #LeetCode75 Complete! 75 Days of Coding Topic: Sliding Window Problem: Maximum Average Subarray I Today, I attempted the Maximum Average Subarray I problem on LeetCode using the Java programming language. The problem required me to find the maximum average value among all possible contiguous subarrays of size 𝑘. 🔍 Approach: I did not use the sliding window technique to find the solution to the problem. Instead, I implemented the optimized solution using the prefix sum technique: I created the prefix sum array to store the cumulative sum. I computed the sum of the subarray using the formula: prefix[i] - prefix[i - 𝑘] and divided the maximum sum by 𝑘. 📚 What I learned: I learned how to use the prefix sum array to store the cumulative sum. I learned how to avoid redundant calculations. I learned the different techniques that could be implemented to solve the sliding window problems. 📌 Key Insight: The prefix sum array is helpful in avoiding redundant calculations. #LeetCode75 #LeetCode #Java #DS #SlidingWindow #ProblemSolving
To view or add a comment, sign in
-
-
LeetCode Problem || Find All Possible Stable Binary Arrays II (3130)🚀 🔹 Problem Idea: We are given a number of 0s and 1s and a constraint called limit. The goal is to count the number of possible binary arrays such that no more than limit consecutive identical elements appear. 🔹 Approach: I used Dynamic Programming to track the number of valid arrays formed with: i zeros j ones and the last placed element (0 or 1). The DP state helps ensure that the consecutive limit condition is maintained while building the array. Always enjoying the process of learning and improving problem-solving skills! 💡 #LeetCode #DynamicProgramming #ProblemSolving #Java #CodingPractice #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 93 - LeetCode Journey Solved LeetCode 9: Palindrome Number in Java ✅ At first glance, it feels like a string problem… but the real challenge is solving it without converting to string. Instead of reversing the whole number, I reversed only half of it and compared both parts. This avoids overflow and keeps it efficient. Smart approach > brute force 💡 Key takeaways: • Handling edge cases (negative numbers, trailing zeroes) • Reversing only half of the number • Avoiding extra space (no string conversion) • Writing optimized mathematical logic ✅ All test cases passed ⚡ O(log n) time and O(1) space Sometimes the best solutions are the simplest ones, just a different way of thinking 🔥 #LeetCode #DSA #Java #Math #ProblemSolving #CodingJourney #InterviewPrep #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 23 📌 Problem: Longest Consecutive Sequence 💻 Language: Java 🧠 Concept Used: Sorting + Sequence Counting 🔍 Platform: LeetCode Today’s challenge was to find the length of the longest sequence of consecutive numbers in an unsorted array. The numbers do not need to be adjacent in the original array, but they must form a continuous sequence like 1,2,3,4. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 → Longest sequence: [1,2,3,4] Approach: ✔ First sort the array ✔ Traverse the array and check if the next number is consecutive ✔ Skip duplicates ✔ Track the longest streak using a counter Time Complexity: O(n log n) (due to sorting) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gguN6KnH 🔗 Code: https://lnkd.in/gyVFpsbg #100DaysOfCode #Day23 #Java #DSA #LeetCode #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Solved LeetCode 1009 – Complement of Base 10 Integer today. In simple terms, the problem asks us to flip every bit of a number’s binary representation — change all 0s to 1s and all 1s to 0s — and then convert it back to a decimal number. The key idea was to create a bit mask of all 1s with the same length as the number’s binary form, and then use the XOR (^) operation to flip the bits. A small problem, but a good reminder of how powerful bit manipulation can be in programming. #LeetCode #DSA #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day X of #LeetCode75 Complete! 75 Days of Coding Topic: Sliding Window Problem: Maximum Number of Vowels in a Substring of Given Length I solved the Maximum Number of Vowels in a Substring problem on the LeetCode platform using Java. I had to find the maximum number of vowels present in any substring of size k. 🔍 Approach: I applied the sliding window concept to the problem. Count the number of vowels in the initial window size 𝑘. Slide the window one character at a time. Increment the window size by including the next character. Decrement the window size by removing the leftmost character. Maintain the maximum count during the process. 📚 What I learned: Efficient usage of the sliding window concept for fixed window size. Avoiding redundant computation using the sliding window. Efficient usage of character checking using the vowel set. 📌 Key Insight: Normally, checking all substrings individually would take O(n*m) time. But using the sliding window concept, we can achieve O(n) time complexity. #LeetCode75 #CodingChallenge #Java #DSA #SlidingWindow #ProblemSolving
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 26 📌 Problem: Remove Duplicates from Sorted Array II 💻 Language: Java 🧠 Concept Used: Two Pointer Technique + Counting 🔍 Platform: LeetCode Today’s challenge was to remove duplicates from a sorted array in-place, but this time allowing at most two occurrences of each element. Approach: ✔ Traverse the array and track occurrences using a counter ✔ Allow elements to appear at most twice ✔ Use a pointer k to place valid elements in the correct position ✔ Skip elements when they exceed the allowed count Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gm2_CHbz 🔗 Code: https://lnkd.in/gKxuTbsG #100DaysOfCode #Day26 #Java #DSA #LeetCode #TwoPointers #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 19 of my Java DSA Journey Today I solved Decode Ways — LeetCode Problem #91. 🔹 Problem A message containing digits can be decoded using the mapping: 1 → A 2 → B ... 26 → Z The task is to determine how many possible ways the string can be decoded. 🔹 Approach Used: Dynamic Programming Idea • At each position we check two possibilities • Decode a single digit (1–9) • Decode two digits together (10–26) By storing previous results, we avoid recomputing overlapping subproblems. 📊 Complexity • Time Complexity: O(n) • Space Complexity: O(n) 💡 Key Learning This problem introduces Dynamic Programming, where we store intermediate results to efficiently compute the final answer. 🔗 GitHub Solution https://lnkd.in/gW8atfqw Each day of practice is helping me improve my algorithmic thinking and problem-solving skills. #Java #DSA #LeetCode #DynamicProgramming #CodingJourney
To view or add a comment, sign in
-
-
Day 55 of #100DaysOfLeetCode 💻✅ Solved #434. Number of Segments in a String problem in Java. Approach: • Initialized a counter to track number of words (segments) • Traversed the string character by character • Checked for non-space characters • If the current character is not a space and either it is the first character or the previous character is a space, incremented the count • This ensures counting only the starting of each word Performance: ✓ Runtime: 0 ms (Beats 100% submissions) 🚀 ✓ Memory: 41.83 MB (Beats 99.81% submissions) Key Learning: ✓ Practiced string traversal without using extra space ✓ Learned how to identify word boundaries efficiently ✓ Improved logic building for handling spaces and edge cases Learning one problem every single day 🚀 #Java #LeetCode #DSA #Strings #ProblemSolving #CodingJourney #100DaysOfCode
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