记录下做题思路。
665.Non-decreasing Array
Given an array with n
integers, your task is to check if it could become non-decreasing by modifying at most 1
element.
We define an array is non-decreasing if array[i] <= array[i + 1]
holds for every i
(1 <= i < n).
Example 1:
1 | Input: [4,2,3] |
Example 2:
1 | Input: [4,2,1] |
1 | /** |
666. Path Sum IV
If the depth of a tree is smaller than 5
, then this tree can be represented by a list of three-digits integers.
For each integer in this list:
- The hundreds digit represents the depth
D
of this node,1 <= D <= 4.
- The tens digit represents the position
P
of this node in the level it belongs to,1 <= P <= 8
. The position is the same as that in a full binary tree. - The units digit represents the value
V
of this node,0 <= V <= 9.
Given a list of ascending
three-digits integers representing a binary with the depth smaller than 5. You need to return the sum of all paths from the root towards the leaves.
1 | var pathSum = function(nums,sum=0) { |
667.Beautiful Arrangement II
Given two integers n
and k
, you need to construct a list which contains n
different positive integers ranging from 1
to n
and obeys the following requirement:
Suppose this list is [a1, a2, a3, … , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, … , |an-1 - an|] has exactly k
distinct integers.
If there are multiple answers, print any of them.
Example 1:
1 | Input: n = 3, k = 1 |
Example 2:
1 | Input: n = 3, k = 2 |
1 | /** |
668. Kth largest Number in Multiplication Table
Nearly every one have used the Multiplication Table. But could you find out the k-th
largest number quickly from the multiplication table?
Given the height m
and the length n
of a m * n
Multiplication Table, and a positive integer k
, you need to return the k-th
largest number in this table.
Example 1:
1 | Input: m = 3, n = 3, k = 5 |
Example 2:
1 | Input: m = 2, n = 3, k = 6 |
1 | /** |