Given a positive integer n, find the least number of perfect square numbers (for example,Continue reading “[Leetcode]279. Perfect Squares”1, 4, 9, 16, ...
) which sum to n. Example 1: Input: n =12
Output: 3 Explanation:12 = 4 + 4 + 4.
[Leetcode]222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h. Example: Input: 1 / \ 2 3 / \ / 4 5 6 Output: 6Continue reading “[Leetcode]222. Count Complete Tree Nodes”
[Leetcode problems]137. Single Number II
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: Input: [2,2,3,2] Output: 3Continue reading “[Leetcode problems]137. Single Number II”
[Leetcode problems]136. Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: Input: [2,2,1] Output: 1Continue reading “[Leetcode problems]136. Single Number”
[Leetcode problems]1488. Avoid Flood in The City
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over theContinue reading “[Leetcode problems]1488. Avoid Flood in The City”nth
lake, thenth
lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer arrayrains
where:rains[i] > 0
means there will be rains over therains[i]
lake.rains[i] == 0
means there are no rains this day and you can choose one lake this day and dry it. Return an arrayans
where:ans.length == rains.length
ans[i] == -1
ifrains[i] > 0
.ans[i]
is the lake you choose to dry in theith
day ifrains[i] == 0
. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4) Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1]
[Leetcode problems]60. Permutation Sequence
The setContinue reading “[Leetcode problems]60. Permutation Sequence”[1,2,3,...,n]
contains a total of n! unique permutations. By listing and labeling all of the permutations in order, we get the following sequence for n = 3:"123";"132"
;"213"
;"231"
;"312"
;"321"
Given n and k, return the kth permutation sequence. Note: Given n will be between 1 and 9 inclusive. Given k will be between 1 and n! inclusive. Example 1: Input: n = 3, k = 3 Output: "213"
[Leetcode problems]75. Sort Colors
Given an array 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. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You are not suppose to use the library's sort function for this problem. Example: Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2]Continue reading “[Leetcode problems]75. Sort Colors”
[Leetcode problems]1482. Minimum Number of Days to Make m Bouquets
Given an integer arrayContinue reading “[Leetcode problems]1482. Minimum Number of Days to Make m Bouquets”bloomDay
, an integerm
and an integerk
. We need to makem
bouquets. To make a bouquet, you need to usek
adjacent flowers from the garden. The garden consists ofn
flowers, theith
flower will bloom in thebloomDay[i]
and then can be used in exactly one bouquet. Return the minimum number of days you need to wait to be able to makem
bouquets from the garden. If it is impossible to makem
bouquets return -1. Example 1: Input: bloomDay = [1,10,3,10,2], m = 3, k = 1 Output: 3 Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden. We need 3 bouquets each should contain 1 flower. After day 1: [x, _, _, _, _] // we can only make one bouquet. After day 2: [x, _, _, _, x] // we can only make two bouquets. After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.