Skip to content

Commit 51caba4

Browse files
authored
GH-169: Solve leetcode 2360, 2492, 1319, 1466, 2316 (#276)
1 parent 0d12776 commit 51caba4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1709
-6
lines changed

collections/leetcode/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<td>32</td>
4747
<td>33</td>
4848
<td>34</td>
49-
<td>35</td>
49+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode35'>35</a></td>
5050
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode36'>36</a></td>
5151
<td>37</td>
5252
<td>38</td>
@@ -1458,7 +1458,7 @@
14581458
<td>1316</td>
14591459
<td>1317</td>
14601460
<td>1318</td>
1461-
<td>1319</td>
1461+
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode1319'>1319</a></td>
14621462
<td>1320</td>
14631463
<tr>
14641464
<td>1321</td>
@@ -1620,7 +1620,7 @@
16201620
<td>1463</td>
16211621
<td>1464</td>
16221622
<td>1465</td>
1623-
<td>1466</td>
1623+
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode1466'>1466</a></td>
16241624
<td>1467</td>
16251625
<td>1468</td>
16261626
<td>1469</td>
@@ -2555,7 +2555,7 @@
25552555
<td>2313</td>
25562556
<td>2314</td>
25572557
<td>2315</td>
2558-
<td>2316</td>
2558+
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode2316'>2316</a></td>
25592559
<td>2317</td>
25602560
<td>2318</td>
25612561
<td>2319</td>
@@ -2603,7 +2603,7 @@
26032603
<td>2357</td>
26042604
<td>2358</td>
26052605
<td>2359</td>
2606-
<td>2360</td>
2606+
<td>🔴&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode2360'>2360</a></td>
26072607
<tr>
26082608
<td>2361</td>
26092609
<td>2362</td>
@@ -2749,7 +2749,7 @@
27492749
<td>2490</td>
27502750
<tr>
27512751
<td>2491</td>
2752-
<td>2492</td>
2752+
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode2492'>2492</a></td>
27532753
<td>2493</td>
27542754
<td>2494</td>
27552755
<td>2495</td>

collections/leetcode/data.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ problems:
2424
- name: 27
2525
languages: python
2626
level: easy
27+
- name: 35
28+
languages: python
29+
level: easy
2730
- name: 36
2831
languages: cpp
2932
level: medium
@@ -124,15 +127,30 @@ problems:
124127
- name: 958
125128
languages: cpp
126129
level: medium
130+
- name: 1319
131+
languages: cpp
132+
level: medium
133+
- name: 1466
134+
languages: cpp
135+
level: medium
127136
- name: 1472
128137
languages: cpp
129138
level: medium
130139
- name: 1480
131140
languages: cpp
132141
level: easy
142+
- name: 2316
143+
languages: cpp
144+
level: medium
133145
- name: 2348
134146
languages: cpp
135147
level: medium
148+
- name: 2360
149+
languages: cpp
150+
level: hard
151+
- name: 2492
152+
languages: cpp
153+
level: medium
136154
- name: 2574
137155
languages: cpp
138156
level: easy

problems/leetcode1319/.gitignore

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

problems/leetcode1319/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/leetcode1319/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", "helpers.h"],
6+
visibility = ["//visibility:public"],
7+
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
#include <iostream>
6+
#include <stack>
7+
#include <queue>
8+
9+
using namespace std;
10+
11+
/* Temmplate class T should be a Nary Tree Node
12+
* See leetcode 589 (https://leetcode.com/problems/n-ary-tree-preorder-traversal/description/)
13+
* class Node {
14+
* public:
15+
* int val;
16+
* vector<Node*> children;
17+
*
18+
* Node() {}
19+
*
20+
* Node(int _val) { val = _val; }
21+
*
22+
* Node(int _val, vector<Node*> _children) {
23+
* val = _val;
24+
* children = _children;
25+
* }
26+
* };
27+
*/
28+
template <typename T> T* parse_nary_tree(vector<string> s) {
29+
T* fake = new T();
30+
T* current = fake;
31+
queue<T*> q;
32+
for(string e: s){
33+
if(e == "null"){
34+
current = q.front();
35+
q.pop();
36+
} else {
37+
T* child = new T(stoi(e));
38+
q.push(child);
39+
current->children.push_back(child);
40+
}
41+
}
42+
return fake->children[0];
43+
}
44+
45+
vector<string> parse_line(string line) {
46+
vector<string> v;
47+
string s;
48+
for (char c : line) {
49+
if (c == ' ') {
50+
v.push_back(s);
51+
s = "";
52+
} else {
53+
s += c;
54+
}
55+
}
56+
v.push_back(s);
57+
return v;
58+
}
59+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "solution.h"
2+
#include "helpers.h"
3+
4+
#include <algorithm>
5+
#include <cmath>
6+
#include <iostream>
7+
#include <map>
8+
#include <string>
9+
#include <vector>
10+
#include <fstream>
11+
#include <filesystem>
12+
13+
using namespace std;
14+
15+
int main() {
16+
ios::sync_with_stdio(false);
17+
cin.tie(0);
18+
19+
bool is_use_file = false;
20+
21+
filesystem::path filepath =
22+
filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
23+
ifstream file(filepath);
24+
25+
if (is_use_file) {
26+
cin.rdbuf(file.rdbuf()); // redirect cin to file
27+
}
28+
29+
// get input
30+
int n;
31+
cin >> n;
32+
33+
int e;
34+
cin >> e;
35+
36+
vector<vector<int>> edges(e, vector<int>(2));
37+
for (int i = 0; i < e; i++) {
38+
for (int j = 0; j < 2; j++) {
39+
cin >> edges[i][j];
40+
}
41+
}
42+
43+
Solution solution;
44+
int output = solution.makeConnected(n, edges);
45+
46+
// print output
47+
cout << output << endl;
48+
49+
return 0;
50+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <algorithm>
2+
#include <climits>
3+
#include <cmath>
4+
#include <map>
5+
#include <queue>
6+
#include <set>
7+
#include <stack>
8+
#include <string>
9+
#include <unordered_map>
10+
#include <vector>
11+
12+
using namespace std;
13+
14+
class Solution {
15+
public:
16+
int makeConnected(int n, vector<vector<int>>& connections) {
17+
int e = connections.size();
18+
if (e < n - 1) {
19+
return -1;
20+
}
21+
vector<bool> visited(n, false);
22+
vector<vector<int>> edges(n, vector<int>{});
23+
int scc = 0;
24+
for (auto connection : connections) {
25+
int x = connection[0];
26+
int y = connection[1];
27+
edges[x].push_back(y);
28+
edges[y].push_back(x);
29+
}
30+
31+
queue<int> q;
32+
for (int i = 0; i < n; i++) {
33+
// bfs
34+
if (!visited[i]) {
35+
scc++;
36+
q.push(i);
37+
while (!q.empty()) {
38+
int node = q.front();
39+
q.pop();
40+
if (visited[node]) continue;
41+
visited[node] = true;
42+
for (auto next : edges[node]) {
43+
if (!visited[next]) {
44+
q.push(next);
45+
}
46+
}
47+
}
48+
}
49+
}
50+
return scc - 1;
51+
}
52+
};

problems/leetcode1319/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)