πŸ“ Coding Test Study/Algorithm Problem

[C++][Programmers][완전탐색] μ΅œμ†Œμ§μ‚¬κ°ν˜•

ibelieveinme 2023. 4. 21. 00:51
728x90

[문제]

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

 

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€

μ½”λ“œ μ€‘μ‹¬μ˜ 개발자 μ±„μš©. μŠ€νƒ 기반의 ν¬μ§€μ…˜ λ§€μΉ­. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€μ˜ 개발자 λ§žμΆ€ν˜• ν”„λ‘œν•„μ„ λ“±λ‘ν•˜κ³ , λ‚˜μ™€ 기술 ꢁ합이 잘 λ§žλŠ” 기업듀을 λ§€μΉ­ λ°›μœΌμ„Έμš”.

programmers.co.kr

 

[해결법]

μœ„μ˜ μ˜ˆμ‹œμ—μ„œ κ°€μž₯ μž‘μ€ μ§€κ°‘μ˜ ν¬κΈ°λŠ” 80*70=5600 κ°™μ§€λ§Œ 2번 λͺ…함을 λˆ•νžˆλ©΄ κ°€λ‘œ70, μ„Έλ‘œ30인 μ‚¬μ΄μ¦ˆκ°€ λ˜λ―€λ‘œ κ°€μž₯ μž‘μ€ μ§€κ°‘μ˜ ν¬κΈ°λŠ” 80 * 50이닀.

 

즉, ν•œμͺ½μ„ 큰 값을 두고 λ‹€λ₯Έ ν•œμͺ½μ„ μž‘μ€ κ°’μœΌλ‘œ μ…‹νŒ…ν•œ ν›„, κ°€λ‘œ 쀑에 κ°€μž₯ 큰 κ°’κ³Ό μ„Έλ‘œ 쀑에 κ°€μž₯ 큰값을 뽑아야 함을 μ•Œ 수 μžˆλ‹€. 

 

1) 2차원 λ²‘ν„°μ˜ fristκ°’(κ°€λ‘œ) > second(μ„Έλ‘œ)값인 μƒνƒœμ—μ„œ λΉ„κ΅ν•˜λ„λ‘ ν•˜κΈ°. 
2) 2차원 λ²‘ν„°μ˜ firstκ°’(κ°€λ‘œ) 쀑에 κ°€μž₯ 큰 κ°’, second(μ„Έλ‘œ)κ°’ 쀑에 κ°€μž₯ 큰값 뽑기
3) λ‘κ°œ κ³±ν•΄μ„œ return.

 

[μ•Œμ•„λ‘˜ 것]

algorithm 라이브러리의 min, max ν•¨μˆ˜

min(1,2); // 1
max(1,2); // 2

 

[μ½”λ“œ]

#include <string>
#include <vector>

using namespace std;

int solution(vector<vector<int>> sizes) {
    int widthMax = 0, heightMax = 0;
    
    for(int i = 0; i<sizes.size(); i++){
        if(sizes[i][0] < sizes[i][1]){
            if(widthMax < sizes[i][1]){
                widthMax = sizes[i][1];
            }
            if(heightMax < sizes[i][0]){
                heightMax = sizes[i][0];
            }
        }else{
            if(widthMax < sizes[i][0]){
                widthMax = sizes[i][0];
            }
            if(heightMax < sizes[i][1]){
                heightMax = sizes[i][1];
            }
        }
    }
    
    return widthMax * heightMax;
}

μœ„μ— μ½”λ“œλ₯Ό min, max ν•¨μˆ˜ μ΄μš©ν•΄μ„œ 정리

#include <string>
#include <vector>

using namespace std;

int solution(vector<vector<int>> sizes) {
    int widthMax = 0, heightMax = 0;
    
    for(int i = 0; i<sizes.size(); i++){
        widthMax = max(widthMax, max(sizes[i][0], sizes[i][1]));
        heightMax = max(heightMax, min(sizes[i][0], sizes[i][1]));
    }
    
    return widthMax * heightMax;
}

μ μ ˆν•œ STL μ‚¬μš©μ˜ μ€‘μš”μ„±...

728x90