Skip to content

Commit f6a67cd

Browse files
committed
1851
1 parent 1536ebf commit f6a67cd

File tree

15 files changed

+504
-1
lines changed

15 files changed

+504
-1
lines changed

leetcode/1851/1.in

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

leetcode/1851/2.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[2,3],[2,5],[1,8],[20,25]]
2+
[2,19,5,22]

leetcode/1851/34.in

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

leetcode/1851/34x.in

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

leetcode/1851/40.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[63,93],[65,86],[7,39],[55,59],[2,45],[97,98],[51,73],[21,46],[100,100],[53,71],[31,33],[95,95],[33,67],[29,77],[49,57],[69,79],[93,93],[6,72],[93,99],[1,3],[49,87],[53,92],[41,99],[86,99],[3,41],[45,73],[55,85],[72,82],[56,70],[50,51],[31,57],[73,78],[6,46],[67,67],[84,85],[1,77],[41,41],[23,41],[43,73],[29,48],[73,91],[41,61],[74,74],[68,74],[17,88],[13,63],[31,80],[25,65],[26,32],[78,78],[9,37],[45,45],[73,98],[41,59],[24,92],[1,11],[47,69],[80,80],[97,99],[10,83],[53,59],[27,91],[80,98],[59,95],[40,50],[33,41],[61,81],[18,42],[49,77],[41,41],[53,87],[83,95],[23,56],[67,95],[65,79],[3,49],[27,61],[71,71],[50,70],[53,83],[21,87],[77,77],[1,91],[12,37],[1,50],[12,16],[39,76],[85,93],[5,70],[9,84],[65,75],[81,93],[61,93],[61,81],[51,73],[1,61],[45,69],[13,61],[41,41],[37,37]]
2+
[96,37,17,75,29,19,41,25,62,87,25,73,45,11,32,61,17,30,59,33,45,21,73,96,31,45,97,1,73,41,19,61,25,54,83,1,96,35,77,39,57,1,61,37,86,7,77,97,11,74,93,55,29,21,63,21,41,55,87,61,86,8,51,1,27,49,61,53,49,61,81,77,21,1,43,48,76,11,51,41,84,65,51,91,84,67,2,93,61,59,93,11,5,51,34,99,61,18,58,99]

leetcode/1851/42.in

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

leetcode/1851/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-4.cpp"
8+
using namespace std;
9+
10+
int main()
11+
{
12+
LeetCodeInput li("2.in");
13+
14+
vector<vector<int>> mat_1 = li.get_vvi(0);
15+
vector<int> arr_1 = li.get_vi(1);
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.minInterval(mat_1, arr_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/1851/solution-2.1.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime:
7+
Memory Usage:
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<int> minInterval(vector<vector<int>>& itvs, vector<int>& qris) {
21+
int n = size(qris);
22+
int m = size(itvs);
23+
24+
int idx_qry[n];
25+
iota(idx_qry, idx_qry+n, 0);
26+
sort(idx_qry, idx_qry+n, [&qris](int & i, int & j){return qris[i] < qris[j];});
27+
28+
sort(all(itvs));
29+
30+
/// sorted by r-l+1, distinguished by additional nonduplicate index
31+
set<long long> s;
32+
/// track the iterator of element in s with least r
33+
/// sorted by r, distinguished by additional nonduplicate index
34+
map<long long, set<long long>::iterator> it_s;
35+
36+
vi res(n);
37+
38+
int idx;
39+
int num;
40+
int cur_idx_itv = 0;
41+
vi * cur_itv = &itvs[cur_idx_itv];
42+
int l = (*cur_itv)[0];
43+
long long r = (*cur_itv)[1];
44+
for (int i=0; i<n; ++i){
45+
idx = idx_qry[i];
46+
num = qris[idx];
47+
while (!it_s.empty() && ((*it_s.begin()).first>>32) < num){
48+
s.erase((*it_s.begin()).second);
49+
it_s.erase(it_s.begin());
50+
}
51+
while (l <= num){
52+
if (r >= num){
53+
it_s.insert({(r<<32) + cur_idx_itv, s.insert(((r - l + 1)<<32) + cur_idx_itv).first});
54+
}
55+
++cur_idx_itv;
56+
if (cur_idx_itv >= m) break;
57+
cur_itv = &itvs[cur_idx_itv];
58+
l = (*cur_itv)[0];
59+
r = (*cur_itv)[1];
60+
}
61+
if (s.empty()){
62+
res[idx] = -1;
63+
} else {
64+
res[idx] = (*s.begin())>>32;
65+
}
66+
}
67+
return res;
68+
}
69+
};
70+
71+
const static auto initialize = [] {
72+
std::ios::sync_with_stdio(false);
73+
std::cin.tie(nullptr);
74+
std::cout.tie(nullptr);
75+
return nullptr;
76+
}();

leetcode/1851/solution-2.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime: 748 ms, faster than 79.22% of C++ online submissions for Minimum Interval to Include Each Query.
7+
Memory Usage: 144.6 MB, less than 20.04% of C++ online submissions for Minimum Interval to Include Each Query.
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<int> minInterval(vector<vector<int>>& itvs, vector<int>& qris) {
21+
int n = size(qris);
22+
int m = size(itvs);
23+
24+
int idx_qry[n];
25+
iota(idx_qry, idx_qry+n, 0);
26+
sort(idx_qry, idx_qry+n, [&qris](int & i, int & j){return qris[i] < qris[j];});
27+
28+
sort(all(itvs));
29+
30+
auto cmp = [](pair<int, int> a, pair<int, int> b) {
31+
if (a.first == b.first){
32+
return a.second < b.second;
33+
}
34+
return a.first < b.first;
35+
};
36+
/// sorted by r-l+1, distinguished by additional nonduplicate index
37+
set<pair<int, int>, decltype(cmp)> s(cmp);
38+
/// track the iterator of element in s with least r
39+
/// sorted by r, distinguished by additional nonduplicate index
40+
map<pair<int, int>, set<pair<int, int>, decltype(cmp)>::iterator, decltype(cmp)> it_s(cmp);
41+
42+
vi res(n);
43+
44+
int idx;
45+
int num;
46+
int cur_idx_itv = 0;
47+
vi * cur_itv = &itvs[cur_idx_itv];
48+
int l = (*cur_itv)[0];
49+
int r = (*cur_itv)[1];
50+
for (int i=0; i<n; ++i){
51+
idx = idx_qry[i];
52+
num = qris[idx];
53+
while (!it_s.empty() && (*it_s.begin()).first.first < num){
54+
s.erase((*it_s.begin()).second);
55+
it_s.erase(it_s.begin());
56+
}
57+
while (l <= num){
58+
if (r >= num){
59+
it_s.insert({{r, cur_idx_itv}, s.insert({r - l + 1, cur_idx_itv}).first});
60+
}
61+
++cur_idx_itv;
62+
if (cur_idx_itv >= m) break;
63+
cur_itv = &itvs[cur_idx_itv];
64+
l = (*cur_itv)[0];
65+
r = (*cur_itv)[1];
66+
}
67+
if (s.empty()){
68+
res[idx] = -1;
69+
} else {
70+
res[idx] = (*s.begin()).first;
71+
}
72+
}
73+
return res;
74+
}
75+
};
76+
77+
const static auto initialize = [] {
78+
std::ios::sync_with_stdio(false);
79+
std::cin.tie(nullptr);
80+
std::cout.tie(nullptr);
81+
return nullptr;
82+
}();

leetcode/1851/solution-3.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime: 496 ms, faster than 99.44% of C++ online submissions for Minimum Interval to Include Each Query.
7+
Memory Usage: 109.3 MB, less than 98.14% of C++ online submissions for Minimum Interval to Include Each Query.
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<int> minInterval(vector<vector<int>>& itvs, vector<int>& qris) {
21+
int n = size(qris);
22+
int m = size(itvs);
23+
24+
int idx_qry[n];
25+
iota(idx_qry, idx_qry+n, 0);
26+
sort(idx_qry, idx_qry+n, [&qris](int & i, int & j){return qris[i] < qris[j];});
27+
28+
sort(all(itvs));
29+
30+
auto cmp = [](pair<int, int> &a, pair<int, int> &b){
31+
return a.second > b.second;
32+
};
33+
/// r, r-l+1
34+
priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> pq(cmp);
35+
36+
vi res(n);
37+
38+
int idx;
39+
int num;
40+
int cur_idx_itv = 0;
41+
int l = itvs[0][0];
42+
int r = itvs[0][1];
43+
44+
for (int i=0; i<n; ++i){
45+
idx = idx_qry[i];
46+
num = qris[idx];
47+
while(l <= num) {
48+
pq.push({r, r - l + 1});
49+
cur_idx_itv++;
50+
if (cur_idx_itv >= m) break;
51+
l = itvs[cur_idx_itv][0];
52+
r = itvs[cur_idx_itv][1];
53+
}
54+
/// not all are poped, but current top guarantees num in range
55+
while(!pq.empty() && pq.top().first < num){
56+
pq.pop();
57+
}
58+
59+
if (pq.empty()){
60+
res[idx] = -1;
61+
} else {
62+
res[idx] = pq.top().second;
63+
}
64+
}
65+
return res;
66+
}
67+
};
68+
69+
const static auto initialize = [] {
70+
std::ios::sync_with_stdio(false);
71+
std::cin.tie(nullptr);
72+
std::cout.tie(nullptr);
73+
return nullptr;
74+
}();

0 commit comments

Comments
 (0)