diff --git a/solution/0800-0899/0853.Car Fleet/README.md b/solution/0800-0899/0853.Car Fleet/README.md index a97070df53df2..72a06e3802c88 100644 --- a/solution/0800-0899/0853.Car Fleet/README.md +++ b/solution/0800-0899/0853.Car Fleet/README.md @@ -66,6 +66,14 @@ +**方法一:排序** + +我们将车辆按照位置降序排序,这样我们只需要比较相邻两辆车的到达时间即可。 + +我们初始化一个变量 $pre$ 表示上一辆车到达终点的时间,如果当前车辆到达终点的时间大于 $pre$,说明当前车辆无法追上前面的车辆,因此需要另外开一个车队,否则当前车辆会与前面的车辆组成一个车队。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是车辆的数量。 + ### **Python3** @@ -73,7 +81,16 @@ ```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** @@ -81,7 +98,96 @@ ```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& position, vector& speed) { + int n = position.size(); + vector 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; +} ``` ### **...** diff --git a/solution/0800-0899/0853.Car Fleet/README_EN.md b/solution/0800-0899/0853.Car Fleet/README_EN.md index a90589f9fff38..fa705811cfa78 100644 --- a/solution/0800-0899/0853.Car Fleet/README_EN.md +++ b/solution/0800-0899/0853.Car Fleet/README_EN.md @@ -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& position, vector& speed) { + int n = position.size(); + vector 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; +} ``` ### **...** diff --git a/solution/0800-0899/0853.Car Fleet/Solution.cpp b/solution/0800-0899/0853.Car Fleet/Solution.cpp new file mode 100644 index 0000000000000..607bf3dcecfc5 --- /dev/null +++ b/solution/0800-0899/0853.Car Fleet/Solution.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int carFleet(int target, vector& position, vector& speed) { + int n = position.size(); + vector 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; + } +}; \ No newline at end of file diff --git a/solution/0800-0899/0853.Car Fleet/Solution.go b/solution/0800-0899/0853.Car Fleet/Solution.go new file mode 100644 index 0000000000000..1a83b7f63b4a5 --- /dev/null +++ b/solution/0800-0899/0853.Car Fleet/Solution.go @@ -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 +} \ No newline at end of file diff --git a/solution/0800-0899/0853.Car Fleet/Solution.java b/solution/0800-0899/0853.Car Fleet/Solution.java new file mode 100644 index 0000000000000..c271f38350b6b --- /dev/null +++ b/solution/0800-0899/0853.Car Fleet/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/0800-0899/0853.Car Fleet/Solution.py b/solution/0800-0899/0853.Car Fleet/Solution.py index 20cbe217155a2..fb910f530d22b 100644 --- a/solution/0800-0899/0853.Car Fleet/Solution.py +++ b/solution/0800-0899/0853.Car Fleet/Solution.py @@ -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 diff --git a/solution/0800-0899/0853.Car Fleet/Solution.ts b/solution/0800-0899/0853.Car Fleet/Solution.ts new file mode 100644 index 0000000000000..0e079109ab61e --- /dev/null +++ b/solution/0800-0899/0853.Car Fleet/Solution.ts @@ -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; +}