Skip to content

feat: add solutions to lc problem: No.0853 #1445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 107 additions & 1 deletion solution/0800-0899/0853.Car Fleet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,128 @@

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

**方法一:排序**

我们将车辆按照位置降序排序,这样我们只需要比较相邻两辆车的到达时间即可。

我们初始化一个变量 $pre$ 表示上一辆车到达终点的时间,如果当前车辆到达终点的时间大于 $pre$,说明当前车辆无法追上前面的车辆,因此需要另外开一个车队,否则当前车辆会与前面的车辆组成一个车队。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是车辆的数量。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
idx = sorted(range(len(position)), key=lambda i: position[i])
ans = pre = 0
for i in idx[::-1]:
t = (target - position[i]) / speed[i]
if t > pre:
ans += 1
pre = t
return ans
```

### **Java**

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

```java
class Solution {
public int carFleet(int target, int[] position, int[] speed) {
int n = position.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
Arrays.sort(idx, (i, j) -> position[j] - position[i]);
int ans = 0;
double pre = 0;
for (int i : idx) {
double t = 1.0 * (target - position[i]) / speed[i];
if (t > pre) {
++ans;
pre = t;
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int carFleet(int target, vector<int>& position, vector<int>& speed) {
int n = position.size();
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int i, int j) {
return position[i] > position[j];
});
int ans = 0;
double pre = 0;
for (int i : idx) {
double t = 1.0 * (target - position[i]) / speed[i];
if (t > pre) {
++ans;
pre = t;
}
}
return ans;
}
};
```

### **Go**

```go
func carFleet(target int, position []int, speed []int) (ans int) {
n := len(position)
idx := make([]int, n)
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(i, j int) bool { return position[idx[j]] < position[idx[i]] })
var pre float64
for _, i := range idx {
t := float64(target-position[i]) / float64(speed[i])
if t > pre {
ans++
pre = t
}
}
return
}
```

### **TypeScript**

```ts
function carFleet(target: number, position: number[], speed: number[]): number {
const n = position.length;
const idx = Array(n)
.fill(0)
.map((_, i) => i)
.sort((i, j) => position[j] - position[i]);
let ans = 0;
let pre = 0;
for (const i of idx) {
const t = (target - position[i]) / speed[i];
if (t > pre) {
++ans;
pre = t;
}
}
return ans;
}
```

### **...**
Expand Down
100 changes: 99 additions & 1 deletion solution/0800-0899/0853.Car Fleet/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,111 @@ Then, the fleet (speed 2) and the car starting at 4 (speed 1) become one fleet,
### **Python3**

```python

class Solution:
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
idx = sorted(range(len(position)), key=lambda i: position[i])
ans = pre = 0
for i in idx[::-1]:
t = (target - position[i]) / speed[i]
if t > pre:
ans += 1
pre = t
return ans
```

### **Java**

```java
class Solution {
public int carFleet(int target, int[] position, int[] speed) {
int n = position.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
Arrays.sort(idx, (i, j) -> position[j] - position[i]);
int ans = 0;
double pre = 0;
for (int i : idx) {
double t = 1.0 * (target - position[i]) / speed[i];
if (t > pre) {
++ans;
pre = t;
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int carFleet(int target, vector<int>& position, vector<int>& speed) {
int n = position.size();
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int i, int j) {
return position[i] > position[j];
});
int ans = 0;
double pre = 0;
for (int i : idx) {
double t = 1.0 * (target - position[i]) / speed[i];
if (t > pre) {
++ans;
pre = t;
}
}
return ans;
}
};
```

### **Go**

```go
func carFleet(target int, position []int, speed []int) (ans int) {
n := len(position)
idx := make([]int, n)
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(i, j int) bool { return position[idx[j]] < position[idx[i]] })
var pre float64
for _, i := range idx {
t := float64(target-position[i]) / float64(speed[i])
if t > pre {
ans++
pre = t
}
}
return
}
```

### **TypeScript**

```ts
function carFleet(target: number, position: number[], speed: number[]): number {
const n = position.length;
const idx = Array(n)
.fill(0)
.map((_, i) => i)
.sort((i, j) => position[j] - position[i]);
let ans = 0;
let pre = 0;
for (const i of idx) {
const t = (target - position[i]) / speed[i];
if (t > pre) {
++ans;
pre = t;
}
}
return ans;
}
```

### **...**
Expand Down
21 changes: 21 additions & 0 deletions solution/0800-0899/0853.Car Fleet/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int carFleet(int target, vector<int>& position, vector<int>& speed) {
int n = position.size();
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int i, int j) {
return position[i] > position[j];
});
int ans = 0;
double pre = 0;
for (int i : idx) {
double t = 1.0 * (target - position[i]) / speed[i];
if (t > pre) {
++ans;
pre = t;
}
}
return ans;
}
};
17 changes: 17 additions & 0 deletions solution/0800-0899/0853.Car Fleet/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
func carFleet(target int, position []int, speed []int) (ans int) {
n := len(position)
idx := make([]int, n)
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(i, j int) bool { return position[idx[j]] < position[idx[i]] })
var pre float64
for _, i := range idx {
t := float64(target-position[i]) / float64(speed[i])
if t > pre {
ans++
pre = t
}
}
return
}
20 changes: 20 additions & 0 deletions solution/0800-0899/0853.Car Fleet/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int carFleet(int target, int[] position, int[] speed) {
int n = position.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
Arrays.sort(idx, (i, j) -> position[j] - position[i]);
int ans = 0;
double pre = 0;
for (int i : idx) {
double t = 1.0 * (target - position[i]) / speed[i];
if (t > pre) {
++ans;
pre = t;
}
}
return ans;
}
}
29 changes: 10 additions & 19 deletions solution/0800-0899/0853.Car Fleet/Solution.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
class Solution:
def carFleet(self, target, position, speed):
"""
:type target: int
:type position: List[int]
:type speed: List[int]
:rtype: int
"""
car = [(pos, spe) for pos, spe in zip(position, speed)]
car.sort(reverse=True)
time = [(target - pos) / spe for pos, spe in car]
ls = []
for i in time:
if not ls:
ls.append(i)
else:
if i > ls[-1]:
ls.append(i)
return len(ls)
class Solution:
def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
idx = sorted(range(len(position)), key=lambda i: position[i])
ans = pre = 0
for i in idx[::-1]:
t = (target - position[i]) / speed[i]
if t > pre:
ans += 1
pre = t
return ans
17 changes: 17 additions & 0 deletions solution/0800-0899/0853.Car Fleet/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function carFleet(target: number, position: number[], speed: number[]): number {
const n = position.length;
const idx = Array(n)
.fill(0)
.map((_, i) => i)
.sort((i, j) => position[j] - position[i]);
let ans = 0;
let pre = 0;
for (const i of idx) {
const t = (target - position[i]) / speed[i];
if (t > pre) {
++ans;
pre = t;
}
}
return ans;
}