104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree Given the root of a binary tree, return its maximum depth. A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 1 2 3 4 Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 1 2 3 4 Example 2: Input: root = [1,null,2] Output: 2 1 2 3 4 Example 3: Input: root = [] Output: 0 1 2 3 4 Example 4: Input: root = [0] Output: 1 Constraints:...

<span title='2021-03-11 00:00:00 +0000 UTC'>March 11, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

112. Path Sum

112. Path Sum Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children. 1 2 3 4 Example 1: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true 1 2 3 4 Example 2: Input: root = [1,2,3], targetSum = 5 Output: false 1 2 3 4 Example 3: Input: root = [1,2], targetSum = 0 Output: false Constraints:...

<span title='2021-03-11 00:00:00 +0000 UTC'>March 11, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

113. Path Sum II

113. Path Sum II Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where each path’s sum equals targetSum. A leaf is a node with no children. 1 2 3 4 Example 1: Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 Output: [[5,4,11,2],[5,8,4,5]] 1 2 3 4 Example 2: Input: root = [1,2,3], targetSum = 5 Output: [] 1 2 3 4 Example 3: Input: root = [1,2], targetSum = 0 Output: [] Constraints:...

<span title='2021-03-11 00:00:00 +0000 UTC'>March 11, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

451. Sort Characters By Frequency

![https://leetcode.com/problems/sort-characters-by-frequency/] Given a string, sort it in decreasing order based on the frequency of characters. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Example 1: Input: "tree" Output: "eert" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'....

<span title='2021-03-07 00:00:00 +0000 UTC'>March 7, 2021</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

767. Reorganize String

Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same. If possible, output any possible result. If not possible, return the empty string. 1 2 3 4 5 6 7 8 9 Example 1: Input: S = "aab" Output: "aba" Example 2: Input: S = "aaab" Output: "" Note: S will consist of lowercase letters and have length in range [1, 500]....

<span title='2021-03-07 00:00:00 +0000 UTC'>March 7, 2021</span>&nbsp;·&nbsp;4 min&nbsp;·&nbsp;volyx

997. Find the Town Judge

![https://leetcode.com/problems/find-the-town-judge/] In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. There is exactly one person that satisfies properties 1 and 2. You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b....

<span title='2021-03-06 00:00:00 +0000 UTC'>March 6, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

1046. Last Stone Weight

![https://leetcode.com/problems/last-stone-weight/] We have a collection of stones, each stone has a positive integer weight. Each turn, we choose the two heaviest stones and smash them together. Suppose the stones have weights x and y with x <= y. The result of this smash is: If x == y, both stones are totally destroyed; If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x....

<span title='2021-03-05 00:00:00 +0000 UTC'>March 5, 2021</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

215. Kth Largest Element in an Array

![https://leetcode.com/problems/kth-largest-element-in-an-array/] Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. 1 2 3 4 5 6 7 8 9 10 11 12 Example 1: Input: [3,2,1,5,6,4] and k = 2 Output: 5 Example 2: Input: [3,2,3,1,2,4,5,5,6] and k = 4 Output: 4 Note: You may assume k is always valid, 1 ≤ k ≤ array's length....

<span title='2021-03-05 00:00:00 +0000 UTC'>March 5, 2021</span>&nbsp;·&nbsp;5 min&nbsp;·&nbsp;volyx

347. Top K Frequent Elements

![https://leetcode.com/problems/top-k-frequent-elements/] Given a non-empty array of integers, return the k most frequent elements. 1 2 3 4 5 6 7 8 9 Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size....

<span title='2021-03-05 00:00:00 +0000 UTC'>March 5, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

378. Kth Smallest Element in a Sorted Matrix

![https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/] Given an n x n matrix where each of the rows and columns are sorted in ascending order, return the kth smallest element in the matrix. Note that it is the kth smallest element in the sorted order, not the kth distinct element. 1 2 3 4 5 6 7 8 9 10 Example 1: Input: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8 Output: 13 Explanation: The elements in the matrix are [1,5,9,10,11,12,13,13,15], and the 8th smallest number is 13 Example 2: Input: matrix = [[-5]], k = 1 Output: -5 Constraints:...

<span title='2021-03-05 00:00:00 +0000 UTC'>March 5, 2021</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

57. Insert Interval

![https://leetcode.com/problems/insert-interval/] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Example 1: Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]] Example 2: Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] Output: [[1,2],[3,10],[12,16]] Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10]....

<span title='2021-03-05 00:00:00 +0000 UTC'>March 5, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

703. Kth Largest Element in a Stream

Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element. Implement KthLargest class: KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums. int add(int val) Returns the element representing the kth largest element in the stream. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Example 1: Input ["KthLargest", "add", "add", "add", "add", "add"] [[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]] Output [null, 4, 5, 5, 8, 8] Explanation KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]); kthLargest....

<span title='2021-03-04 00:00:00 +0000 UTC'>March 4, 2021</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

1329. Sort the Matrix Diagonally

![https://leetcode.com/problems/sort-the-matrix-diagonally/] A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix’s end. For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0], mat[3][1], and mat[4][2]. Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and return the resulting matrix....

<span title='2021-03-03 00:00:00 +0000 UTC'>March 3, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

75. Sort Colors

![https://leetcode.com/problems/sort-colors/] Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Example 1: Input: nums = [2,0,2,1,1,0] Output: [0,0,1,1,2,2] Example 2: Input: nums = [2,0,1] Output: [0,1,2] Example 3: Input: nums = [0] Output: [0] Example 4: Input: nums = [1] Output: [1] Constraints:...

<span title='2021-03-03 00:00:00 +0000 UTC'>March 3, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

56. Merge Intervals

![https://leetcode.com/problems/merge-intervals/] Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. 1 2 3 4 5 6 7 8 9 10 11 Example 1: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. Example 2: Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping....

<span title='2021-03-02 00:00:00 +0000 UTC'>March 2, 2021</span>&nbsp;·&nbsp;4 min&nbsp;·&nbsp;volyx