문제 조건 :
#1 111
#2 60
#3 165
N, 리스트 tower에 저장range(2, N - 2) )tower[i]가 좌우 2개보다 클 경우 탐색. 이는 조망권이 확보될 수 있는 tower[i]를 찾는 것.tower[i]는 좌우 2개의 건물 중 높이의 최댓값을 tower[i]의 높이에서 뺌 → 조망권 계산.메모리: 59,520 KB, 시간: 77 ms, 코드길이: 400 Bytes
answer = []
for n in range(1, 11):
N = input()
tower = [int(x) for x in input().split()]
count = 0
for i in range (2, N - 2):
if (tower[i] > tower[i-2]) and(tower[i] > tower[i-1]) and (tower[i] > tower[i + 1]) and (tower[i] > tower[i + 2]):
count += tower[i] - max(tower[i-1], tower[i-2], tower[i+1], tower[i+2])
answer.append(f"#{n} {count}")
print("\n".join(answer))
맞음.
all() 함수 적용if (tower[i] > tower[i-2]) and(tower[i] > tower[i-1]) and (tower[i] > tower[i + 1]) and (tower[i] > tower[i + 2]):
를 아래와 같이 바꿀 수 있음.
if all(tower[i] > tower[i+j] for j in [-2, -1, 1, 2]):
if all(tower[i] > tower[j] for j in [i-2, i-1, i+1, i+2]):
answer = []
for n in range(1, 11):
input() # N은 사용하지 않으므로 생략 처리
tower = list(map(int, input().split()))
count = 0
for i in range(2, len(tower) - 2):
if all(tower[i] > tower[j] for j in [i-2, i-1, i+1, i+2]):
count += tower[i] - max(tower[i-2], tower[i-1], tower[i+1], tower[i+2])
answer.append(f"#%d %d" % (n, count))
print("\n".join(answer))