Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle 1 2 3 4 5 6 [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle....

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

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

Plus One

Given a non-empty array of digits representing a non-negative integer, increment one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself. Example 1: 1 2 3 Input: [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123....

<span title='2020-07-12 00:00:00 +0000 UTC'>July 12, 2020</span>&nbsp;·&nbsp;2 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

Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note: 0 ≤ x, y < 231. 1 2 3 4 5 6 7 8 9 10 11 Example: Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different....

<span title='2020-07-07 00:00:00 +0000 UTC'>July 7, 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

Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree [3,9,20,null,null,15,7], 1 2 3 4 5 3 / \ 9 20 / \ 15 7 return its bottom-up level order traversal as: 1 2 3 4 5 [ [15,7], [9,20], [3] ] 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 43 44 45 46 47 /** * Definition for a binary tree node....

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

Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within the range of a 32-bit signed integer. Example 1: 1 2 3 4 5 6 n = 5 The coins can form the following rows: ¤ ¤ ¤ ¤ ¤ Because the 3rd row is incomplete, we return 2....

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