본문 바로가기
알고리즘 일기

프로그래머스 땅따먹기 // get_max 함수 구현하기

by Beijing_KingGod 2019. 9. 3.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<vector<int> > land)
{
    int answer = 0;
    vector<int> temp;
    for(int i=0; i<land.size()-1; i++){
        temp = land[i];
        temp.erase(temp.begin()+0);
        land[i+1][0]+= *max_element(temp.begin(),temp.end());
        temp = land[i];
        temp.erase(temp.begin()+1);
        land[i+1][1]+= *max_element(temp.begin(),temp.end());
        temp = land[i];
        temp.erase(temp.begin()+2);
        land[i+1][2]+= *max_element(temp.begin(),temp.end());
        temp = land[i];
        temp.erase(temp.begin()+3);
        land[i+1][3]+= *max_element(temp.begin(),temp.end());
    }
    answer+= *max_element(land[land.size()-1].begin(),land[land.size()-1].end());

    return answer;
}

// 12ms  나옴

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int get_max(int a, int b, int c){
    if(a>b){
        if(b>c){
            return a;
        }else{
            if(a>c){
                return a;
            }else{
                return c;
            }
        }
    }else if(b>c){
        return b;
    }else if(c>b){
        if(a>c){
            return a;
        }else{
            return c;
        }
    }
}
int solution(vector<vector<int> > land)
{
    int answer = 0;
    for(int i=0; i<land.size()-1; i++){
        land[i+1][0]+= get_max(land[i][1],land[i][2], land[i][3]);
        land[i+1][1]+= get_max(land[i][0],land[i][2], land[i][3]);
        land[i+1][2]+= get_max(land[i][1],land[i][0], land[i][3]);
        land[i+1][3]+= get_max(land[i][1],land[i][2], land[i][0]);
    }
    answer+= *max_element(land[land.size()-1].begin(),land[land.size()-1].end());

    return answer;
}

//6ms --> 반으로 줄어 들었다 벡터에 복사해서 지우고 찾는거 보다 함수 구현하니깐 반으로 줄었다.

 

** 거꾸로 생각하기

** 겹치는 열빼고 큰수 구하기로 

** 그 큰수를 더해서 차근차근 큰수 구하기

댓글