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
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
 
int solution(vector<int> A, vector<int> B) {
    int answer = 0;
    sort(A.begin(),A.end());
    sort(B.begin(),B.end());
    
    int lenA=0;
    int lenB=0;
    
    while(lenA<A.size() && lenB<B.size()){
        if(B[lenB]>A[lenA]){
            lenA++,lenB++,answer++;
        }else if(B[lenB]<A[lenA]){
            lenB++;
        }
        else{
            lenB++;
        }
    }
    
    return answer;
}
cs

출처: https://school.programmers.co.kr/learn/courses/30/lessons/12987

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

단순 비교로는 O(N^2) 시간이 걸릴 것 같아서 
투포인터와 비슷한 개념으로 풀었다. 정렬이 O(NlgN)이므로 최종 시간복잡도는 O(NlgN)으로 예상된다. 

문제의 핵심
순서를 맞추는 것이 아니라, 최대한 A를 이길수 있는 B의 멤버 수만 구하는 것이 정답. 

 

'알고리즘 > 프로그래머스' 카테고리의 다른 글

[c++] 셔틀버스  (1) 2022.09.21
[c++] 보석 쇼핑  (1) 2022.09.20
[c++] 등굣길  (0) 2022.09.19
[c++] 이중우선순위큐  (1) 2022.09.19
[c++] 정수 삼각형  (2) 2022.09.19

+ Recent posts