Skip to content

Commit aaa93ac

Browse files
authored
GH-169: Solve problem leetcode 2589 (#184)
1 parent 8841d69 commit aaa93ac

File tree

15 files changed

+462
-1
lines changed

15 files changed

+462
-1
lines changed

collections/leetcode/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2855,7 +2855,7 @@
28552855
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode2586'>2586</a></td>
28562856
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode2587'>2587</a></td>
28572857
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode2588'>2588</a></td>
2858-
<td>2589</td>
2858+
<td>🔴&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode2589'>2589</a></td>
28592859
</table>
28602860

28612861
## Similar Repositories

collections/leetcode/data.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ problems:
4444
- name: 2588
4545
languages: cpp
4646
level: medium
47+
- name: 2589
48+
languages: cpp
49+
level: hard

problems/leetcode2589/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
solution
2+
*.dSYM

problems/leetcode2589/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Leetcode Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run cpp/main:solution < data/1.in
9+
```
10+
11+
Test program
12+
13+
```
14+
# Run all tests
15+
bazel test --test_output=all tests:solution_test
16+
bazel test --test_output=all --cache_test_results=no tests:solution_test
17+
# Run test with pecific test id
18+
bazel test --test_output=all tests:solution_test --test_arg=1
19+
```

problems/leetcode2589/cpp/main/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "solution",
5+
srcs = ["solution.cpp", "solution.h"],
6+
visibility = ["//visibility:public"],
7+
)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "solution.h"
2+
3+
#include <algorithm>
4+
#include <cmath>
5+
#include <iostream>
6+
#include <map>
7+
#include <string>
8+
#include <vector>
9+
#include <fstream>
10+
11+
using namespace std;
12+
13+
int main() {
14+
ios::sync_with_stdio(false);
15+
cin.tie(0);
16+
17+
bool is_use_file = false;
18+
19+
filesystem::path filepath =
20+
filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
21+
ifstream file(filepath);
22+
23+
if (is_use_file) {
24+
cin.rdbuf(file.rdbuf()); // redirect cin to file
25+
}
26+
27+
// get input
28+
int n;
29+
cin >> n;
30+
31+
vector<vector<int>> tasks(n, vector<int>(3));
32+
for (int i = 0; i < n; i++) {
33+
cin >> tasks[i][0] >> tasks[i][1] >> tasks[i][2];
34+
}
35+
36+
Solution solution;
37+
int output = solution.findMinimumTime(tasks);
38+
39+
// print output
40+
cout << output << endl;
41+
42+
return 0;
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <map>
4+
#include <queue>
5+
#include <set>
6+
#include <stack>
7+
#include <string>
8+
#include <unordered_map>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
class Solution {
14+
public:
15+
int findMinimumTime(vector<vector<int>>& tasks) {
16+
vector<bool> used(2001, false);
17+
sort(begin(tasks), end(tasks),
18+
[](vector<int> a, vector<int> b) { return a[1] < b[1]; });
19+
int total_time = 0;
20+
for (auto task : tasks) {
21+
int s = task[0], e = task[1], d = task[2];
22+
for (int i = s; i <= e; i++) {
23+
if (used[i]) {
24+
d--;
25+
}
26+
}
27+
28+
while (d > 0) {
29+
if (!used[e]) {
30+
total_time++;
31+
d--;
32+
used[e] = true;
33+
}
34+
e--;
35+
}
36+
}
37+
38+
return total_time;
39+
}
40+
};
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Minimum Time to Complete All Tasks\n",
8+
"\n",
9+
"**Key Ideas**\n",
10+
"\n",
11+
"* Sort the tasks by their end time\n",
12+
"* Use boolean array to mark the time is used or not\n",
13+
"* For each task\n",
14+
" * In time range [start time, end time]\n",
15+
" * If the time is used, duration - 1\n",
16+
" * Finish left duration from end time"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 18,
22+
"metadata": {
23+
"vscode": {
24+
"languageId": "cpp"
25+
}
26+
},
27+
"outputs": [],
28+
"source": [
29+
"#include <vector>\n",
30+
"#include <iostream>\n",
31+
"#include <string>"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 19,
37+
"metadata": {
38+
"vscode": {
39+
"languageId": "cpp"
40+
}
41+
},
42+
"outputs": [],
43+
"source": [
44+
"using namespace std;"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"metadata": {
51+
"vscode": {
52+
"languageId": "cpp"
53+
}
54+
},
55+
"outputs": [],
56+
"source": [
57+
"int x;\n",
58+
"cin >> x;"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 29,
64+
"metadata": {
65+
"vscode": {
66+
"languageId": "cpp"
67+
}
68+
},
69+
"outputs": [],
70+
"source": [
71+
"auto parse_inputs(string s){\n",
72+
" vector<vector<int>> inputs;\n",
73+
" vector<int> input;\n",
74+
" string value = \"\";\n",
75+
" for (int i = 0; i < s.size(); i++) {\n",
76+
" if (s[i] == '[') {\n",
77+
" input.clear();\n",
78+
" } else if (s[i] == ']') {\n",
79+
" if (value != \"\") {\n",
80+
" input.push_back(stoi(value));\n",
81+
" value = \"\";\n",
82+
" }\n",
83+
" inputs.push_back(input);\n",
84+
" } else if (s[i] == ',') {\n",
85+
" if (value != \"\") {\n",
86+
" input.push_back(stoi(value));\n",
87+
" value = \"\";\n",
88+
" }\n",
89+
" } else {\n",
90+
" value += s[i];\n",
91+
" }\n",
92+
" }\n",
93+
" // get rid of the last input\n",
94+
" inputs.pop_back();\n",
95+
" return inputs;\n",
96+
"}"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 32,
102+
"metadata": {
103+
"vscode": {
104+
"languageId": "cpp"
105+
}
106+
},
107+
"outputs": [],
108+
"source": [
109+
"auto tasks = parse_inputs(\"[[2,3,1],[4,5,1],[1,5,2]]\");"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 33,
115+
"metadata": {
116+
"vscode": {
117+
"languageId": "cpp"
118+
}
119+
},
120+
"outputs": [
121+
{
122+
"data": {
123+
"text/plain": [
124+
"{ { 2, 3, 1 }, { 4, 5, 1 }, { 1, 5, 2 } }"
125+
]
126+
},
127+
"execution_count": 33,
128+
"metadata": {},
129+
"output_type": "execute_result"
130+
}
131+
],
132+
"source": [
133+
"tasks"
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": 36,
139+
"metadata": {
140+
"vscode": {
141+
"languageId": "cpp"
142+
}
143+
},
144+
"outputs": [],
145+
"source": [
146+
"sort(begin(tasks), end(tasks), [](vector<int> a, vector<int> b) {\n",
147+
" return a[1] < b[1];\n",
148+
"});"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 37,
154+
"metadata": {
155+
"vscode": {
156+
"languageId": "cpp"
157+
}
158+
},
159+
"outputs": [
160+
{
161+
"data": {
162+
"text/plain": [
163+
"{ { 2, 3, 1 }, { 4, 5, 1 }, { 1, 5, 2 } }"
164+
]
165+
},
166+
"execution_count": 37,
167+
"metadata": {},
168+
"output_type": "execute_result"
169+
}
170+
],
171+
"source": [
172+
"tasks"
173+
]
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": 38,
178+
"metadata": {
179+
"vscode": {
180+
"languageId": "cpp"
181+
}
182+
},
183+
"outputs": [],
184+
"source": [
185+
"vector<int> v = {1, 2, 3};"
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 41,
191+
"metadata": {
192+
"vscode": {
193+
"languageId": "cpp"
194+
}
195+
},
196+
"outputs": [],
197+
"source": [
198+
"std::vector<int> v = {1, 2, 3};\n",
199+
"int a = v[0], b = v[1], c = v[2];"
200+
]
201+
}
202+
],
203+
"metadata": {
204+
"kernelspec": {
205+
"display_name": "C++17",
206+
"language": "C++17",
207+
"name": "xcpp17"
208+
},
209+
"language_info": {
210+
"codemirror_mode": "text/x-c++src",
211+
"file_extension": ".cpp",
212+
"mimetype": "text/x-c++src",
213+
"name": "c++",
214+
"version": "17"
215+
},
216+
"orig_nbformat": 4
217+
},
218+
"nbformat": 4,
219+
"nbformat_minor": 2
220+
}

problems/leetcode2589/cpp/tests/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
2+
3+
cc_test(
4+
name = "solution_test",
5+
srcs = ["solution_test.cpp"],
6+
deps = [
7+
"//problems/codeforcesAA/src/main:solution",
8+
"@com_google_googletest//:gtest_main",
9+
],
10+
data = ["data"]
11+
)

0 commit comments

Comments
 (0)