LC675 Cut Off Trees for Golf Event
Problem
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map:
0represents theobstaclecan’t be reached.1represents thegroundcan be walked through.The place with number bigger than 1represents atreecan be walked through, and this positive number represents the tree’s height.In one step you can walk in any of the four directions
top,bottom,leftandrightalso when standing in a point which is a tree you can decide whether or not to cut off the tree.You are asked to cut off all the trees in this forest in the order of tree’s height - always cut off the tree with lowest height first. And after cutting, the original place has the tree will become a grass (value 1).
You will start from the point (0, 0) and you should output the minimum steps you need to walk to cut off all the trees. If you can’t cut off all the trees, output -1 in that situation.
You are guaranteed that no two
treeshave the same height and there is at least one tree needs to be cut off.Example 1:
1
2
3
4
5
6
7 Input:
[
[1,2,3],
[0,0,4],
[7,6,5]
]
Output: 6Example 2:
1
2
3
4
5
6
7 Input:
[
[1,2,3],
[0,0,0],
[7,6,5]
]
Output: -1Example 3:
1
2
3
4
5
6
7
8 Input:
[
[2,3,4],
[0,0,5],
[8,7,6]
]
Output: 6
Explanation: You started from the point (0,0) and you can cut off the tree in (0,0) directly without walking.Constraints:
1 <= forest.length <= 501 <= forest[i].length <= 500 <= forest[i][j] <= 10^9
Think
首先获得所有的树,然后排序,从最矮的开始,每次从当前树的位置bfs到后一棵树。
Code
1 | int cutOffTree(vector<vector<int>>& forest) { |
LC690 Employee Importance
Problem
You are given a data structure of employee information, which includes the employee’s unique id, their importance value and their direct subordinates’ id.
For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.
Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all their subordinates.
Example 1:
1
2
3
4 Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
Output: 11
Explanation:
Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.Note:
- One employee has at most one direct leader and may have several subordinates.
- The maximum number of employees won’t exceed 2000.
Think
BFS, p.s. 可以不使用visited数组,因为一个人只有一个领导。
Code
1 | int getImportance(vector<Employee*> employees, int id) { |
LC744 Find Smallest Letter Greater Than Target
Problem
Given a list of sorted characters
letterscontaining only lowercase letters, and given a target lettertarget, find the smallest element in the list that is larger than the given target.Letters also wrap around. For example, if the target is
target = 'z'andletters = ['a', 'b'], the answer is'a'.Examples:
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 Input:
letters = ["c", "f", "j"]
target = "a"
Output: "c"
Input:
letters = ["c", "f", "j"]
target = "c"
Output: "f"
Input:
letters = ["c", "f", "j"]
target = "d"
Output: "f"
Input:
letters = ["c", "f", "j"]
target = "g"
Output: "j"
Input:
letters = ["c", "f", "j"]
target = "j"
Output: "c"
Input:
letters = ["c", "f", "j"]
target = "k"
Output: "c"Note:
lettershas a length in range[2, 10000].lettersconsists of lowercase letters, and contains at least 2 unique letters.targetis a lowercase letter.
Think
Upper_bound
Code
1 | char nextGreatestLetter(vector<char>& letters, char target) { |
LC753 Cracking the Safe
Problem
There is a box protected by a password. The password is a sequence of
ndigits where each digit can be one of the firstkdigits0, 1, ..., k-1.While entering a password, the last
ndigits entered will automatically be matched against the correct password.For example, assuming the correct password is
"345", if you type"012345", the box will open because the correct password matches the suffix of the entered password.Return any password of minimum length that is guaranteed to open the box at some point of entering it.
Example 1:
1
2
3 Input: n = 1, k = 2
Output: "01"
Note: "10" will be accepted too.Example 2:
1
2
3 Input: n = 2, k = 2
Output: "00110"
Note: "01100", "10011", "11001" will be accepted too.Note:
nwill be in the range[1, 4].kwill be in the range[1, 10].k^nwill be at most4096.
Think
贪心算法,每次修改最后一位数字,注意内层要反向遍历
Code
1 | string crackSafe(int n, int k) { |
LC764 Largest Plus Sign
Problem
In a 2D
gridfrom (0, 0) to (N-1, N-1), every cell contains a1, except those cells in the given listmineswhich are0. What is the largest axis-aligned plus sign of1s contained in the grid? Return the order of the plus sign. If there is none, return 0.An “axis-aligned plus sign of
1s of order k” has some centergrid[x][y] = 1along with 4 arms of lengthk-1going up, down, left, and right, and made of1s. This is demonstrated in the diagrams below. Note that there could be0s or1s beyond the arms of the plus sign, only the relevant area of the plus sign is checked for 1s.Examples of Axis-Aligned Plus Signs of Order k:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 Order 1:
000
010
000
Order 2:
00000
00100
01110
00100
00000
Order 3:
0000000
0001000
0001000
0111110
0001000
0001000
0000000Example 1:
1
2
3
4
5
6
7
8
9 Input: N = 5, mines = [[4, 2]]
Output: 2
Explanation:
11111
11111
11111
11111
11011
In the above grid, the largest plus sign can only be order 2. One of them is marked in bold.Example 2:
1
2
3
4 Input: N = 2, mines = []
Output: 1
Explanation:
There is no plus sign of order 2, but there is of order 1.Example 3:
1
2
3
4 Input: N = 1, mines = [[0, 0]]
Output: 0
Explanation:
There is no plus sign, so return 0.Note:
Nwill be an integer in the range[1, 500].mineswill have length at most5000.mines[i]will be length 2 and consist of integers in the range[0, N-1].- (Additionally, programs submitted in C, C++, or C# will be judged with a slightly smaller time limit.)
Think
分别建立上下左右四个方向上的dp数组,然后取四个当中的最小值即可。
Code
1 | int orderOfLargestPlusSign(int N, vector<vector<int>>& mines) { |
LC787 Cheapest Flights Within K Stops
Problem
There are
ncities connected bymflights. Each flight starts from cityuand arrives atvwith a pricew.Now given all the cities and flights, together with starting city
srcand the destinationdst, your task is to find the cheapest price fromsrctodstwith up tokstops. If there is no such route, output-1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 Example 1:
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.
Example 2:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph looks like this:
The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.Constraints:
- The number of nodes
nwill be in range[1, 100], with nodes labeled from0ton`` - 1.- The size of
flightswill be in range[0, n * (n - 1) / 2].- The format of each flight will be
(src, ``dst``, price).- The price of each flight will be in the range
[1, 10000].kis in the range of[0, n - 1].- There will not be any duplicated flights or self cycles.