Skip to content

Commit 002fc3a

Browse files
committed
1776
1 parent ec76b7d commit 002fc3a

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

leetcode/1776/a.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[[1,2],[2,1],[4,3],[7,2]]

leetcode/1776/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
#include "../../lib/LeetCodeInput.hpp"
7+
#include "solution.cpp"
8+
using namespace std;
9+
10+
int main()
11+
{
12+
LeetCodeInput li("a.in");
13+
14+
vector<vector<int>> mat_1 = li.get_vvi(0);
15+
// vector<int> arr_1 = li.get_vi(0);
16+
// int num_1 = li.get_i(0);
17+
// string s_1 = li.get_s(0);
18+
19+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
20+
Solution solution;
21+
auto output = solution.getCollisionTimes(mat_1);
22+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
23+
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds> (end - begin).count() << "µs" << endl;
24+
25+
// cout << output << endl;
26+
27+
for (int i: output){
28+
cout << i << ' ';
29+
}
30+
31+
// for (auto i: output){
32+
// for (int j: i){
33+
// cout << j << ' ';
34+
// }
35+
// cout << '\n';
36+
// }
37+
38+
return 0;
39+
}

leetcode/1776/solution.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
3+
time: O(n2)
4+
space: O(n)
5+
6+
Runtime: 488 ms, faster than 100.00% of C++ online submissions for Car Fleet II.
7+
Memory Usage: 129.9 MB, less than 25.36% of C++ online submissions for Car Fleet II.
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
#define all(x) begin(x), end(x)
15+
typedef vector<int> vi;
16+
typedef vector<vector<int>> vvi;
17+
18+
class Solution {
19+
public:
20+
vector<double> getCollisionTimes(vector<vector<int>>& cars) {
21+
/// car = { b, k }
22+
int n = size(cars);
23+
24+
vector<double> res(n);
25+
/// envelop k and b
26+
int k[100000];
27+
double b[100000];
28+
/// collision position of car ? and car i
29+
double x[100000];
30+
31+
int env_size = 1;
32+
res[n-1] = -1;
33+
k[0] = cars[n-1][1];
34+
b[0] = cars[n-1][0];
35+
36+
int j;
37+
int k_i;
38+
double b_i;
39+
for (int i=n-2; i>-1; --i){
40+
k_i = cars[i][1];
41+
b_i = cars[i][0];
42+
for (j = 0; j<env_size; ++j){
43+
/// will never caught
44+
if (k_i <= k[j]) break;
45+
/// y = k1 x + b1
46+
/// y = k2 x + b2
47+
/// x = (b2 - b1)/(k1 - k2)
48+
x[j] = (b[j] - b_i) / (k_i - k[j]);
49+
}
50+
if (j) {
51+
/// use env_size to temp store the min element pos
52+
env_size = min_element(x, x+j) - x;
53+
res[i] = x[env_size];
54+
/// the actual replacement happens after min element
55+
++env_size;
56+
} else {
57+
env_size = 0;
58+
res[i] = -1;
59+
}
60+
k[env_size] = k_i;
61+
b[env_size] = b_i;
62+
/// the actual env_size
63+
++env_size;
64+
}
65+
66+
return res;
67+
}
68+
};
69+
70+
const static auto initialize = [] {
71+
std::ios::sync_with_stdio(false);
72+
std::cin.tie(nullptr);
73+
std::cout.tie(nullptr);
74+
return nullptr;
75+
}();

0 commit comments

Comments
 (0)