🚀 Day 5 – DSA Daily Series Today’s Problem: Container With Most Water (LeetCode 11) Today I worked on this interesting problem and really enjoyed the process of figuring out the optimal approach. 🧠 Problem Given an array height where each element represents the height of a vertical line, we need to find two lines that together with the x-axis can form a container that holds the maximum amount of water. The amount of water the container can hold depends on: • The shorter height between the two lines • The distance between them Formula used: Area = min(height[i], height[j]) × (j - i) 💡 Approach At first glance, it may look like we need to check all pairs of lines, which would lead to a brute-force O(n²) solution. Instead, I used the Two Pointer technique: • Start with one pointer at the beginning and another at the end of the array • Calculate the area between them • Keep track of the maximum area found • Move the pointer pointing to the smaller height, since moving the larger one won’t increase the area This makes the solution much more efficient. ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) 🔎 Key Learning Problems like this are interesting because they show how the right observation can reduce a brute-force solution into a much more efficient one using patterns like Two Pointers. Continuing the DSA Daily Series — one problem at a time. 🚀 #DSA #LeetCode #Python #Algorithms #ProblemSolving #CodingJourney
Container With Most Water Problem Solution
More Relevant Posts
-
🚀 Day 9 – DSA Daily Series Today’s Problem: Merge Sorted Array (LeetCode 88) Today I solved an interesting problem that involves merging two sorted arrays efficiently. 🧠 Problem You are given two sorted arrays nums1 and nums2. nums1 has enough extra space at the end to hold elements from nums2. The goal is to merge both arrays into a single sorted array inside nums1. Example: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] 💡 Approach Instead of creating a new array, I solved this using the two-pointer technique from the end. Steps followed: • Start pointers from the end of both arrays • Compare the elements from nums1 and nums2 • Place the larger element at the end of nums1 • Move the pointers accordingly until all elements are merged This approach avoids unnecessary shifting of elements. ⏱ Complexity Time Complexity: O(m + n) Space Complexity: O(1) 🔎 Key Learning Working from the end of the array is a smart trick that helps avoid overwriting elements while merging. Continuing the DSA Daily Series — improving problem-solving skills one problem at a time. 🚀 #DSA #LeetCode #Python #Algorithms #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 11 – DSA Daily Series Today’s Problem: Find Minimum in Rotated Sorted Array (LeetCode 153) Today I solved an interesting problem that involves finding the minimum element in a rotated sorted array. 🧠 Problem You are given a sorted array that has been rotated several times. The task is to find the minimum element in the array. Example: Input: nums = [3,4,5,1,2] Output: 1 💡 Approach I solved this problem using Binary Search. Key idea: • In a rotated sorted array, the minimum element lies at the rotation point • Compare the middle element with the rightmost element • If nums[mid] > nums[high], the minimum lies in the right half • Otherwise, it lies in the left half including mid By narrowing the search space, we efficiently locate the minimum element. ⏱ Complexity Time Complexity: O(log n) Space Complexity: O(1) 🔎 Key Learning Binary Search can be extended beyond simple searching and used to identify structural properties of arrays like rotation points. Continuing the DSA Daily Series — solving and learning one problem at a time. 🚀 #DSA #LeetCode #Python #Algorithms #BinarySearch #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 10 – DSA Daily Series Today’s Problem: Search in Rotated Sorted Array (LeetCode 33) Today I solved an interesting problem where a sorted array is rotated, and we still need to find the target efficiently. 🧠 Problem You are given a sorted array that has been rotated at an unknown pivot. The task is to find the index of the target element. If the target is not present, return -1. Example: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 💡 Approach This problem is solved using a modified Binary Search. Key idea: • Even though the array is rotated, one half will always remain sorted • Check which half is sorted (left or right) • Determine whether the target lies in that sorted half • Adjust the search range accordingly By doing this, we can still achieve logarithmic time complexity. ⏱ Complexity Time Complexity: O(log n) Space Complexity: O(1) 🔎 Key Learning This problem helped me understand how Binary Search can be adapted for slightly complex scenarios like rotated arrays. Continuing the DSA Daily Series — solving and learning one problem at a time. 🚀 #DSA #LeetCode #Python #Algorithms #BinarySearch #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 74 of 100 Days LeetCode Challenge Problem: 🔹 #653 – Two Sum IV: Input is a BST 🔗 https://lnkd.in/g9vDFKMA Learning Journey: 🔹 Today’s problem required checking whether there exist two nodes in a Binary Search Tree whose values sum to k. 🔹 I performed a Depth-First Search (DFS) traversal using a stack to visit all nodes of the tree. 🔹 While traversing, I stored each node’s value in a list. 🔹 Then I used a hash set to track visited values and checked whether the complement (k - value) already exists. 🔹 If the complement is found, it means two numbers sum to k, so the function returns True. Concepts Used: 🔹 Depth-First Search (DFS) 🔹 Stack-based Tree Traversal 🔹 Hash Set for Fast Lookup 🔹 Two Sum Pattern Key Insight: 🔹 The classic Two Sum approach works well after collecting values from the BST. 🔹 Using a set allows O(1) lookup, making it efficient to check complements during iteration. Complexity: 🔹 Time: O(n) 🔹 Space: O(n) #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #SoftwareEngineering #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
-
✅ Day 78 of 100 Days LeetCode Challenge Problem: 🔹 #3110 – Score of a String 🔗 https://lnkd.in/g9jgxcdK Learning Journey: 🔹 Today’s problem involved calculating the score of a string based on the absolute differences between ASCII values of adjacent characters. 🔹 I iterated through the string starting from index 1 and computed the difference between the current and previous character using ord(). 🔹 For each pair, I added the absolute difference to a running total. 🔹 This approach efficiently accumulates the score in a single pass. Concepts Used: 🔹 String Traversal 🔹 ASCII Value Conversion (ord()) 🔹 Absolute Difference Calculation Key Insight: 🔹 The problem reduces to comparing adjacent elements, making it a straightforward linear scan. 🔹 Using ord() allows direct access to ASCII values, simplifying the computation. Complexity: 🔹 Time: O(n) 🔹 Space: O(1) #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #SoftwareEngineering #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
-
🚀 Day 14 – DSA Daily Series Today’s Problem: Find Peak Element (LeetCode 162) Today I worked on an problem that focuses on identifying a peak element in an array. 🧠 Problem Given an array nums, return the index of any peak element. A peak element is an element that is greater than its neighbours. Example: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is greater than both its neighbours, so it is a peak element. 💡 Approach I solved this problem using Binary Search. Key idea: • Check the middle element mid • If nums[mid] > nums[mid + 1], a peak exists on the left side (including mid) • Otherwise, a peak exists on the right side • Continue narrowing the search space until the peak element is found This works because there will always be at least one peak in the array. ⏱ Complexity Time Complexity: O(log n) Space Complexity: O(1) 🔎 Key Learning Binary Search can be applied not only to sorted arrays but also to problems where we need to identify patterns or properties in the array. Continuing the DSA Daily Series — solving and learning one problem at a time. 🚀 #DSA #LeetCode #Python #Algorithms #BinarySearch #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 8 – DSA Daily Series Today’s Problem: Search Insert Position (LeetCode 35) Today I solved another interesting problem that uses the concept of Binary Search on a sorted array. 🧠 Problem Given a sorted array of distinct integers and a target value, return the index if the target is found. If the target is not found, return the index where it should be inserted to maintain the sorted order. Example: Input: nums = [1,3,5,6], target = 5 Output: 2 Example: Input: nums = [1,3,5,6], target = 2 Output: 1 💡 Approach I solved this using Binary Search. Steps followed: • Initialize two pointers low and high • Calculate the middle index mid • Compare nums[mid] with the target • If target is greater → move to the right half • If target is smaller → move to the left half • If not found, the low pointer gives the correct insert position ⏱ Complexity Time Complexity: O(log n) Space Complexity: O(1) 🔎 Key Learning Binary Search is not just for searching — it can also help determine the correct position to insert elements efficiently in a sorted array. Continuing the DSA Daily Series — improving problem-solving skills one problem at a time. 🚀 #DSA #LeetCode #Python #Algorithms #BinarySearch #CodingJourney
To view or add a comment, sign in
-
-
✅ Day 76 of 100 Days LeetCode Challenge Problem: 🔹 #75 – Sort Colors 🔗 https://lnkd.in/gpH9fEJu Learning Journey: 🔹 Today’s problem required sorting an array containing only 0s, 1s, and 2s (representing colors) in-place without using built-in sort. 🔹 I implemented a selection sort approach, where for each index, I searched for the minimum element in the remaining array. 🔹 Once found, I swapped it with the current position to gradually build the sorted array. 🔹 This ensured the array was sorted in ascending order (0 → 1 → 2). Concepts Used: 🔹 Selection Sort 🔹 In-place Swapping 🔹 Nested Iteration Key Insight: 🔹 Even though the problem has an optimal linear-time solution (Dutch National Flag algorithm), a simple sorting approach like selection sort still works correctly. 🔹 Iteratively placing the smallest remaining element ensures correctness, though not optimal efficiency. Complexity: 🔹 Time: O(n²) 🔹 Space: O(1) #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #SoftwareEngineering #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
-
🚀 Day 16 – DSA Daily Series Today’s Problem: Two Sum II – Input Array Is Sorted (LeetCode 167) Today’s problem was simple yet really interesting — especially because of how efficiently it can be solved using the two-pointer technique. 🧠 Problem Given a sorted array, find two numbers such that they add up to a target value and return their indices (1-based). Example: Input: [2,7,11,15], target = 9 Output: [1,2] 💡 Approach Instead of using extra space like a hashmap, I used: • Two pointers — one at the start and one at the end • If sum is too large → move right pointer left • If sum is too small → move left pointer right • Stop when target is found Clean and efficient ⚡ ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) 🔎 Key Learning When the array is already sorted, always think of two pointers before anything else — it saves both time and space. Solved it today and it felt really smooth to implement! 💯 Continuing to stay consistent and improve step by step 🚀 #DSA #LeetCode #Python #TwoPointers #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 35 of #60DaysCodingChallenge | ABTalks Today’s problem focused on the Sliding Window Technique (Variable Window) while solving the classic Longest Substring Without Repeating Characters problem. 🔍 Key Learning: Instead of checking all possible substrings (which would be inefficient), the Sliding Window approach allows us to dynamically adjust the window size while traversing the string. This reduces the time complexity significantly and helps build a more optimized solution. 💡 What I Practiced Today • Understanding the concept of Variable Sliding Window • Efficient string processing techniques • Optimizing brute force solutions into linear time algorithms ⚙ Approach Used Two pointers (left and right) maintain the window A data structure keeps track of characters inside the window If a duplicate appears, the window shrinks from the left Track the maximum window size without repeating characters 📈 Complexity Time Complexity: O(n) Space Complexity: O(n) Consistency is slowly building stronger problem-solving skills every day. The goal is not just solving problems but understanding the patterns behind them. Looking forward to Day 36 tomorrow. 🔥 ABTalksOnAI #ABTalks #60DaysChallenge #DSA #SlidingWindow #ProblemSolving #CodingJourney #Python #LearningInPublic
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