Solved! Leetcode 23. Merge k Sorted Lists

Table of ContentsMerge k Sorted ListsJavaPythonTime ComplexitySpace Complexity source: https://leetcode.com/problems/merge-k-sorted-lists/ Merge k Sorted Lists You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Example 1:Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,4,4,5,6]Explanation: The linked-lists are:[1->4->5,1->3->4,2->6]merging them into one sorted list:1->1->2->3->4->4->5->6 ...

Solved! Leetcode 352: Data Stream as Disjoint Intervals

source: https://leetcode.com/problems/data-stream-as-disjoint-intervals/description/ Data Stream as Disjoint Intervals Given a data stream input of non-negative integers a1, a2, …, an, summarize the numbers seen so far as a list of disjoint intervals. Implement the SummaryRanges class: SummaryRanges() Initializes the object with an empty stream. void addNum(int value) Adds the integer value to the stream. int[][] getIntervals() ...

Solved! Leetcode 1162: As Far from Land as Possible

source: https://leetcode.com/problems/as-far-from-land-as-possible/description/ As Far from Land as Possible Given an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. If no land or water exists in the ...

Solved! Leetcode 567: Permutation in String

source: https://leetcode.com/problems/permutation-in-string/ Permutation in String Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1’s permutations is the substring of s2. Example 1: Input: s1 = “ab”, s2 = “eidbaooo” Output: true Explanation: s2 contains one permutation of ...

Solved! Leetcode 646. Maximum Length of Pair Chain

source: https://leetcode.com/problems/maximum-length-of-pair-chain/description/ Maximum Length of Pair Chain You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti. A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion. Return the length ...

Solved! Leetcode 1055. Shortest Way to Form String

source: https://leetcode.com/problems/shortest-way-to-form-string/description/ Shortest Way to Form String A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., “ace” is a subsequence of “abcde” while “aec” is not). Given two strings ...

Solved! Leetcode 1345. Jump Game IV

source: https://leetcode.com/problems/jump-game-iv/description/ Jump Game IV Given an array of integers arr, you are initially positioned at the first index of the array. In one step you can jump from index i to index: i + 1 where: i + 1 < arr.length. i – 1 where: i – 1 >= 0. j where: arr[i] == ...

Solved! Leetcode 742. Nearest Leaf in a Binary Tree

source: https://leetcode.com/problems/closest-leaf-in-a-binary-tree/description/ Given the root of a binary tree where every node has a unique value and a target integer k, return the value of the nearest leaf node to the target k in the tree. Nearest to a leaf means the least number of edges traveled on the binary tree to reach any leaf ...

Map.computeIfAbsent: Write a clear and concise code

Table of ContentsHow?Method SignatureBehaviourTest Your KnowledgeReferences How? Suppose you are solving a graph problem where you have been given a 2D array of edges and you need to find the shortest path between a source and a destination node. At this point, you could create a graph from the given edges. ... public List<Integer> findShortestPath(int[][] ...

Solved! Leetcode 1586. Binary Search Tree Iterator II

source: https://leetcode.com/problems/binary-search-tree-iterator-ii/description/ Binary Search Tree Iterator II Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST): BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number ...