forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrumbs22.cpp
More file actions
28 lines (25 loc) ยท 924 Bytes
/
crumbs22.cpp
File metadata and controls
28 lines (25 loc) ยท 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> res;
int i = 0;
// ๊ฒน์น์ง ์์ ๋ res ๋ฒกํฐ์ intervals๋ฅผ ๊ทธ๋๋ก ์ฝ์
while (i < intervals.size() && intervals[i][1] < newInterval[0]) {
res.push_back(intervals[i]);
i++;
}
// ๊ฒน์น๋ ๋์ ๋ฐ๋ณตํ๋ฉฐ ์์์ง์ ๊ณผ ๋์ง์ ๊ฐฑ์
while (i < intervals.size() && intervals[i][0] <= newInterval[1]) {
newInterval[0] = min(intervals[i][0], newInterval[0]);
newInterval[1] = max(newInterval[1], intervals[i][1]);
i++;
}
res.push_back(newInterval); // ์ฝ์
// ๋จ์ ๋ฒกํฐ ๋ง์ ์ฝ์
while (i < intervals.size()) {
res.push_back(intervals[i]);
i++;
}
return (res);
}
};