You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.
LeetCode wants to give one of its best employees the option to travel among N cities to collect algorithm problems. But all work and no play makes Jack a dull boy, you could take vacations in some particular cities and weeks. Your job is to schedule the traveling to maximize the number of vacation days you could take, but there are certain rules and restrictions you need to follow.
8
+
9
+
Rules and restrictions:
10
+
You can only travel among N cities, represented by indexes from 0 to N-1. Initially, you are in the city indexed 0 on Monday.
11
+
The cities are connected by flights. The flights are represented as a N*N matrix (not necessary symmetrical), called flights representing the airline status from the city i to the city j. If there is no flight from the city i to the city j, flights[i][j] = 0; Otherwise, flights[i][j] = 1. Also, flights[i][i] = 0 for all i.
12
+
You totally have K weeks (each week has 7 days) to travel. You can only take flights at most once per day and can only take flights on each week's Monday morning. Since flight time is so short, we don't consider the impact of flight time.
13
+
For each city, you can only have restricted vacation days in different weeks, given an N*K matrix called days representing this relationship. For the value of days[i][j], it represents the maximum days you could take vacation in the city i in the week j.
14
+
You're given the flights matrix and days matrix, and you need to output the maximum vacation days you could take during K weeks.
15
+
16
+
Example 1:
17
+
Input:flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[1,3,1],[6,0,3],[3,3,3]]
18
+
Output: 12
19
+
Explanation:
20
+
Ans = 6 + 3 + 3 = 12.
21
+
22
+
One of the best strategies is:
23
+
1st week : fly from city 0 to city 1 on Monday, and play 6 days and work 1 day.
24
+
(Although you start at city 0, we could also fly to and start at other cities since it is Monday.)
25
+
2nd week : fly from city 1 to city 2 on Monday, and play 3 days and work 4 days.
26
+
3rd week : stay at city 2, and play 3 days and work 4 days.
27
+
Example 2:
28
+
Input:flights = [[0,0,0],[0,0,0],[0,0,0]], days = [[1,1,1],[7,7,7],[7,7,7]]
29
+
Output: 3
30
+
Explanation:
31
+
Ans = 1 + 1 + 1 = 3.
32
+
33
+
Since there is no flights enable you to move to another city, you have to stay at city 0 for the whole 3 weeks.
34
+
For each week, you only have one day to play and six days to work.
35
+
So the maximum number of vacation days is 3.
36
+
Example 3:
37
+
Input:flights = [[0,1,1],[1,0,1],[1,1,0]], days = [[7,0,0],[0,7,0],[0,0,7]]
38
+
Output: 21
39
+
Explanation:
40
+
Ans = 7 + 7 + 7 = 21
41
+
42
+
One of the best strategies is:
43
+
1st week : stay at city 0, and play 7 days.
44
+
2nd week : fly from city 0 to city 1 on Monday, and play 7 days.
45
+
3rd week : fly from city 1 to city 2 on Monday, and play 7 days.
46
+
Note:
47
+
N and K are positive integers, which are in the range of [1, 100].
48
+
In the matrix flights, all the values are integers in the range of [0, 1].
49
+
In the matrix days, all the values are integers in the range [0, 7].
50
+
You could stay at a city beyond the number of vacation days, but you should work on the extra days, which won't be counted as vacation days.
51
+
If you fly from the city A to the city B and take the vacation on that day, the deduction towards vacation days will count towards the vacation days of city B in that week.
52
+
We don't consider the impact of flight hours towards the calculation of vacation days.
Given a robot cleaner in a room modeled as a grid.
16
+
17
+
Each cell in the grid can be empty or blocked.
18
+
19
+
The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees.
20
+
21
+
When it tries to move into a blocked cell, its bumper sensor detects the obstacle and it stays on the current cell.
22
+
23
+
Design an algorithm to clean the entire room using only the 4 given APIs shown below.
24
+
25
+
interface Robot {
26
+
// returns true if next cell is open and robot moves into the cell.
27
+
// returns false if next cell is obstacle and robot stays on the current cell.
28
+
boolean move();
29
+
30
+
// Robot will stay on the same cell after calling turnLeft/turnRight.
31
+
// Each turn will be 90 degrees.
32
+
void turnLeft();
33
+
void turnRight();
34
+
35
+
// Clean the current cell.
36
+
void clean();
37
+
}
38
+
Example:
39
+
40
+
Input:
41
+
room = [
42
+
[1,1,1,1,1,0,1,1],
43
+
[1,1,1,1,1,0,1,1],
44
+
[1,0,1,1,1,1,1,1],
45
+
[0,0,0,1,0,0,0,0],
46
+
[1,1,1,1,1,1,1,1]
47
+
],
48
+
row = 1,
49
+
col = 3
50
+
51
+
Explanation:
52
+
All grids in the room are marked by either 0 or 1.
53
+
0 means the cell is blocked, while 1 means the cell is accessible.
54
+
The robot initially starts at the position of row=1, col=3.
55
+
From the top left corner, its position is one row below and three columns right.
56
+
Notes:
57
+
58
+
The input is only given to initialize the room and the robot's position internally. You must solve this problem "blindfolded". In other words, you must control the robot using only the mentioned 4 APIs, without knowing the room layout and the initial robot's position.
59
+
The robot's initial position will always be in an accessible cell.
60
+
The initial direction of the robot will be facing up.
61
+
All accessible cells are connected, which means the all cells marked as 1 will be accessible by the robot.
62
+
Assume all four edges of the grid are all surrounded by wall.
63
+
*/
64
+
65
+
/**
66
+
* // This is the robot's control interface.
67
+
* // You should not implement it, or speculate about its implementation
68
+
* interface Robot {
69
+
* // Returns true if the cell in front is open and robot moves into the cell.
70
+
* // Returns false if the cell in front is blocked and robot stays in the current cell.
71
+
* public boolean move();
72
+
*
73
+
* // Robot will stay in the same cell after calling turnLeft/turnRight.
74
+
* // Each turn will be 90 degrees.
75
+
* public void turnLeft();
76
+
* public void turnRight();
77
+
*
78
+
* // Clean the current cell.
79
+
* public void clean();
80
+
* }
81
+
*/
82
+
/*
83
+
- Different from regular dfs to visit all, the robot move() function need to be called, backtrack needs to move() manually and backtracking path shold not be blocked by visited positions
84
+
- Mark on the way in using set, but backtrack directly without re-check against set
85
+
- Mark coordinate 'x@y'
86
+
- Use degree to mark direction, rather than {0,0,1,-1}
87
+
- Backtrack: turn 2 times to revert, move 1 step, and turn 2 times to revert back.
88
+
*/
89
+
90
+
classSolution {
91
+
int[] dx = {-1, 0, 1, 0};
92
+
int[] dy = {0, 1, 0, -1};
93
+
publicvoidcleanRoom(Robotrobot) {
94
+
// use 'x@y' mark visited nodes, where x,y are integers tracking the coordinates
0 commit comments