Skip to content

Commit 166ec20

Browse files
authored
feat: add solutions to lcp problems: No.55,56 (doocs#1298)
* No.55.采集果实 * No.56.信物传送
1 parent eaa6853 commit 166ec20

12 files changed

+482
-2
lines changed

lcp/LCP 55. 采集果实/README.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,85 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:贪心**
58+
59+
对于每个任务,我们贪心地按照 $limit$ 的大小来采集,那么每个任务需要的时间为 $\lceil \frac{num}{limit} \rceil \times time[type]$,其中 $\lceil x \rceil$ 表示对 $x$ 向上取整。我们将所有任务需要的时间求和即为答案。
60+
61+
时间复杂度 $O(n)$,其中 $n$ 是数组 $fruits$ 的长度。空间复杂度 $O(1)$。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
6066

6167
<!-- 这里可写当前语言的特殊实现逻辑 -->
6268

6369
```python
64-
70+
class Solution:
71+
def getMinimumTime(
72+
self, time: List[int], fruits: List[List[int]], limit: int
73+
) -> int:
74+
return sum((num + limit - 1) // limit * time[i] for i, num in fruits)
6575
```
6676

6777
### **Java**
6878

6979
<!-- 这里可写当前语言的特殊实现逻辑 -->
7080

7181
```java
82+
class Solution {
83+
public int getMinimumTime(int[] time, int[][] fruits, int limit) {
84+
int ans = 0;
85+
for (int[] f : fruits) {
86+
int i = f[0], num = f[1];
87+
ans += (num + limit - 1) / limit * time[i];
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int getMinimumTime(vector<int>& time, vector<vector<int>>& fruits, int limit) {
100+
int ans = 0;
101+
for (auto& f : fruits) {
102+
int i = f[0], num = f[1];
103+
ans += (num + limit - 1) / limit * time[i];
104+
}
105+
return ans;
106+
}
107+
};
108+
```
109+
110+
### **Go**
111+
112+
```go
113+
func getMinimumTime(time []int, fruits [][]int, limit int) (ans int) {
114+
for _, f := range fruits {
115+
i, num := f[0], f[1]
116+
ans += (num + limit - 1) / limit * time[i]
117+
}
118+
return
119+
}
120+
```
72121

122+
### **TypeScript**
123+
124+
```ts
125+
function getMinimumTime(
126+
time: number[],
127+
fruits: number[][],
128+
limit: number,
129+
): number {
130+
let ans = 0;
131+
for (const [i, num] of fruits) {
132+
ans += Math.ceil(num / limit) * time[i];
133+
}
134+
return ans;
135+
}
73136
```
74137

75138
### **...**

lcp/LCP 55. 采集果实/Solution.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int getMinimumTime(vector<int>& time, vector<vector<int>>& fruits, int limit) {
4+
int ans = 0;
5+
for (auto& f : fruits) {
6+
int i = f[0], num = f[1];
7+
ans += (num + limit - 1) / limit * time[i];
8+
}
9+
return ans;
10+
}
11+
};

lcp/LCP 55. 采集果实/Solution.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func getMinimumTime(time []int, fruits [][]int, limit int) (ans int) {
2+
for _, f := range fruits {
3+
i, num := f[0], f[1]
4+
ans += (num + limit - 1) / limit * time[i]
5+
}
6+
return
7+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int getMinimumTime(int[] time, int[][] fruits, int limit) {
3+
int ans = 0;
4+
for (int[] f : fruits) {
5+
int i = f[0], num = f[1];
6+
ans += (num + limit - 1) / limit * time[i];
7+
}
8+
return ans;
9+
}
10+
}

lcp/LCP 55. 采集果实/Solution.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def getMinimumTime(
3+
self, time: List[int], fruits: List[List[int]], limit: int
4+
) -> int:
5+
return sum((num + limit - 1) // limit * time[i] for i, num in fruits)

lcp/LCP 55. 采集果实/Solution.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function getMinimumTime(
2+
time: number[],
3+
fruits: number[][],
4+
limit: number,
5+
): number {
6+
let ans = 0;
7+
for (const [i, num] of fruits) {
8+
ans += Math.ceil(num / limit) * time[i];
9+
}
10+
return ans;
11+
}

lcp/LCP 56. 信物传送/README.md

+199-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,220 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:双端队列 BFS(0-1 BFS)**
57+
58+
每走到一个格子 $(i, j)$,有 $4$ 个方向可以走,如果方向与当前格子的方向相同,那么不需要施法,否则需要施法一次。
59+
60+
因此,我们可以使用 BFS 求出从起点到终点的最短路径。
61+
62+
我们定义一个双端队列 $q$,存储当前可以到达的格子,每次从队首取出一个格子 $(i, j)$,然后向四个方向扩展,如果扩展的格子 $(x, y)$ 不需要施法,那么将其加入队首,否则加入队尾。当我们第一次到达终点时,就得到了最短路径。
63+
64+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
65+
5666
<!-- tabs:start -->
5767

5868
### **Python3**
5969

6070
<!-- 这里可写当前语言的特殊实现逻辑 -->
6171

6272
```python
63-
73+
class Solution:
74+
def conveyorBelt(self, matrix: List[str], start: List[int], end: List[int]) -> int:
75+
dirs = (-1, 0, 1, 0, -1)
76+
d = {"^": 0, "v": 2, "<": 3, ">": 1}
77+
i, j = start
78+
q = deque([(i, j)])
79+
m, n = len(matrix), len(matrix[0])
80+
dist = [[inf] * n for _ in range(m)]
81+
dist[i][j] = 0
82+
while 1:
83+
i, j = q.popleft()
84+
if i == end[0] and j == end[1]:
85+
return int(dist[i][j])
86+
for k in range(4):
87+
x, y = i + dirs[k], j + dirs[k + 1]
88+
t = dist[i][j] + int(k != d[matrix[i][j]])
89+
if 0 <= x < m and 0 <= y < n and t < dist[x][y]:
90+
dist[x][y] = t
91+
if dist[x][y] == dist[i][j]:
92+
q.appendleft((x, y))
93+
else:
94+
q.append((x, y))
6495
```
6596

6697
### **Java**
6798

6899
<!-- 这里可写当前语言的特殊实现逻辑 -->
69100

70101
```java
102+
class Solution {
103+
public int conveyorBelt(String[] matrix, int[] start, int[] end) {
104+
int[] dirs = {-1, 0, 1, 0, -1};
105+
Map<Character, Integer> d = new HashMap<>(4);
106+
d.put('^', 0);
107+
d.put('>', 1);
108+
d.put('v', 2);
109+
d.put('<', 3);
110+
Deque<int[]> q = new ArrayDeque<>();
111+
q.offer(new int[] {start[0], start[1]});
112+
int m = matrix.length, n = matrix[0].length();
113+
int[][] dist = new int[m][n];
114+
for (int[] row : dist) {
115+
Arrays.fill(row, 1 << 30);
116+
}
117+
dist[start[0]][start[1]] = 0;
118+
while (true) {
119+
int[] p = q.poll();
120+
int i = p[0], j = p[1];
121+
if (i == end[0] && j == end[1]) {
122+
return dist[i][j];
123+
}
124+
for (int k = 0; k < 4; ++k) {
125+
int x = i + dirs[k], y = j + dirs[k + 1];
126+
int t = dist[i][j] + (k == d.get(matrix[i].charAt(j)) ? 0 : 1);
127+
if (x >= 0 && x < m && y >= 0 && y < n && t < dist[x][y]) {
128+
dist[x][y] = t;
129+
if (dist[x][y] == dist[i][j]) {
130+
q.offerFirst(new int[] {x, y});
131+
} else {
132+
q.offerLast(new int[] {x, y});
133+
}
134+
}
135+
}
136+
}
137+
}
138+
}
139+
```
140+
141+
### **C++**
142+
143+
```cpp
144+
class Solution {
145+
public:
146+
int conveyorBelt(vector<string>& matrix, vector<int>& start, vector<int>& end) {
147+
int dirs[5] = {-1, 0, 1, 0, -1};
148+
unordered_map<char, int> d;
149+
d['^'] = 0;
150+
d['>'] = 1;
151+
d['v'] = 2;
152+
d['<'] = 3;
153+
deque<pair<int, int>> q;
154+
q.emplace_back(start[0], start[1]);
155+
int m = matrix.size(), n = matrix[0].size();
156+
int dist[m][n];
157+
memset(dist, 0x3f, sizeof(dist));
158+
dist[start[0]][start[1]] = 0;
159+
while (1) {
160+
auto [i, j] = q.front();
161+
q.pop_front();
162+
if (i == end[0] && j == end[1]) {
163+
return dist[i][j];
164+
}
165+
for (int k = 0; k < 4; ++k) {
166+
int x = i + dirs[k], y = j + dirs[k + 1];
167+
int t = dist[i][j] + (k == d[matrix[i][j]] ? 0 : 1);
168+
if (x >= 0 && x < m && y >= 0 && y < n && t < dist[x][y]) {
169+
dist[x][y] = t;
170+
if (dist[x][y] == dist[i][j]) {
171+
q.emplace_front(x, y);
172+
} else {
173+
q.emplace_back(x, y);
174+
}
175+
}
176+
}
177+
}
178+
}
179+
};
180+
```
181+
182+
### **Go**
183+
184+
```go
185+
func conveyorBelt(matrix []string, start []int, end []int) int {
186+
dirs := [5]int{-1, 0, 1, 0, -1}
187+
d := map[byte]int{
188+
'^': 0,
189+
'>': 1,
190+
'v': 2,
191+
'<': 3,
192+
}
193+
q := [][2]int{[2]int{start[0], start[1]}}
194+
m, n := len(matrix), len(matrix[0])
195+
dist := make([][]int, m)
196+
for i := range dist {
197+
dist[i] = make([]int, n)
198+
for j := range dist[i] {
199+
dist[i][j] = 1 << 30
200+
}
201+
}
202+
dist[start[0]][start[1]] = 0
203+
for {
204+
p := q[0]
205+
i, j := p[0], p[1]
206+
if i == end[0] && j == end[1] {
207+
return dist[i][j]
208+
}
209+
q = q[1:]
210+
for k := 0; k < 4; k++ {
211+
x, y := i+dirs[k], j+dirs[k+1]
212+
t := dist[i][j]
213+
if k != d[matrix[i][j]] {
214+
t++
215+
}
216+
if x >= 0 && x < m && y >= 0 && y < n && t < dist[x][y] {
217+
dist[x][y] = t
218+
if dist[x][y] == dist[i][j] {
219+
q = append([][2]int{[2]int{x, y}}, q...)
220+
} else {
221+
q = append(q, [2]int{x, y})
222+
}
223+
}
224+
}
225+
}
226+
}
227+
```
228+
229+
### **TypeScript**
71230

231+
```ts
232+
function conveyorBelt(
233+
matrix: string[],
234+
start: number[],
235+
end: number[],
236+
): number {
237+
const dirs = [-1, 0, 1, 0, -1];
238+
const d: Map<string, number> = new Map();
239+
d.set('^', 0);
240+
d.set('>', 1);
241+
d.set('v', 2);
242+
d.set('<', 3);
243+
const q: number[][] = [start];
244+
const m = matrix.length;
245+
const n = matrix[0].length;
246+
const dist: number[][] = Array(m)
247+
.fill(0)
248+
.map(() => Array(n).fill(1 << 30));
249+
dist[start[0]][start[1]] = 0;
250+
while (true) {
251+
const [i, j] = q.shift()!;
252+
if (i === end[0] && j === end[1]) {
253+
return dist[i][j];
254+
}
255+
for (let k = 0; k < 4; ++k) {
256+
const x = i + dirs[k];
257+
const y = j + dirs[k + 1];
258+
const t = dist[i][j] + (d.get(matrix[i][j]) === k ? 0 : 1);
259+
if (x >= 0 && x < m && y >= 0 && y < n && t < dist[x][y]) {
260+
dist[x][y] = t;
261+
if (t == dist[i][j]) {
262+
q.unshift([x, y]);
263+
} else {
264+
q.push([x, y]);
265+
}
266+
}
267+
}
268+
}
269+
}
72270
```
73271

74272
### **...**

lcp/LCP 56. 信物传送/Solution.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
int conveyorBelt(vector<string>& matrix, vector<int>& start, vector<int>& end) {
4+
int dirs[5] = {-1, 0, 1, 0, -1};
5+
unordered_map<char, int> d;
6+
d['^'] = 0;
7+
d['>'] = 1;
8+
d['v'] = 2;
9+
d['<'] = 3;
10+
deque<pair<int, int>> q;
11+
q.emplace_back(start[0], start[1]);
12+
int m = matrix.size(), n = matrix[0].size();
13+
int dist[m][n];
14+
memset(dist, 0x3f, sizeof(dist));
15+
dist[start[0]][start[1]] = 0;
16+
while (1) {
17+
auto [i, j] = q.front();
18+
q.pop_front();
19+
if (i == end[0] && j == end[1]) {
20+
return dist[i][j];
21+
}
22+
for (int k = 0; k < 4; ++k) {
23+
int x = i + dirs[k], y = j + dirs[k + 1];
24+
int t = dist[i][j] + (k == d[matrix[i][j]] ? 0 : 1);
25+
if (x >= 0 && x < m && y >= 0 && y < n && t < dist[x][y]) {
26+
dist[x][y] = t;
27+
if (dist[x][y] == dist[i][j]) {
28+
q.emplace_front(x, y);
29+
} else {
30+
q.emplace_back(x, y);
31+
}
32+
}
33+
}
34+
}
35+
}
36+
};

0 commit comments

Comments
 (0)