Skip to content

Commit e38b82b

Browse files
committed
feat: solve No.1578,980
1 parent 6d23159 commit e38b82b

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# 1578. Minimum Time to Make Rope Colorful
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, String, Dynamic Programming, Greedy.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Alice has `n` balloons arranged on a rope. You are given a **0-indexed** string `colors` where `colors[i]` is the color of the `ith` balloon.
10+
11+
Alice wants the rope to be **colorful**. She does not want **two consecutive balloons** to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it **colorful**. You are given a **0-indexed** integer array `neededTime` where `neededTime[i]` is the time (in seconds) that Bob needs to remove the `ith` balloon from the rope.
12+
13+
Return **the **minimum time** Bob needs to make the rope **colorful****.
14+
15+
 
16+
Example 1:
17+
18+
![](https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg)
19+
20+
```
21+
Input: colors = "abaac", neededTime = [1,2,3,4,5]
22+
Output: 3
23+
Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
24+
Bob can remove the blue balloon at index 2. This takes 3 seconds.
25+
There are no longer two consecutive balloons of the same color. Total time = 3.
26+
```
27+
28+
Example 2:
29+
30+
![](https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg)
31+
32+
```
33+
Input: colors = "abc", neededTime = [1,2,3]
34+
Output: 0
35+
Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.
36+
```
37+
38+
Example 3:
39+
40+
![](https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg)
41+
42+
```
43+
Input: colors = "aabaa", neededTime = [1,2,3,4,1]
44+
Output: 2
45+
Explanation: Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
46+
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
47+
```
48+
49+
 
50+
**Constraints:**
51+
52+
53+
54+
- `n == colors.length == neededTime.length`
55+
56+
- `1 <= n <= 105`
57+
58+
- `1 <= neededTime[i] <= 104`
59+
60+
- `colors` contains only lowercase English letters.
61+
62+
63+
64+
## Solution
65+
66+
```javascript
67+
/**
68+
* @param {string} colors
69+
* @param {number[]} neededTime
70+
* @return {number}
71+
*/
72+
var minCost = function(colors, neededTime) {
73+
var cost = 0;
74+
var sum = 0;
75+
var max = 0;
76+
var color = '';
77+
for (var i = 0; i < colors.length; i++) {
78+
if (color === colors[i]) {
79+
sum += neededTime[i];
80+
max = Math.max(max, neededTime[i]);
81+
} else {
82+
color = colors[i];
83+
cost += (sum - max);
84+
sum = neededTime[i];
85+
max = neededTime[i];
86+
}
87+
}
88+
cost += (sum - max);
89+
return cost;
90+
};
91+
```
92+
93+
**Explain:**
94+
95+
nope.
96+
97+
**Complexity:**
98+
99+
* Time complexity : O(n).
100+
* Space complexity : O(1).

901-1000/980. Unique Paths III.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# 980. Unique Paths III
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Backtracking, Bit Manipulation, Matrix.
5+
- Similar Questions: Sudoku Solver, Unique Paths II, Word Search II.
6+
7+
## Problem
8+
9+
You are given an `m x n` integer array `grid` where `grid[i][j]` could be:
10+
11+
12+
13+
- `1` representing the starting square. There is exactly one starting square.
14+
15+
- `2` representing the ending square. There is exactly one ending square.
16+
17+
- `0` representing empty squares we can walk over.
18+
19+
- `-1` representing obstacles that we cannot walk over.
20+
21+
22+
Return **the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once**.
23+
24+
 
25+
Example 1:
26+
27+
![](https://assets.leetcode.com/uploads/2021/08/02/lc-unique1.jpg)
28+
29+
```
30+
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
31+
Output: 2
32+
Explanation: We have the following two paths:
33+
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
34+
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
35+
```
36+
37+
Example 2:
38+
39+
![](https://assets.leetcode.com/uploads/2021/08/02/lc-unique2.jpg)
40+
41+
```
42+
Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
43+
Output: 4
44+
Explanation: We have the following four paths:
45+
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
46+
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
47+
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
48+
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
49+
```
50+
51+
Example 3:
52+
53+
![](https://assets.leetcode.com/uploads/2021/08/02/lc-unique3-.jpg)
54+
55+
```
56+
Input: grid = [[0,1],[2,0]]
57+
Output: 0
58+
Explanation: There is no path that walks over every empty square exactly once.
59+
Note that the starting and ending square can be anywhere in the grid.
60+
```
61+
62+
 
63+
**Constraints:**
64+
65+
66+
67+
- `m == grid.length`
68+
69+
- `n == grid[i].length`
70+
71+
- `1 <= m, n <= 20`
72+
73+
- `1 <= m * n <= 20`
74+
75+
- `-1 <= grid[i][j] <= 2`
76+
77+
- There is exactly one starting cell and one ending cell.
78+
79+
80+
81+
## Solution
82+
83+
```javascript
84+
/**
85+
* @param {number[][]} grid
86+
* @return {number}
87+
*/
88+
var uniquePathsIII = function(grid) {
89+
var start;
90+
var m = grid.length;
91+
var n = grid[0].length;
92+
var emptyNum = 0;
93+
for (var i = 0; i < m; i++) {
94+
for (var j = 0; j < n; j++) {
95+
if (grid[i][j] === 1) start = [i, j];
96+
else if (grid[i][j] === 0) emptyNum++;
97+
}
98+
}
99+
return getPathNum(start[0], start[1], grid, 0, emptyNum);
100+
};
101+
102+
var getPathNum = function(i, j, grid, visitedNum, emptyNum) {
103+
var res = 0;
104+
var directions = [
105+
[1, 0], // up
106+
[-1, 0], // down
107+
[0, -1], // left
108+
[0, 1], // right
109+
];
110+
for (var k = 0; k < 4; k++) {
111+
var [diffX, diffY] = directions[k];
112+
if (grid[i + diffX] && grid[i + diffX][j + diffY] === 0) {
113+
grid[i + diffX][j + diffY] = -1;
114+
res += getPathNum(i + diffX, j + diffY, grid, visitedNum + 1, emptyNum);
115+
grid[i + diffX][j + diffY] = 0;
116+
} else if (grid[i + diffX] && grid[i + diffX][j + diffY] === 2) {
117+
res += (visitedNum === emptyNum ? 1 : 0);
118+
}
119+
}
120+
return res;
121+
};
122+
```
123+
124+
**Explain:**
125+
126+
nope.
127+
128+
**Complexity:**
129+
130+
* Time complexity : O(n!).
131+
* Space complexity : O(1).

0 commit comments

Comments
 (0)