Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 |
30 | 31 |
Tags
- FastAPI
- 자바스크립트
- AZURE
- Deployment
- ansible
- asyncio
- AZ-104
- IAM
- terraform
- Service
- asgi
- Python
- EC2
- elasticsearch
- leetcode
- AZ-900
- dockerfile
- Django
- Role
- POD
- DevOps
- docker
- Kubernetes
- ebs
- Network
- AWS
- EKS
- RBAC
- IAC
- K8S
Archives
- Today
- Total
궁금한게 많은 개발자 노트
[ leetcode ] 1288. Remove Covered Intervals 본문
여러 구간들이 주어졌을 때, 어떤 한 구간이 다른 구간에 포함되는 경우 제거하고 남은 구간의 수를 반환하는 문제입니다.
이 때 정렬이 필요한데, 기본 오름 차순 정렬이 아닌 start는 오름 차순이고 end는 큰수가 먼저 앞에 오도록 정렬해야 순서대로 확인 시 다음 구간을 포함하는지 확인하기에 용이합니다.
#include <algorithm>
bool compare(vector<int> a, vector<int> b) {
if (a[0] == b[0]) return a[1] > b[1];
return a[0] < b[0];
}
class Solution {
public:
int removeCoveredIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), compare);
int start = intervals[0][0];
int end = intervals[0][1];
int count = 0;
for (int i = 1; i < intervals.size(); i++) {
if (end >= intervals[i][1]) count++;
else end = intervals[i][1];
}
return intervals.size() - count;
}
};
'Algorithm' 카테고리의 다른 글
[ leetcode ] 111. Minimum Depth of Binary Tree (0) | 2023.05.02 |
---|---|
[ leetcode ] 258. Add Digits (0) | 2023.05.02 |
[ leetcode ] 56. Merge Intervals (0) | 2023.05.02 |
[ leetcode ] 1046. Last Stone Weight (0) | 2023.05.02 |
[ leetcode ] 236. Lowest Common Ancestor of a Binary Tree (0) | 2023.05.02 |
Comments