Populating Next Right Pointers in Each Node

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: 1 2 3 4 5 6 struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL....

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

Diagonal Traverse

Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. Example: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,4,7,5,3,6,8,9] Explanation: Note: The total number of elements of the given matrix will not exceed 10,000. Solution: 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 32 33 34 35 36 37 38 39 40 41 42 class Solution { public int[] findDiagonalOrder(int[][] A) { if (A == null || A....

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

Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 1 2 3 4 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a non-empty string containing only digits, determine the total number of ways to decode it. 1 2 3 4 5 Example 1: Input: "12" Output: 2 Explanation: It could be decoded as "AB" (1 2) or "L" (12). 1 2 3 4 5 Example 2: Input: "226" Output: 3 Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6)....

<span title='2020-08-23 00:00:00 +0000 UTC'>August 23, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list. Note: Do not modify the linked list. Example 1: Input: head = [3,2,0,-4], pos = 1 Output: tail connects to node index 1 Explanation: There is a cycle in the linked list, where tail connects to the second node....

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

Check If a String Contains All Binary Codes of Size K

Given a binary string s and an integer k. Return True if every binary code of length k is a substring of s. Otherwise, return False. Example 1: 1 2 3 Input: s = "00110110", k = 2 Output: true Explanation: The binary codes of length 2 are "00", "01", "10" and "11". They can be all found as substrings at indicies 0, 1, 3 and 2 respectively. Example 2:...

<span title='2020-07-17 00:00:00 +0000 UTC'>July 17, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Daily Temperatures

Given a list of daily temperatures T, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead. For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0]....

<span title='2020-07-16 00:00:00 +0000 UTC'>July 16, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Maximum Width of Binary Tree

Given a binary tree, write a function to get the maximum width of the given tree. The maximum width of a tree is the maximum width among all levels. The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation. It is guaranteed that the answer will in the range of 32-bit signed integer....

<span title='2020-07-16 00:00:00 +0000 UTC'>July 16, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

3Sum

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. 1 2 3 4 5 6 7 8 9 Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Solution:...

<span title='2020-07-14 00:00:00 +0000 UTC'>July 14, 2020</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

Diagonal Traverse II

Given a list of lists of integers, nums, return all elements of nums in diagonal order as shown in the below images. Example 1: 1 2 Input: nums = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,4,2,7,5,3,8,6,9] Example 2: 1 2 Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]] Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16] Example 3: 1 2 Input: nums = [[1,2,3],[4],[5,6,7],[8],[9,10,11]] Output: [1,4,2,5,3,8,6,9,7,10,11] Example 4: 1 2 Input: nums = [[1,2,3,4,5,6]] Output: [1,2,3,4,5,6] Constraints: 1 <= nums.length <= 10^5 1 <= nums[i]....

<span title='2020-07-11 00:00:00 +0000 UTC'>July 11, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Prison Cells After N Days

There are 8 prison cells in a row, and each cell is either occupied or vacant. Each day, whether the cell is occupied or vacant changes according to the following rules: If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied. Otherwise, it becomes vacant. (Note that because the prison is a row, the first and the last cells in the row can’t have two adjacent neighbors....

<span title='2020-07-04 00:00:00 +0000 UTC'>July 4, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Rotting Oranges

In a given grid, each cell can have one of three values: the value 0 representing an empty cell; the value 1 representing a fresh orange; the value 2 representing a rotten orange. Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1 instead....

<span title='2020-07-01 00:00:00 +0000 UTC'>July 1, 2020</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

279. Perfect Squares

Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n. 1 2 3 4 5 Example 1: Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4. 1 2 3 4 5 Example 2: Input: n = 13 Output: 2 Explanation: 13 = 4 + 9. Solution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution { public int numSquares(int n) { int[] dp = new int[n + 1]; dp[0] = 0; dp[1] = 1; for (int i = 2; i < dp....

<span title='2020-06-29 00:00:00 +0000 UTC'>June 29, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Largest Divisible Subset

Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. If there are multiple solutions, return any subset is fine. Example 1: Input: [1,2,3] Output: [1,2] (of course, [1,3] will also be ok) Example 2: Input: [1,2,4,8] Output: [1,2,4,8] Solution: 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 32 33 34 35 class Solution { public List<Integer> largestDivisibleSubset(int[] nums) { int n = nums....

<span title='2020-06-29 00:00:00 +0000 UTC'>June 29, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Longest Increasing Subsequence

Given an unsorted array of integers, find the length of longest increasing subsequence. 1 2 3 4 5 Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be more than one LIS combination, it is only necessary for you to return the length. Your algorithm should run in O(n2) complexity. Follow up: Could you improve it to O(n log n) time complexity?...

<span title='2020-06-28 00:00:00 +0000 UTC'>June 28, 2020</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx

Cheapest Flights Within K Stops

There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w. Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1. Example 1: 1 2 3 4 5 6 Input: n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]] src = 0, dst = 2, k = 1 Output: 200 Explanation: The graph looks like this: The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture....

<span title='2020-06-26 00:00:00 +0000 UTC'>June 26, 2020</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;volyx