Skip to content

Commit 2a8d1e4

Browse files
committed
0757 solved.
1 parent bb296a2 commit 2a8d1e4

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
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_0757)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0757 main.cpp)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <cassert>
5+
6+
using namespace std;
7+
8+
9+
class Solution {
10+
public:
11+
int intersectionSizeTwo(vector<vector<int>>& intervals) {
12+
13+
sort(intervals.begin(), intervals.end(),
14+
[](const vector<int>& interval1, const vector<int>& interval2){
15+
16+
if(interval1[1] != interval2[1]) return interval1[1] < interval2[1];
17+
return interval1[0] >= interval2[0];
18+
});
19+
20+
int res = 2, largest1 = intervals[0][1], largest2 = intervals[0][1] - 1;
21+
for(int i = 1; i < intervals.size(); i ++){
22+
int a = intervals[i][0], b = intervals[i][1];
23+
if(a <= largest2 && largest1 <= b){
24+
continue;
25+
}
26+
else if(largest1 >= a){
27+
res ++;
28+
largest2 = largest1;
29+
largest1 = b;
30+
}
31+
else{
32+
res += 2;
33+
largest1 = b;
34+
largest2 = b - 1;
35+
}
36+
}
37+
return res;
38+
}
39+
};
40+
41+
42+
int main() {
43+
44+
vector<vector<int>> intervals1 = {{1,3},{1,4},{2,5},{3,5}};
45+
cout << Solution().intersectionSizeTwo(intervals1) << '\n';
46+
// 3
47+
48+
return 0;
49+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
760760
| 753 | [Cracking the Safe](https://leetcode.com/problems/cracking-the-safe/) | [] | [C++](0501-1000/0753-Cracking-the-Safe/cpp-0753/) | | |
761761
| 754 | [Reach a Number](https://leetcode.com/problems/reach-a-number/) | [Solution](https://leetcode.com/problems/reach-a-number/solution/) | [C++](0501-1000/0754-Reach-a-Number/cpp-0754/) | | |
762762
| | | | | | |
763+
| 757 | [Set Intersection Size At Least Two](https://leetcode.com/problems/set-intersection-size-at-least-two/) | [] | [C++](0501-1000/0757-Set-Intersection-Size-At-Least-Two/cpp-0757/) | | |
764+
| | | | | | |
763765
| 761 | [Special Binary String](https://leetcode.com/problems/special-binary-string/) | [solution](https://leetcode.com/problems/special-binary-string/solution/) | [C++](0501-1000/0761-Special-Binary-String/cpp-0761/) | | |
764766
| 762 | [Prime Number of Set Bits in Binary Representation](https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/) | [] | [C++](0501-1000/0762-Prime-Number-of-Set-Bits-in-Binary-Representation/cpp-0762/) | | |
765767
| 763 | [Partition Labels](https://leetcode.com/problems/partition-labels/) | [] | [C++](0501-1000/0763-Partition-Labels/cpp-0763/) | | |

0 commit comments

Comments
 (0)