Skip to content

Commit bc89a7f

Browse files
committed
2350
1 parent ae2b325 commit bc89a7f

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

leetcode/2350/0.in

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

leetcode/2350/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-2.cpp"
8+
using namespace std;
9+
10+
int main()
11+
{
12+
LeetCodeInput li("0.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.shortestSequence(arr_1, num_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/2350/solution-2.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
3+
time: O(n)
4+
space: O(k)
5+
6+
Runtime: 83 ms, faster than 100.00% of C++ online submissions for Shortest Impossible Sequence of Rolls.
7+
Memory Usage: 86.9 MB, less than 95.12% of C++ online submissions for Shortest Impossible Sequence of Rolls.
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+
int shortestSequence(vector<int>& rolls, int k) {
21+
int seen[k+1];
22+
int memset_bytes = 4*k+4;
23+
memset(seen, 0, memset_bytes);
24+
int cnt = 0;
25+
int res = 1;
26+
for (auto & roll : rolls){
27+
if (!seen[roll]){
28+
seen[roll] = 1;
29+
++cnt;
30+
if (cnt == k){
31+
memset(seen, 0, memset_bytes);
32+
++res;
33+
cnt = 0;
34+
}
35+
}
36+
}
37+
return res;
38+
}
39+
};
40+
41+
const static auto initialize = [] {
42+
std::ios::sync_with_stdio(false);
43+
std::cin.tie(nullptr);
44+
std::cout.tie(nullptr);
45+
return nullptr;
46+
}();

leetcode/2350/solution.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime: 218 ms, faster than 93.81% of C++ online submissions for Shortest Impossible Sequence of Rolls.
7+
Memory Usage: 113.7 MB, less than 54.58% of C++ online submissions for Shortest Impossible Sequence of Rolls.
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+
int shortestSequence(vector<int>& rolls, int k) {
21+
/// position sequence of any given number
22+
vector<vi> pos_seqs(k+1);
23+
int pos = 0;
24+
for (auto & roll : rolls){
25+
pos_seqs[roll].push_back(pos++);
26+
}
27+
28+
/// position of any given number now
29+
vector<vi::iterator> pos_now_its(k+1);
30+
int res = 1;
31+
int pos_next_min = 0;
32+
for (int i=1; i<=k; ++i){
33+
if (pos_seqs[i].size() == 0){
34+
return res;
35+
}
36+
pos_now_its[i] = pos_seqs[i].begin();
37+
pos_next_min = max(pos_next_min, pos_seqs[i][0]+1);
38+
}
39+
40+
int pos_now_min;
41+
vi::iterator it;
42+
for (res = 2;; ++res){
43+
pos_now_min = pos_next_min;
44+
for (int i=1; i<=k; ++i){
45+
it = lower_bound(pos_now_its[i], pos_seqs[i].end(), pos_now_min);
46+
if (it == pos_seqs[i].end()){
47+
return res;
48+
} else {
49+
pos_now_its[i] = it;
50+
pos_next_min = max(pos_next_min, *it+1);
51+
}
52+
}
53+
}
54+
}
55+
};
56+
57+
const static auto initialize = [] {
58+
std::ios::sync_with_stdio(false);
59+
std::cin.tie(nullptr);
60+
std::cout.tie(nullptr);
61+
return nullptr;
62+
}();

0 commit comments

Comments
 (0)