Given a positive integerContinue reading “[Leetcode]279. Perfect Squares”n, find the least number of perfect square numbers (for example,`1, 4, 9, 16, ...`

) which sum ton.Example 1:Input:n=`12`

Output:3Explanation:`12 = 4 + 4 + 4.`

## [Leetcode]222. Count Complete Tree Nodes

Given aContinue reading “[Leetcode]222. Count Complete Tree Nodes”completebinary tree, count the number of nodes.Note: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 2Definition of a complete binary tree from Wikipedia:^{h}nodes inclusive at the last level h.Example:Input:1 / \ 2 3 / \ / 4 5 6Output:6

## [Leetcode problems]137. Single Number II

Given aContinue reading “[Leetcode problems]137. Single Number II”non-emptyarray of integers, every element appearsthreetimes 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:3

## [Leetcode problems]136. Single Number

Given aContinue reading “[Leetcode problems]136. Single Number”non-emptyarray of integers, every element appearstwiceexcept 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:1

## [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, the`nth`

lake becomes full of water. If it rains over a lake which isfull of water, there will be aflood. Your goal is to avoid the flood in any lake. Given an integer array`rains`

where:`rains[i] > 0`

means there will be rains over the`rains[i]`

lake.`rains[i] == 0`

means there are no rains this day and you can chooseone lakethis day anddry it. Returnan arraywhere:`ans`

`ans.length == rains.length`

`ans[i] == -1`

if`rains[i] > 0`

.`ans[i]`

is the lake you choose to dry in the`ith`

day if`rains[i] == 0`

. If there are multiple valid answers returnanyof them. If it is impossible to avoid flood returnan 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,...,`

contains a total ofn]n! unique permutations. By listing and labeling all of the permutations in order, we get the following sequence forn= 3:`"123";"132"`

;`"213"`

;`"231"`

;`"312"`

;`"321"`

Givennandk, return thek^{t}^{h}permutation sequence.Note:Givennwill be between 1 and 9 inclusive. Givenkwill be between 1 andn! inclusive.Example 1:Input:n = 3, k = 3Output:"213"

## [Leetcode problems]75. Sort Colors

Given an array withContinue reading “[Leetcode problems]75. Sort Colors”nobjects colored red, white or blue, sort themin-placeso 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]

## [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 integer`m`

and an integer`k`

. We need to make`m`

bouquets. To make a bouquet, you need to use`k`

adjacent flowersfrom the garden. The garden consists of`n`

flowers, the`ith`

flower will bloom in the`bloomDay[i]`

and then can be used inexactly onebouquet. Returnthe minimum number of daysyou need to wait to be able to make`m`

bouquets from the garden. If it is impossible to make`m`

bouquets return-1.Example 1:Input:bloomDay = [1,10,3,10,2], m = 3, k = 1Output:3Explanation: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.