✅ Day 62/75 – Non-overlapping Intervals 📌 Problem Given a list of intervals, remove the minimum number of intervals so that the remaining intervals are non-overlapping. 👉 Intervals that only touch at a point (e.g., [1,2] and [2,3]) are considered non-overlapping. 💡 Approach (Greedy) 🔺 Sort intervals by their ending time 🔺 Always keep the interval that ends earliest 🔺 If the next interval overlaps with the previous one, remove it 📘 Example 🔺Input: [[1,2], [2,3], [3,4], [1,3]] 🔺Output: 1 ⏱ Complexity 🔺Time: O(n log n) 🔺Space: O(1) #Day62 #75DaysOfCode #GreedyAlgorithm #Java #DSA #ProblemSolving #LeetCode #CodingJourney
Remove Overlapping Intervals with Greedy Algorithm
More Relevant Posts
-
Day 4 of #60daysofLeetcode, #7. Reverse Integer - https://lnkd.in/dqA_ciDP is the question. Solution. To reverse the integer, we repeatedly extract the last digit using modulo 10, then build the result from left to right. Each iteration: extract the last digit, update result = result * 10 + digit, and divide input by 10. We continue until the input becomes zero. Before each result update, we check if the operation would cause overflow, and if so, return 0. Time Complexity is O(log n) or O(d) where d is the number of digits. Space complexity is O(1). #LeetCode, #Java, #DSA, #LearningInPublic
To view or add a comment, sign in
-
-
Hello Everyone, Day 26 / #100DaysOfCode LeetCode 1200 — Minimum Absolute Difference (Easy) Problem (short): Given an array of distinct integers, find all pairs [a, b] such that |a - b| is minimum among all possible pairs. Key Insight: After sorting, the minimum absolute difference can only occur between adjacent elements. Why? - Any non-adjacent pair will have a larger gap due to sorting. - So instead of checking all O(n²) pairs, we only scan once. Approach: 1. Sort the array 2. Traverse from left to right 3. Track the smallest difference between arr[i] and arr[i-1] 4. Reset the result list when a smaller diff is found 5. Append pairs when the diff matches the current minimum Complexity: - Time: O(n log n) (sorting) - Space: O(1) extra (excluding output) #Leetcode #DSA #Java
To view or add a comment, sign in
-
-
🚀 Day 15 — LeetCode Progress (Java) 🔹 Find Lucky Integer in an Array 🕒 Required: Find the largest integer in the array whose value is exactly equal to its frequency. If no such number exists, return -1. 💡 Idea: The key observation is that we need both the value and how many times it appears. So the problem naturally reduces to frequency counting. Once frequencies are known, we can compare each number with its count and track the maximum valid one. ⚙️ Approach: Iterate through the array and store frequencies using a HashMap. After building the frequency map, traverse through the entries and check where key == value. Among all valid candidates, keep updating the maximum lucky number. This ensures correctness while keeping the logic clean and readable. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) #LeetCode #DSA #Java #CodingJourney
To view or add a comment, sign in
-
-
Solved LeetCode 26: Remove Duplicates from Sorted Array This problem focuses on modifying a sorted array in-place without using extra memory. I used the two-pointer approach, where one pointer tracks unique elements and the other scans the array. Whenever a new value is found, it is placed at the next valid position. This ensures that only unique elements remain in the first part of the array. The solution works in O(n) time complexity and O(1) space complexity, making it optimal. #DSA #Java
To view or add a comment, sign in
-
-
📝 Day 17/30 – LeetCode #238 (Product of Array Except Self) | Java The straightforward solution using division fails due to edge cases involving zeros and is explicitly disallowed. A brute-force approach would also be inefficient with O(n²) time complexity. By computing prefix products and suffix products, I was able to calculate the result for each index without using division. Each element in the output array represents the product of all numbers before and after it, resulting in an O(n) time solution with constant extra space. This problem highlighted how breaking a problem into directional passes can simplify complex constraints. #LeetCode #Java #DSA #Arrays #PrefixSum #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
📝 Day 11/30 – LeetCode #26 (Remove Duplicates from Sorted Array) | Java A brute-force approach using extra space would work, but the constraint here is to modify the array in-place. Since the array is already sorted, duplicates always appear next to each other. Using a two-pointer approach, one pointer tracks the position of the last unique element while the other scans through the array. Whenever a new value is found, it is placed at the next valid position. This results in an O(n) solution with O(1) extra space. This problem reinforced how understanding input constraints can significantly simplify the solution. #LeetCode #Java #DSA #TwoPointers #Arrays #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
Day 6/30 – LeetCode #242 (Valid Anagram) | Java My initial thought was to sort both strings and compare them, but that adds unnecessary overhead. The more efficient approach was to use a frequency count, since only character occurrences matter here. By using an int[26] array and incrementing/decrementing counts while iterating through both strings, I was able to solve this in O(n) time with O(1) space. This problem reinforced how fixed-size arrays in Java can outperform maps when the character set is known. #LeetCode #Java #DSA #Strings #Hashing #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 13 — LeetCode Progress (Java) 🔹 Majority Element 🕒 Required: Identify the element that appears more than ⌊n/2⌋ times in the array while maintaining efficient time complexity and minimal extra processing. 💡 Idea: Since one element dominates the frequency, tracking occurrences during traversal helps quickly determine which value exceeds the threshold without needing multiple passes . ⚙️ Approach: Implemented a HashMap to store element frequencies while iterating through the array. Updated counts dynamically and leveraged the frequency data to determine the majority element based on occurrence dominance ✅. #LeetCode #DSA #Java #CodingJourney
To view or add a comment, sign in
-
-
DSA Problem Solved: Check if Two Strings are Anagrams :- Problem Statement :- Two strings are called anagrams if they contain the same characters with the same frequency, but possibly in a different order. Example: "listen" and "silent" → Anagrams "hello" and "world" → Not Anagrams Approach Used :- First, I checked whether the lengths of both strings are equal. If lengths are different, they cannot be anagrams. Then, I converted both strings into character arrays using toCharArray(). After that, I sorted both character arrays using Arrays.sort(). Finally, I compared each character of both sorted arrays: If all characters match → Strings are anagrams If any character differs → Not anagrams Sorting ensures that characters are arranged in the same order, making comparison easy and efficient. #DSA #JAVA
To view or add a comment, sign in
-
-
🚀 Day 3 / 100 | Sliding Window Maximum -> Problem Solved: 239 – Sliding Window Maximum 💡Intuition : The goal is to find the maximum element in every window of size k. A brute-force approach would recheck each window using nested loops, which results in O(n × k) time complexity. To solve the problem in a linear time complexity O(n), we use a deque, which is a double-ended queue. My initial intuition is a brute-force approach, but how deque optimises this problem is a key takeaway today. I’m not very familiar with Deques in Java yet, so I intentionally solved this problem in C++ 🔑Key Insights: ->A Deque maintains only useful elements, eliminating unnecessary comparisons. ->Storing indices instead of values helps manage window boundaries efficiently. #100DaysOfCode #DSA #Java #ProblemSolving
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