한량처럼 살고 싶다

1206. [S/W 문제해결 기본] 1일차 - View(Python) 본문

PS/SWEA

1206. [S/W 문제해결 기본] 1일차 - View(Python)

투영 2023. 10. 19. 19:01

https://swexpertacademy.com/main/code/problem/problemDetail.do

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

그렇게 어렵지 않은 문제였다.

엄청나게 많은 if문을 적었지만 사실 파이썬은 min 안에 여러 인자를 넣어도 된다고 하니 코드를 더 줄일 수 있을 것이다.

친구가 친절하게 가르쳐줬음.

def solution():
    for i in range(10):
        length = int(input())
        heights = list(map(int, input().split()))
        answer = 0
 
        #앞의 두개, 뒤의 두개를 제외
        for index in range(2, length-2, 1):
 
            #양쪽으로 +- 1개 확인하기
            left1 = heights[index]-heights[index-1]
            right1= heights[index]-heights[index+1]
            #나보다 양쪽으로 한칸의 건물 두 개가 키가 작으면 된다
 
            if left1 > 0 and right1 > 0:
                #양쪽으로 +- 2개 확인하기 
                left2 = heights[index]-heights[index-2]
                right2 = heights[index]-heights[index+2]
                if left2 > 0 and right2 > 0:
                    total_left = min(left1, left2)
                    total_right = min(right1, right2)
                    answer += min(total_left, total_right)
         
        print("#%d %d" %(i+1, answer))
 
 
solution()

'PS > SWEA' 카테고리의 다른 글

[SWEA] 파리 퇴치 (python)  (0) 2024.02.02
[SWEA] 창용 마을 무리의 개수 (python)  (0) 2024.01.13
1859. 백만 장자 프로젝트(Python)  (0) 2023.10.19
2072. 홀수만 더하기(Python)  (0) 2023.10.19