Skip to content

Commit 642e5ac

Browse files
committed
2332 solved.
1 parent 4d5d587 commit 642e5ac

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_2332)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_2332 main.cpp)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/// Source : https://leetcode.com/problems/the-latest-time-to-catch-a-bus/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-12
4+
5+
#include <iostream>
6+
#include <vector>
7+
#include <algorithm>
8+
9+
using namespace std;
10+
11+
12+
/// Simulation
13+
/// Time Complexity: O(n + m)
14+
/// Space Complexity: O(n)
15+
class Solution {
16+
public:
17+
int latestTimeCatchTheBus(vector<int>& buses, vector<int>& passengers, int capacity) {
18+
19+
sort(buses.begin(), buses.end());
20+
sort(passengers.begin(), passengers.end());
21+
22+
vector<int> pre(passengers.size());
23+
for(int start = 0, i = 1; i <= passengers.size(); i ++)
24+
if(i == passengers.size() || passengers[i] != passengers[start] + (i - start)){
25+
for(int j = start; j < i; j ++) pre[j] = passengers[start] - 1;
26+
start = i;
27+
}
28+
29+
int res = 1;
30+
int pi = 0;
31+
for(int bi = 0; bi < buses.size(); bi ++){
32+
vector<int> v;
33+
for(; pi < passengers.size() && passengers[pi] <= buses[bi] && v.size() < capacity; pi ++)
34+
v.push_back(pi);
35+
36+
for(int i: v)
37+
res = max(res, pre[i]);
38+
39+
if(v.size() < capacity){
40+
if(v.empty() || passengers[v.back()] != buses[bi])
41+
res = max(res, buses[bi]);
42+
}
43+
}
44+
return res;
45+
}
46+
};
47+
48+
49+
int main() {
50+
51+
vector<int> buses1 = {10, 20}, passengers1 = {2, 17, 18, 19};
52+
cout << Solution().latestTimeCatchTheBus(buses1, passengers1, 2) << '\n';
53+
// 16
54+
55+
vector<int> buses2 = {20, 30, 10}, passengers2 = {19, 13, 26, 4, 25, 11, 21};
56+
cout << Solution().latestTimeCatchTheBus(buses2, passengers2, 2) << '\n';
57+
// 20
58+
59+
vector<int> buses3 = {2}, passengers3 = {2};
60+
cout << Solution().latestTimeCatchTheBus(buses3, passengers3, 2) << '\n';
61+
// 1
62+
63+
vector<int> buses4 = {5, 15}, passengers4 = {11, 12, 13, 14, 15};
64+
cout << Solution().latestTimeCatchTheBus(buses4, passengers4, 2) << '\n';
65+
// 10
66+
67+
vector<int> buses5 = {7,12,15,11,14,13,5,4,2,8,9,19}, passengers5 = {2,5,10,12,8,19,9,14,4,7,15,13};
68+
cout << Solution().latestTimeCatchTheBus(buses5, passengers5, 2) << '\n';
69+
// 18
70+
71+
return 0;
72+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
21792179
| 2329 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - |
21802180
| 2330 | [Valid Palindrome IV](https://leetcode.com/problems/valid-palindrome-iv/) | [] | [C++](2001-2500/2330-Valid-Palindrome-IV/cpp-2330/) | | |
21812181
| 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree/) | [] | [C++](2001-2500/2331-Evaluate-Boolean-Binary-Tree/cpp-2331/) | | |
2182-
| | | | | | |
2182+
| 2332 | [The Latest Time to Catch a Bus](https://leetcode.com/problems/the-latest-time-to-catch-a-bus/) | [] | [C++](2001-2500/2332-The-Latest-Time-to-Catch-a-Bus/cpp-2332/) | | |
21832183
| 2333 | [Minimum Sum of Squared Difference](https://leetcode.com/problems/minimum-sum-of-squared-difference/) | [] | [C++](2001-2500/2333-Minimum-Sum-of-Squared-Difference/cpp-2333/) | | |
21842184
| 2334 | [Subarray With Elements Greater Than Varying Threshold](https://leetcode.com/problems/subarray-with-elements-greater-than-varying-threshold/) | [] | [C++](2001-2500/2334-Subarray-With-Elements-Greater-Than-Varying-Threshold/cpp-2334/) | | |
21852185
| 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/) | [] | [C++](2001-2500/2335-Minimum-Amount-of-Time-to-Fill-Cups/cpp-2335/) | | |

0 commit comments

Comments
 (0)