๋ฐ์ํ
๐ฑ ๋ฌธ์
๐ฑ ํ์ด
1. ๊ธฐ์ธ๊ธฐ ๊ณ์ฐ
def calc_gradient(x1, y1, x2, y2):
if x1 == x2:
return 0
gradient = (y2 - y1) / (x2 - x1)
return gradient
๊ฑด๋ฌผ์ด ๋ณด์ด๋์ง ์ฌ๋ถ๋ ๊ธฐ์ธ๊ธฐ์ ๋ฐ๋ผ ๊ฒฐ์ ๋๋ฏ๋ก ๊ธฐ์ธ๊ธฐ๋ฅผ ๊ณ์ฐํ๋ ํจ์๋ฅผ ์์ฑํฉ๋๋ค.
1. ์์ ํ์ (Bruteforce)
results = []
for idx in range(len(buildings)):
cnt = 0
min_gradient = INF
for i in range(idx - 1, -1, -1):
gradient = calc_gradient(i, buildings[i], idx, buildings[idx])
if gradient < min_gradient:
cnt += 1
min_gradient = gradient
max_gradient = -INF
for i in range(idx + 1, len(buildings)):
gradient = calc_gradient(i, buildings[i], idx, buildings[idx])
if gradient > max_gradient:
cnt += 1
max_gradient = gradient
results.append(cnt)
- ์์ ํ์์ผ๋ก ๋ชจ๋ ๊ฑด๋ฌผ๋ค์ ๊ธฐ์ธ๊ธฐ๋ฅผ ๋น๊ตํ์ฌ ๋ณด์ด๋ ๊ฑด๋ฌผ์ ๊ฐ์(
cnt
)๋ฅผ ์นด์ดํธํฉ๋๋ค. - ์ผ์ชฝ ๊ฑด๋ฌผ๋ค์ ํ์ฌ๊น์ง ์ธก์ ํ ์ต์ ๊ธฐ์ธ๊ธฐ(
min_gradient
)๋ณด๋ค ๊ธฐ์ธ๊ธฐ๊ฐ ํฌ๋ฉด ๋ณด์ด์ง ์์ต๋๋ค. - ์ค๋ฅธ์ชฝ ๊ฑด๋ฌผ๋ค์ ํ์ฌ๊น์ง ์ธก์ ํ ์ต๋ ๊ธฐ์ธ๊ธฐ(
max_gradient
)๋ณด๋ค ๊ธฐ์ธ๊ธฐ๊ฐ ์์ผ๋ฉด ๋ณด์ด์ง ์์ต๋๋ค.
๐ฑ ์ฝ๋
# !usr/bin/env python
# -*- coding: utf-8 -*-
# boj 1027 ๊ณ ์ธต๊ฑด๋ฌผ
import sys
INF = sys.maxsize
n = int(input())
buildings = list(map(int, input().split()))
def calc_gradient(x1, y1, x2, y2):
if x1 == x2:
return 0
gradient = (y2 - y1) / (x2 - x1)
return gradient
results = []
for idx in range(len(buildings)):
cnt = 0
min_gradient = INF
for i in range(idx - 1, -1, -1):
gradient = calc_gradient(i, buildings[i], idx, buildings[idx])
if gradient < min_gradient:
cnt += 1
min_gradient = gradient
max_gradient = -INF
for i in range(idx + 1, len(buildings)):
gradient = calc_gradient(i, buildings[i], idx, buildings[idx])
if gradient > max_gradient:
cnt += 1
max_gradient = gradient
results.append(cnt)
print(max(results))
๋ฐ์ํ
'Algorithm > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] #11000 ๊ฐ์์ค ๋ฐฐ์ (Python, ์ฐ์ ์์ ํ, ๊ทธ๋ฆฌ๋) (2) | 2023.03.17 |
---|---|
[BOJ] #16724 ํผ๋ฆฌ ๋ถ๋ ์ฌ๋์ด (0) | 2023.03.14 |
[BOJ] #1450 ๋ ์๋ฌธ์ (์ด๋ถ ํ์, Python) (0) | 2023.02.27 |
[BOJ] #2110 ๊ณต์ ๊ธฐ ์ค์น (์ด๋ถํ์, Python) (0) | 2023.02.26 |
[BOJ] #2252 ์ค ์ธ์ฐ๊ธฐ (Python) (0) | 2023.02.22 |
๋๊ธ