Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

0과 1 사이

[파이썬(python)] 기둥과 보 설치 본문

코딩테스트

[파이썬(python)] 기둥과 보 설치

고후 2022. 2. 28. 20:27

몇몇 테스트 케이스에서 통과했는데 10개의 테스트 케이스에서 런타임 에러로 실패했다.

왜인지 계속 들여다봤는데 내 코드가 답지와도 유사해서 더 모르겠더라.

 

계속 보니까 조건 하나를 빠트려서 실패했다.

기둥일 경우 보의 한쪽 끝부분 위에 있는 경우는 2가지가 있다. 

나는 1가지 경우만 체크했었음 ..

그래서 그 조건만 추가하니까 통과!

 

def istrue(answer):
    #조건 모두 만족하는지 확인
    
    for i in answer:
        if i[2] == 0:
            #기둥
            if i[1] == 0:
                #바닥 위에 있는 경우
                continue
            row = [i[0] - 1, i[1], 1]

            row_2 = [i[0], i[1], 1]
            col = [i[0], i[1] - 1, 0]
            
            if row in answer or row_2 in answer:
                #보 위에 있는 경우
                continue
            if col in answer:
                #기둥 위에 있는 경우
                continue
                
            return False
        else:
            #보
            row1 = [i[0], i[1] - 1, 0]
            row2 = [i[0] + 1, i[1] - 1, 0]
            
            #기둥 위에 있는 경우
            if row1 in answer:
                continue
            if row2 in answer:
                continue
            
            col1 = [i[0] - 1, i[1], 1]
            col2 = [i[0] + 1, i[1], 1]
            
            #양쪽 끝에 보가 있는 경우
            if col1 in answer and col2 in answer:
                continue
                
            return False
            
    return True
    
    
def solution(n, build_frame):
    answer = []
    #frame = [[] * (n+1)] * (n+1)
    #print(frame)
    
    for build in build_frame:
        if build[3] == 0:
            #삭제
            #일단 삭제해봄
            answer.remove(build[:3])
            #조건 만족하는지 확인
            if istrue(answer) == False:
                answer.append(build[:3])
            #만족하지 않으면 다시 지음
        else:
            #설치
            answer.append(build[:3])
            #일단 설치
            if not istrue(answer):
                answer.pop()
            #조건 맞는지 확인
            #만족하지 않으면 삭제
            
    result = sorted(answer)
    
    return result

 

 

하지만 코드를 좀더 깔끔하게 작성해봤다.

 

def istrue(answer):
    #조건 모두 만족하는지 확인
    
    for x, y, stuff in answer:
        if stuff == 0:
            #기둥
            if y == 0:
                #바닥 위에 있는 경우
                continue
            
            if [x-1, y, 1] in answer or [x, y, 1] in answer:
                #보 위에 있는 경우
                continue
            if [x, y-1, 0] in answer:
                #기둥 위에 있는 경우
                continue
                
            return False
        else:
            #보
            
            #기둥 위에 있는 경우
            if [x, y-1, 0] in answer:
                continue
            if [x+1, y-1, 0] in answer:
                continue
            
            
            #양쪽 끝에 보가 있는 경우
            if [x-1, y, 1] in answer and [x+1, y, 1] in answer:
                continue
                
            return False
            
    return True
    
    
def solution(n, build_frame):
    answer = []
    #frame = [[] * (n+1)] * (n+1)
    #print(frame)
    
    for build in build_frame:
        x, y, stuff, operate = build
        frame = build[:3]
        
        if operate == 0:
            #삭제
            #일단 삭제
            answer.remove(frame)
            #조건 만족하는지 확인
            if istrue(answer) == False:
                answer.append(frame)
            #만족하지 않으면 다시 지음
        else:
            #설치
            answer.append(frame)
            #일단 설치
            if not istrue(answer):
                answer.pop()
            #조건 맞는지 확인
            #만족하지 않으면 삭제
            
    result = sorted(answer)
    
    return result
Comments