반응형
https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/
The Number of Weak Characters in the Game - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
attack 내림차순, defence 오름차순으로 정렬해줍니다.
순차적으로 탐색하면서 defence의 최댓값을 업데이트해줍니다.
현재 탐색하고 있는 defence가 최댓값보다 작다면, weak character입니다.
bool comp(vector<int>& a, vector<int>& b) {
return a[0] != b[0] ? a[0] > b[0] : a[1] < b[1];
}
class Solution {
public:
int numberOfWeakCharacters(vector<vector<int>>& props) {
sort(props.begin(), props.end(), comp);
int mx = props[0][1];
int cnt = 0;
for(auto& prop : props) {
if(prop[1] < mx) cnt++;
mx = max(mx, prop[1]);
}
return cnt++;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 16 : 3Sum Closest (1) | 2022.10.08 |
---|---|
LeetCode 393 : UTF-8 Validation (0) | 2022.09.13 |
LeetCode 814 : Binary Tree Pruning (0) | 2022.09.06 |
LeetCode 987 : Vertical Order Traversal of a Binary Tree (0) | 2022.09.04 |
LeetCode 967 : Numbers With Same Consecutive Differences (0) | 2022.09.03 |