[Programmers] #120585 - 머쓱이보다 키 큰 사람 [Java][C++][Python]
[Programmers] #120585 - 머쓱이보다 키 큰 사람 [Java][C++][Python]
1. 아이디어
배열에서 머쓱이의 키보다 큰 원소의 개수를 세는 문제다. 키가 같은 사람은 더 큰 것이 아니므로 비교는 ‘이상’이 아니라 ‘초과’다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
class Solution {
public int solution(int[] array, int height) {
int cnt = 0;
for (int x : array) {
if (x > height) cnt++;
}
return cnt;
}
}
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> array, int height) {
int cnt = 0;
for (int x : array) {
if (x > height) cnt++;
}
return cnt;
}
1
2
def solution(array, height):
return sum(x > height for x in array)
조건식의 결과가 bool이라 sum으로 참인 개수를 셌다.
This post is licensed under CC BY 4.0 by the author.