본문 바로가기

C++/코드트리

[코드트리] 숫자들의 최대 차

https://www.codetree.ai/missions/5/problems/maximum-difference-in-numbers?utm_source=clipboard&utm_medium=text 

 

코드트리 | 코딩테스트 준비를 위한 알고리즘 정석

국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.

www.codetree.ai

완전탐색으로 풀었고

정렬하고 처음부터 갯수 세고

그 다음부터 갯수 세고를 반복했다.

#include <iostream>
#include <algorithm>
using namespace std;
int n, k, map[1000];
int main() {
    cin >> n >> k;
    for(int i=0; i<n; i++)
        cin >> map[i];
    sort(map, map + n);
    int answer = 0;
    for(int i=0; i<n; i++){
        int cnt = 0;
        for(int j=i; j<n; j++){
            if(map[j] - map[i] > k) continue;
            cnt++; 
        }
        answer = max(answer, cnt);
    }

    cout << answer;
    return 0;
}