본문 바로가기
IT/알고리즘

32] Leetcode 807. Max Increase to Keep City Skyline

by 깻잎쌈 2020. 7. 12.
반응형

https://leetcode.com/problems/max-increase-to-keep-city-skyline/

 

Max Increase to Keep City Skyline - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

각 행과 열의 최댓값을 구하고 

최대 increase할 수 있는 높이는 두 최댓값 중 작은 값이므로

두 값중 작은 값에서 기존 값을 더한 값들을 리턴한다. 

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        // 각 행과 열의 최대값을 저장하는 배열
        int garo[51] = {-1};
        int sero[51] = {-1}; 
        int garoMax = -1;
        int seroMax = -1;
        
        // 각 행에서 최대값 
        for(int i = 0;i<grid.size();i++){
            garoMax = -1;
            for(int j = 0;j<grid.size();j++){
                if(garoMax < grid[i][j])
                    garoMax = grid[i][j];
            }
            
            garo[i] = garoMax;
        }
        
        // 각 열에서 최대값
        for(int i = 0;i<grid.size();i++){
            seroMax = -1;
            for(int j = 0;j<grid.size();j++){
                if(seroMax < grid[j][i])
                    seroMax = grid[j][i];
            }
            
            sero[i] = seroMax;
        }
        
        //행과 열의 최대값중 작은 값까지만 increase 가능 
        int ans = 0;
        for(int i = 0;i<grid.size();i++)
            for(int j = 0;j<grid.size();j++)
                ans += sero[i]<garo[j]? sero[i]-grid[i][j] : garo[j]- grid[i][j];
        
        return ans;
            
    }
};
반응형

댓글