반응형
https://leetcode.com/problems/max-increase-to-keep-city-skyline/
각 행과 열의 최댓값을 구하고
최대 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;
}
};
반응형
'IT > 알고리즘' 카테고리의 다른 글
34] Leetcode 1351. Count Negative Numbers in a Sorted Matrix (0) | 2020.07.19 |
---|---|
33] Leetcode 1304. Find N Unique Integers Sum up to Zero (0) | 2020.07.15 |
31] Leetcode 1512. Number of Good Pairs (0) | 2020.07.12 |
30★★] Leetcode 1010. Pairs of Songs With Total Durations Divisible by 60 (0) | 2020.07.10 |
29★] Leetcode 1013. Partition Array Into Three Parts With Equal Sum (0) | 2020.07.07 |
댓글