297. Serialize and Deserialize Binary Tree

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure....

<span title='2021-09-19 00:00:00 +0000 UTC'>September 19, 2021</span>&nbsp;·&nbsp;4 min&nbsp;·&nbsp;volyx

1650. Lowest Common Ancestor of a Binary Tree III

Given two nodes of a binary tree p and q, return their lowest common ancestor (LCA). Each node will have a reference to its parent node. The definition for Node is below: 1 2 3 4 5 6 class Node { public int val; public Node left; public Node right; public Node parent; } According to the definition of LCA on Wikipedia: “The lowest common ancestor of two nodes p and q in a tree T is the lowest node that has both p and q as descendants (where we allow a node to be a descendant of itself)....

<span title='2021-09-16 00:00:00 +0000 UTC'>September 16, 2021</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

1662. Check If Two String Arrays are Equivalent

Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise. A string is represented by an array if the array elements concatenated in order forms the string. 1 2 3 4 5 6 7 8 Example 1: Input: word1 = ["ab", "c"], word2 = ["a", "bc"] Output: true Explanation: word1 represents string "ab" + "c" -> "abc" word2 represents string "a" + "bc" -> "abc" The strings are the same, so return true....

<span title='2021-09-16 00:00:00 +0000 UTC'>September 16, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

259. 3Sum Smaller

259. 3Sum Smaller Given an array of n integers nums and an integer target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Example 1: Input: nums = [-2,0,1,3], target = 2 Output: 2 Explanation: Because there are two triplets which sums are less than 2: [-2,0,1] [-2,0,3] Example 2: Input: nums = [], target = 0 Output: 0 Example 3: Input: nums = [0], target = 0 Output: 0 Constraints:...

<span title='2021-09-09 00:00:00 +0000 UTC'>September 9, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Two/Three Sum Workout

Two/Three Sum Workout https://leetcode.com/problems/two-sum/ 1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> valueIndex = new HashMap<>(); for (int i = 0; i < nums.length; i++) { Integer find = valueIndex.get(target - nums[i]); if (find != null) { return new int[] {find, i}; } valueIndex.put(nums[i], i); } return new int[] {-1, -1}; } } https://leetcode....

<span title='2021-09-09 00:00:00 +0000 UTC'>September 9, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

315. Count of Smaller Numbers After Self

315. Count of Smaller Numbers After Self You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Example 1: Input: nums = [5,2,6,1] Output: [2,1,1,0] Explanation: To the right of 5 there are 2 smaller elements (2 and 1)....

<span title='2021-09-08 00:00:00 +0000 UTC'>September 8, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

350. Intersection of Two Arrays II

350. Intersection of Two Arrays II Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order. 1 2 3 4 5 6 7 8 9 10 Example 1: Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2,2] Example 2: Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [4,9] Explanation: [9,4] is also accepted....

<span title='2021-09-07 00:00:00 +0000 UTC'>September 7, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

255. Verify Preorder Sequence in Binary Search Tree

255. Verify Preorder Sequence in Binary Search Tree Given an array of unique integers preorder, return true if it is the correct preorder traversal sequence of a binary search tree. 1 2 3 4 5 6 7 8 9 Example 1: Input: preorder = [5,2,1,3,6] Output: true Example 2: Input: preorder = [5,2,6,1,3] Output: false Constraints: 1 <= preorder.length <= 10^4 1 <= preorder[i] <= 10^4 All the elements of preorder are unique....

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

2654. Maximum Binary Tree

654. Maximum Binary Tree You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm: Create a root node whose value is the maximum value in nums. Recursively build the left subtree on the subarray prefix to the left of the maximum value. Recursively build the right subtree on the subarray suffix to the right of the maximum value....

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

307. Range Sum Query - Mutable

307. Range Sum Query - Mutable Given an integer array nums, handle multiple queries of the following types: Update the value of an element in nums. Calculate the sum of the elements of nums between indices left and right inclusive where left <= right. Implement the NumArray class: NumArray(int[] nums) Initializes the object with the integer array nums. void update(int index, int val) Updates the value of nums[index] to be val....

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

304. Range Sum Query 2D - Immutable

304. Range Sum Query 2D - Immutable Given a 2D matrix matrix, handle multiple queries of the following type: Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). Implement the NumMatrix class: NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix. int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2)....

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

962. Maximum Width Ramp

962. Maximum Width Ramp A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j]. The width of such a ramp is j - i. Given an integer array nums, return the maximum width of a ramp in nums. If there is no ramp in nums, return 0. 1 2 3 4 5 6 7 8 9 10 11 Example 1: Input: nums = [6,0,8,2,1,5] Output: 4 Explanation: The maximum width ramp is achieved at (i, j) = (1, 5): nums[1] = 0 and nums[5] = 5....

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

319. Bulb Switcher

319. Bulb Switcher There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off if it’s on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Return the number of bulbs that are on after n rounds....

<span title='2021-09-01 00:00:00 +0000 UTC'>September 1, 2021</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

581. Shortest Unsorted Continuous Subarray

581. Shortest Unsorted Continuous Subarray Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order. Return the shortest such subarray and output its length. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Example 1: Input: nums = [2,6,4,8,10,9,15] Output: 5 Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order....

<span title='2021-08-26 00:00:00 +0000 UTC'>August 26, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

907. Sum of Subarray Minimums

907. Sum of Subarray Minimums Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo 109 + 7. 1 2 3 4 5 6 7 8 9 10 11 12 13 Example 1: Input: arr = [3,1,2,4] Output: 17 Explanation: Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]....

<span title='2021-08-25 00:00:00 +0000 UTC'>August 25, 2021</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx