Skip to content

Commit c6e2072

Browse files
authored
feat: add solutions to lc problem: No.0853 (#1445)
No.0853.Car Fleet close #1443
1 parent 3d725b4 commit c6e2072

File tree

7 files changed

+291
-21
lines changed

7 files changed

+291
-21
lines changed

solution/0800-0899/0853.Car Fleet/README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,128 @@
6666

6767
<!-- 这里可写通用的实现逻辑 -->
6868

69+
**方法一:排序**
70+
71+
我们将车辆按照位置降序排序,这样我们只需要比较相邻两辆车的到达时间即可。
72+
73+
我们初始化一个变量 $pre$ 表示上一辆车到达终点的时间,如果当前车辆到达终点的时间大于 $pre$,说明当前车辆无法追上前面的车辆,因此需要另外开一个车队,否则当前车辆会与前面的车辆组成一个车队。
74+
75+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是车辆的数量。
76+
6977
<!-- tabs:start -->
7078

7179
### **Python3**
7280

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

7583
```python
76-
84+
class Solution:
85+
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
86+
idx = sorted(range(len(position)), key=lambda i: position[i])
87+
ans = pre = 0
88+
for i in idx[::-1]:
89+
t = (target - position[i]) / speed[i]
90+
if t > pre:
91+
ans += 1
92+
pre = t
93+
return ans
7794
```
7895

7996
### **Java**
8097

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

83100
```java
101+
class Solution {
102+
public int carFleet(int target, int[] position, int[] speed) {
103+
int n = position.length;
104+
Integer[] idx = new Integer[n];
105+
for (int i = 0; i < n; ++i) {
106+
idx[i] = i;
107+
}
108+
Arrays.sort(idx, (i, j) -> position[j] - position[i]);
109+
int ans = 0;
110+
double pre = 0;
111+
for (int i : idx) {
112+
double t = 1.0 * (target - position[i]) / speed[i];
113+
if (t > pre) {
114+
++ans;
115+
pre = t;
116+
}
117+
}
118+
return ans;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
int carFleet(int target, vector<int>& position, vector<int>& speed) {
129+
int n = position.size();
130+
vector<int> idx(n);
131+
iota(idx.begin(), idx.end(), 0);
132+
sort(idx.begin(), idx.end(), [&](int i, int j) {
133+
return position[i] > position[j];
134+
});
135+
int ans = 0;
136+
double pre = 0;
137+
for (int i : idx) {
138+
double t = 1.0 * (target - position[i]) / speed[i];
139+
if (t > pre) {
140+
++ans;
141+
pre = t;
142+
}
143+
}
144+
return ans;
145+
}
146+
};
147+
```
148+
149+
### **Go**
150+
151+
```go
152+
func carFleet(target int, position []int, speed []int) (ans int) {
153+
n := len(position)
154+
idx := make([]int, n)
155+
for i := range idx {
156+
idx[i] = i
157+
}
158+
sort.Slice(idx, func(i, j int) bool { return position[idx[j]] < position[idx[i]] })
159+
var pre float64
160+
for _, i := range idx {
161+
t := float64(target-position[i]) / float64(speed[i])
162+
if t > pre {
163+
ans++
164+
pre = t
165+
}
166+
}
167+
return
168+
}
169+
```
84170

171+
### **TypeScript**
172+
173+
```ts
174+
function carFleet(target: number, position: number[], speed: number[]): number {
175+
const n = position.length;
176+
const idx = Array(n)
177+
.fill(0)
178+
.map((_, i) => i)
179+
.sort((i, j) => position[j] - position[i]);
180+
let ans = 0;
181+
let pre = 0;
182+
for (const i of idx) {
183+
const t = (target - position[i]) / speed[i];
184+
if (t > pre) {
185+
++ans;
186+
pre = t;
187+
}
188+
}
189+
return ans;
190+
}
85191
```
86192

87193
### **...**

solution/0800-0899/0853.Car Fleet/README_EN.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,111 @@ Then, the fleet (speed 2) and the car starting at 4 (speed 1) become one fleet,
6666
### **Python3**
6767

6868
```python
69-
69+
class Solution:
70+
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
71+
idx = sorted(range(len(position)), key=lambda i: position[i])
72+
ans = pre = 0
73+
for i in idx[::-1]:
74+
t = (target - position[i]) / speed[i]
75+
if t > pre:
76+
ans += 1
77+
pre = t
78+
return ans
7079
```
7180

7281
### **Java**
7382

7483
```java
84+
class Solution {
85+
public int carFleet(int target, int[] position, int[] speed) {
86+
int n = position.length;
87+
Integer[] idx = new Integer[n];
88+
for (int i = 0; i < n; ++i) {
89+
idx[i] = i;
90+
}
91+
Arrays.sort(idx, (i, j) -> position[j] - position[i]);
92+
int ans = 0;
93+
double pre = 0;
94+
for (int i : idx) {
95+
double t = 1.0 * (target - position[i]) / speed[i];
96+
if (t > pre) {
97+
++ans;
98+
pre = t;
99+
}
100+
}
101+
return ans;
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int carFleet(int target, vector<int>& position, vector<int>& speed) {
112+
int n = position.size();
113+
vector<int> idx(n);
114+
iota(idx.begin(), idx.end(), 0);
115+
sort(idx.begin(), idx.end(), [&](int i, int j) {
116+
return position[i] > position[j];
117+
});
118+
int ans = 0;
119+
double pre = 0;
120+
for (int i : idx) {
121+
double t = 1.0 * (target - position[i]) / speed[i];
122+
if (t > pre) {
123+
++ans;
124+
pre = t;
125+
}
126+
}
127+
return ans;
128+
}
129+
};
130+
```
131+
132+
### **Go**
133+
134+
```go
135+
func carFleet(target int, position []int, speed []int) (ans int) {
136+
n := len(position)
137+
idx := make([]int, n)
138+
for i := range idx {
139+
idx[i] = i
140+
}
141+
sort.Slice(idx, func(i, j int) bool { return position[idx[j]] < position[idx[i]] })
142+
var pre float64
143+
for _, i := range idx {
144+
t := float64(target-position[i]) / float64(speed[i])
145+
if t > pre {
146+
ans++
147+
pre = t
148+
}
149+
}
150+
return
151+
}
152+
```
75153

154+
### **TypeScript**
155+
156+
```ts
157+
function carFleet(target: number, position: number[], speed: number[]): number {
158+
const n = position.length;
159+
const idx = Array(n)
160+
.fill(0)
161+
.map((_, i) => i)
162+
.sort((i, j) => position[j] - position[i]);
163+
let ans = 0;
164+
let pre = 0;
165+
for (const i of idx) {
166+
const t = (target - position[i]) / speed[i];
167+
if (t > pre) {
168+
++ans;
169+
pre = t;
170+
}
171+
}
172+
return ans;
173+
}
76174
```
77175

78176
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int carFleet(int target, vector<int>& position, vector<int>& speed) {
4+
int n = position.size();
5+
vector<int> idx(n);
6+
iota(idx.begin(), idx.end(), 0);
7+
sort(idx.begin(), idx.end(), [&](int i, int j) {
8+
return position[i] > position[j];
9+
});
10+
int ans = 0;
11+
double pre = 0;
12+
for (int i : idx) {
13+
double t = 1.0 * (target - position[i]) / speed[i];
14+
if (t > pre) {
15+
++ans;
16+
pre = t;
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func carFleet(target int, position []int, speed []int) (ans int) {
2+
n := len(position)
3+
idx := make([]int, n)
4+
for i := range idx {
5+
idx[i] = i
6+
}
7+
sort.Slice(idx, func(i, j int) bool { return position[idx[j]] < position[idx[i]] })
8+
var pre float64
9+
for _, i := range idx {
10+
t := float64(target-position[i]) / float64(speed[i])
11+
if t > pre {
12+
ans++
13+
pre = t
14+
}
15+
}
16+
return
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int carFleet(int target, int[] position, int[] speed) {
3+
int n = position.length;
4+
Integer[] idx = new Integer[n];
5+
for (int i = 0; i < n; ++i) {
6+
idx[i] = i;
7+
}
8+
Arrays.sort(idx, (i, j) -> position[j] - position[i]);
9+
int ans = 0;
10+
double pre = 0;
11+
for (int i : idx) {
12+
double t = 1.0 * (target - position[i]) / speed[i];
13+
if (t > pre) {
14+
++ans;
15+
pre = t;
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
1-
class Solution:
2-
def carFleet(self, target, position, speed):
3-
"""
4-
:type target: int
5-
:type position: List[int]
6-
:type speed: List[int]
7-
:rtype: int
8-
"""
9-
car = [(pos, spe) for pos, spe in zip(position, speed)]
10-
car.sort(reverse=True)
11-
time = [(target - pos) / spe for pos, spe in car]
12-
ls = []
13-
for i in time:
14-
if not ls:
15-
ls.append(i)
16-
else:
17-
if i > ls[-1]:
18-
ls.append(i)
19-
return len(ls)
1+
class Solution:
2+
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
3+
idx = sorted(range(len(position)), key=lambda i: position[i])
4+
ans = pre = 0
5+
for i in idx[::-1]:
6+
t = (target - position[i]) / speed[i]
7+
if t > pre:
8+
ans += 1
9+
pre = t
10+
return ans
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function carFleet(target: number, position: number[], speed: number[]): number {
2+
const n = position.length;
3+
const idx = Array(n)
4+
.fill(0)
5+
.map((_, i) => i)
6+
.sort((i, j) => position[j] - position[i]);
7+
let ans = 0;
8+
let pre = 0;
9+
for (const i of idx) {
10+
const t = (target - position[i]) / speed[i];
11+
if (t > pre) {
12+
++ans;
13+
pre = t;
14+
}
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
 (0)