🚀 Day 36 / 100 | Insert Interval -Intuition: -This problem is based on interval merging with insertion. The Simple idea is to insert the new interval in the correct position and merge if overlapping occurs. If the current interval ends before the new interval starts, there is no overlap, so add it directly. If the current interval starts before or equal to the new interval end, they overlap. "So instead of adding separately, update the new interval by taking the minimum start and maximum end". Once merging is done, add the merged interval and then add the remaining intervals. -Approach: O(n) Traverse all intervals from left to right. First, add all intervals that come before the new interval (no overlap). Then merge all overlapping intervals by updating: new.start = min(new.start, current.start) new.end = max(new.end, current.end) Add the merged interval to result. Finally, add all remaining intervals that come after. -Complexity: Time Complexity: O(n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode
Day 36: Merging Intervals with Insertion in Java
More Relevant Posts
-
🚀 Day 42 / 100 | Remove Duplicates from Sorted List II -Intuition: -The linked list is sorted, so duplicate values appear next to each other. -The goal is to remove all nodes that contain duplicate numbers and keep only distinct values. -To handle cases where duplicates appear at the beginning of the list, use a temp node before the head. -When a duplicate sequence is detected, we skip all nodes with that value. -Approach: O(n) -Create a temp node pointing to the head of the linked list. -Use two pointers: prev (last confirmed unique node) and curr (current node). -Traverse the linked list while checking if the current node has duplicates. -If duplicates are found, skip all nodes with that value. -Update prev.next to the next non-duplicate node. -If no duplicate is found, move both pointers forward. -Repeat the process until the end of the list. -Complexity: Time Complexity: O(n) Space Complexity: O(1) #100DaysOfCode #Java #DSA #LeetCode #linkedlist
To view or add a comment, sign in
-
-
Day 41 - Find Pivot Index Solved using a prefix sum approach. First computed the total sum of the array, then traversed once while maintaining a running left sum. For each index, the right sum is calculated using total - left - nums[i]. If both sums match, that index is the pivot. Time Complexity: O(n) #Day41 #LeetCode #Java #PrefixSum #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 18/30 – Remove Duplicates from Sorted List 🚀 Problem: Remove duplicate nodes from a sorted linked list. Key Insight: Since the list is sorted, duplicates will be adjacent. Approach: • Traverse using one pointer • If current.val == current.next.val → skip node • Else → move forward Time Complexity: O(n) Space Complexity: O(1) Simple logic. Clean pointer handling. #30DaysOfCode #Java #DSA #LinkedList #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 40 / 100 | Contains Duplicate II -Intuition: -The goal is to check whether there exist two equal elements within a distance k. -Instead of checking every pair, we only check for duplicates inside a window of size k. -If a number repeats within this window, the condition |i - j| ≤ k is satisfied, then return true. -Approach: O(n) -Use a HashSet to maintain a sliding window of size k. -Traverse through the array. -If the current element already exists in the set, return true. -Add the current element to the set. -If the window size exceeds k, remove the element that goes out of range (nums[i - k]). -If traversal completes without finding duplicates, return false. -Complexity: Time Complexity: O(n) Space Complexity: O(k) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow
To view or add a comment, sign in
-
-
Day 38 Explored some core concepts of the Spring Framework through hands-on practice. 🔹 Bean Configuration – Understanding how Spring manages and creates objects using the IoC container. 🔹 @Component – Learned how Spring automatically detects and registers classes as beans. 🔹 @Autowired – Explored dependency injection and how Spring automatically injects required dependencies. It was interesting to see how these concepts reduce manual object creation and make applications more loosely coupled. Step by step diving deeper into the Spring ecosystem. 🚀 #SpringFramework #Java #BackendDevelopment #DependencyInjection #LearningInPublic
To view or add a comment, sign in
-
Solved "Container With Most Water" on LeetCode today using the Two-Pointer Technique. 🚀 The key insight is that the area formed by two lines depends on the shorter height and the distance between them. Starting with pointers at both ends of the array, we compute the area and move the pointer at the smaller height inward to potentially find a taller boundary and maximize the area. This approach efficiently reduces the problem from O(n²) brute force to O(n) time with O(1) space. Problems like this are a great reminder that the right observation can drastically optimize a solution. 💡 #LeetCode #DSA #TwoPointers #Java #ProblemSolving
To view or add a comment, sign in
-
-
LeetCode 83 — Remove Duplicates from Sorted List This problem gives the head of a sorted linked list and asks to remove all duplicate values so that each element appears only once. Since the list is already sorted, duplicate values always appear next to each other. The task is simply to skip repeated nodes while keeping the original order of the list. Example : Input : 1 → 1 → 2 → 3 → 3 Output : 1 → 2 → 3 Approach used — Single Pass Pointer Traversal Because the list is sorted, the solution can be done in one traversal. Two pointers are used during traversal : - One pointer (a) keeps track of the last unique node. - Another pointer (b) scans the list forward. When both nodes have the same value, the duplicate node is skipped. When a new value appears, the unique node is connected to it. At the end, the last node’s next pointer is set to null to ensure the list ends correctly. This approach works in O(n) time and O(1) extra space. #leetcode #datastructures #linkedlist #java #problemSolving
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfCode Solved 80. Remove Duplicates from Sorted Array II on LeetCode 🔢 🧠 Key Insight: The array is already sorted, and we need to ensure that each element appears at most twice, modifying the array in-place. ⚙️ Approach: 🔹Maintain a pointer i representing the position to place the next valid element 🔹Start iterating from index 2 🔹For each element nums[j], compare it with nums[i] 🔹If they are different, place the element at nums[i + 2] and move the pointer forward This ensures that no element appears more than twice while maintaining the sorted order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Arrays #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
#day329 of #1001daysofcode problem statement (1784): Check if Binary String Has at Most One Segment of Ones Approach 1: If the pattern "01" appears in the string, it means the sequence of '1's was broken Approach 2: Traverse the string and count segments of consecutive '1's. Whenever a '1' is found, skip the entire sequence of continuous '1's and increase the segment count. If the number of segments of '1's is exactly one, the condition is satisfied. --both approaches have same time and space complexity. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 85/100: Construct Tree from Inorder & Postorder Day 85. MEDIUM. Last element of postorder = root. Split inorder. Recurse. Build tree backwards. ✅ 📌 Problem: Build binary tree from inorder and postorder traversals. 💡 Solution: Postorder's last = root Find root in inorder → splits left/right Recurse on both sides with correct indices 🎯 The Logic: Postorder: Left → Right → Root (last is always root!) Inorder: Left → Root → Right (root splits sides) 📊 Complexity: O(n²) time (can optimize with HashMap) Day 85. Tree construction. 🌳 #100DaysOfCode #DSA #LeetCode #Day85 #Java #TreeConstruction
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