s_탑의 높이
| source | softeer.ai/practice/11003 |
| type | 📌 개발노트 |
| topics | 600-알고리즘 & 코딩테스트 601 구현 & 완전탐색 |
| types | 문제풀이 |
문제
답
초기에
그냥 단순 바로 주변껄 보려고했다.
근데생각해보니 언제 오르막길이나타나는지 구해야할것같다
그래프라고 생각해보자 언제 오르막과 언제 내리막이 나타나는지 구하면된다.
!```python
import sys
고점을 고르고
고점중에 주변이 차이가 안나느것을 고른다
def addHeight(tarr,idx,k):
if k<0:
return -1
tarr[idx] += 1
k -= 1
left = idx - 1
right = idx + 1
if left >= 0 and tarr[idx]-tarr[left]>1:
return addHeight(tarr,left,k)
if right < len(tarr) and tarr[idx]-tarr[right]>1:
return addHeight(tarr,right,k)
return k
n, k = map(int,sys.stdin.readline().strip().split())
arr = list(map(int,sys.stdin.readline().split()))
length = len(arr)
안전하게 하나씩더하는 함수를 만들어야할듯
result = 0
if length < 2 :
result = arr[0] + k
else:
keyIdx = [0]
pickIdx = [0] if arr[0]-arr[1]>0 else []
l = 0
i = 2
while i<length:
m = i-1
r = i
if arr[m] == arr[r]:
i += 1
else:
ldif = arr[l] -arr[m]
rdif = arr[r]-arr[m]
check = (ldif>0 and rdif> 0) or (ldif<0 and rdif<0)
if check:
# 꼭짓점이다. 기울기의 방향이 바뀌는 구간이다.
keyIdx.append(m)
if ldif<0 and rdif<0: pickIdx.append(m)
l = m
i = m+2
else:
i += 1
if arr[length-1]-arr[keyIdx[-1]]>0 : pickIdx.append(length-1)
keyIdx.append(length-1)
# print(keyIdx,pickIdx)
if len(pickIdx) is 0 :
f = 0
while k>=0:
f += 1
k -= f
result = f+arr[0]-1
else:
top = arr[pickIdx[0]]
# tmpkeyIdx = keyIdx[:]
for p in pickIdx:
tmpk = k
tmparr = arr[:]
pTop = tmparr[p]
while tmpk >0:
tmpk = addHeight(tmparr,p,tmpk)
# print('tmpk',tmpk,p,tmparr)
if tmpk >= 0 :
pTop = tmparr[p]
# print('pTop',pTop)
if top < pTop:
top = pTop
result = top
print(result)