Tag: java
-
Data Structures & Algorithms in Java – Intervals – Non overlapping intervals
Problem: Given an array of intervals , find the minimum number of overlapping intervals that need to be removed so that the array consists of only non overlapping intervals. For example, Consider the array of intervals: [[1,2],[2,3],[3,5],[1,3]] [1,2] and [1,3] are overlapping. Also [2,3] and [1,3] are overlapping. How do you know two intervals are…
-
Data Structures & Algorithms in Java – Intervals – Merge Intervals
Problem: Given an array of intervals , merge overlapping intervals and return the non overlapping array of intervals. For example, Given the array: [[1,3],[2,6],[8,10]] The output should be [[1,6],[8,10]] Try out the solution here: https://leetcode.com/problems/merge-intervals/ Solution: First sort the array , this makes it easier to merge overlapping intervals. Once sorted , compare two successive…
-
Data Structures & Algorithms in Java – Intervals- Insert new interval
Problem: Given an array of sorted intervals , insert a new interval at the correct position. If the new interval overlaps with the previous interval merge them into one interval and do the same for the subsequent intervals after the new interval. For example, Given the below array of intervals: [[1,2] ,[3,5],[6,7],[8,10],[12,16]] and the new…
-
Data Structures & Algorithms in Java – Strings – Encode and Decode Strings
Problem: Given a list of strings , encode them and then decode them back. Consider the use case where you want to send a list of strings over the network. You have the limitation of using only string data structure. And somehow you need to represent the list of strings as a string and then…
-
Data Structures & Algorithms in Java – Linked List – Merge k sorted lists.
Problem: Given k singly linked sorted lists merge them together into one sorted list. For example, given the below lists: [[1,4,5],[1,3,4],[2,6]] merge them into [1,1,2,3,4,4,5,6] Try out the solution here: https://leetcode.com/problems/merge-k-sorted-lists/ Solution: Hint: Let’s look at all of them . Brute force In this case , you copy all the node values into an array.…
-
Data Structures & Algorithms in Java – Linked List – Remove Nth Node from the last
Problem: Given a linked list , remove the nth node from the end of the list For example, For the linked list [1,2,3,4,5] and the number n = 3 Remove the 3rd element from the last which is 3. So the final list is [1,2,4,5] Try out the solution here: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ Solution: Hint: Explanation: Two…
-
Data Structures & Algorithms in Java – Linked List – Reorder List
Problem: Given a singly linked list , reorder it in such a way that the first and last elements are next to each other , the second and second from last are next to each other etc. For example, Given the list [1,2,3,4,5] Reorder it into: [1,5,2,4,3] As you see we are merging elements from…
-
Data Structures & Algorithms in Java – Linked List – Merge Two Sorted Lists
Problem : Given two sorted lists merge them without copying them to a new linked list. For example: Given the linked lists: [1,3,4] and [1,2,4]. merge them into [1,1,2,3,4] Try out the solution here: https://leetcode.com/problems/merge-two-sorted-lists/submissions/ Solution: Hint: Explanation: The problem has a recursive solution and an iterative solution. Let’s look at both of them. In…
-
Data Structures & Algorithms in Java – Linked Lists- Linked List Cycle
Problem: Given a linked list and the head node of the linked list find if there is a loop / cycle in the linked list, that is if the tail node points back to any of the previous nodes. For example: The below linked list has a cycle: Output is true for the above linked…
-
Data Structures & Algorithms in Java – Strings – Palindromic substrings
Problem: Given a string , find the number of palindromic substrings in the string. For example , for the input “babad” the number of palindromic substrings is 7: “b” , “a”, “b”,”a”,”d” , “aba” , “bab” Try out the solution here: https://leetcode.com/problems/palindromic-substrings/ Solution: The solution is almost the same as we discussed in the previous…