[LeetCode] 807. Max Increase to Keep City Skyline

Updated:

Max Increase to Keep City Skyline

n * n ํฌ๊ธฐ์˜ ๋•…์— ๊ฐ์ž ๋†’์ด๊ฐ€ ๋‹ค๋ฅธ ๋นŒ๋”ฉ์ด ์žˆ๋‹ค. ๊ธฐ์กด์˜ ์Šค์นด์ด๋ผ์ธ์ด ๋ฐ”๋€Œ์ง€ ์•Š๊ฒŒ ๋นŒ๋”ฉ๋“ค์˜ ์ธต ์ˆ˜๋ฅผ ์˜ฌ๋ฆฌ๋ ค๊ณ  ํ•œ๋‹ค.

๊ฐ ๋นŒ๋”ฉ๋“ค์€ ์ตœ๋Œ€ ๋ช‡ ์ธต ๊นŒ์ง€ ์˜ฌ๋ฆด ์ˆ˜ ์žˆ์„๊นŒ ? ์ด๋•Œ ๋ชจ๋“  ๋นŒ๋”ฉ์˜ ์ถ”๊ฐ€ ์ธต ์ˆ˜ ๋ฅผ ๋”ํ•ด์„œ ๋ฐ˜ํ™˜ ํ•˜๋ฉด ๋œ๋‹ค.

์˜ˆ์‹œ์—์„œ ๋ณด์ž๋ฉด ์™ผ์ชฝ์—์„œ ๋ดค์„๋•Œ ์Šค์นด์ด๋ผ์ธ์€ [8, 7, 9, 3] ์ด๊ณ  ๋ฐ‘์—์„œ ๋ดค์„ ๋•Œ ์Šค์นด์ด๋ผ์ธ์€ [9, 4, 8, 7] ์ด๋‹ค.

์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด์„œ ๋นŒ๋”ฉ ์ธต ์„ ๋†’์—ฌ๋ณด๋ฉด ์ผ์ •ํ•œ ๊ทœ์น™์ด ๋ณด์ธ๋‹ค.

๋ฐ”๋กœ ๊ฐ index ์—์„œ ์™ผ์ชฝ์—์„œ์˜ ์Šค์นด์ด๋ผ์ธ, ๋ฐ‘์—์„œ์˜ ์Šค์นด์ด๋ผ์ธ ๊ฐ’ ์ค‘ ์ž‘์€ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

(0, 0) (0, 1) (0, 2) (0, 3)

(1, 0) (1, 1) (1, 2) (1, 3)

(2, 0) (2, 1) (2, 2) (2, 3)

(3, 0) (3, 1) (3, 2) (3, 3) ์ด๋ ‡๊ฒŒ index ๋ฅผ ๋น„๊ตํ•˜๋ฉด ๋œ๋‹ค.


class Solution:
	def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
		row = [max(val) for val in grid]
		col = [max(val) for val in zip(*grid)]

		increase = []
		for i in range(len(row)):
			tmp = []
			for j in range(len(col)):
				tmp.append(min(row[i], col[j]))
			increase.append(tmp)

		answer = 0
		for origin, up in zip(grid, increase):
			answer += sum(up) - sum(origin)

		return answer

ํ–‰๋ ฌ์„ 90๋„ ๋Œ๋ฆฌ๊ณ  ์‹ถ๋‹ค๋ฉด zip(*arr) ์„ ์‚ฌ์šฉํ•˜์ž ์•„์ฃผ ์œ ์šฉํ•˜๋‹ค.


Categories:

Updated:

Leave a comment