[프로그래머스/C++]Lv.0 - 직사각형 넓이 구하기

YH J·2023년 4월 18일
0

프로그래머스

목록 보기
39/168

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/120860

내 풀이

점들의 x좌표와 y좌표를 한곳에 몰아놓고 정렬시킨다. x의 최대에서 최소를 뺀값(x길이) y최대에서 y최소를 뺀 값(y길이)의 곱을 리턴해준다.

내 코드

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<vector<int>> dots) {
    int answer = 0;
    
    vector<int> x;
    vector<int> y;
    
    for(const auto& d : dots)
    {
        x.push_back(d[0]);
        y.push_back(d[1]);
    }
    
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    
    answer = (x[3] - x[0])*(y[3]-y[0]);
    
    return answer;
}

다른 사람의 풀이

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

int solution(vector<vector<int>> dots)
{
    int answer=0;

    sort(dots.begin(), dots.end());
    answer=(dots[3][0]-dots[0][0])*(dots[3][1]-dots[0][1]);

    return answer;
}

다른 사람의 풀이 해석

다른곳에 옮길 필요 없이 정렬 후 계산한다.

profile
게임 개발자 지망생

0개의 댓글