Post

[Programmers] 120585번 - 머쓱이보다 키 큰 사람 [Java][C++]

[Programmers] 120585번 - 머쓱이보다 키 큰 사람 [Java][C++]

문제 링크


1. 아이디어

머쓱이네 반 친구들의 키가 담긴 정수 배열 array와 머쓱이의 키 height가 매개변수로 주어질 때, 머쓱이보다 키 큰 사람 수를 return하는 문제로 array에서 height 보다 큰 원소의 수를 세주면 된다.


2. 복잡도

시간복잡도공간복잡도
$O(N)$$O(1)$

3. 코드

풀이 [Java][C++]

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
12
#include <vector>

using namespace std;

int solution(vector<int> array, int height) {
    int cnt = 0;
    for (int x : array) {
        if (x > height) cnt++;
    }

    return cnt;
}

This post is licensed under CC BY 4.0 by the author.