codility의 PrettyTiling 문제를 보며 느낀점

2023. 2. 8. 23:57·코딩테스트/Python

https://app.codility.com/programmers/challenges/carol_of_the_code2022/

 

Carol of the Code challenge

Take part in our Carol of the Code challenge.

app.codility.com

 

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import sys
sys.setrecursionlimit(10 ** 9)

answer = 987654321

def solution(A):
    # Implement your solution here
    answer = 987654321

    N = len(A)
    for i in range(4):
        rotate = i if i != 3 else 1
        prev_r = (1+i)%4
        prev_c = A[0][prev_r]
        for j in range(1, N):
            match_r = A[j].index(prev_c)
            rotate += abs(3-match_r) if match_r != 0 else 1
            prev_r = (match_r + 2) % 4
            prev_c = A[j][prev_r]
        answer = min(answer, rotate)
    return answer

 

 

이제 코테는 문제풀이용 알고리즘 찾고 하는것보단 진짜 그 문제를 풀기위한 코드를 작성하도록 바뀌는 것 같다.

그래서 문제의 본질을 파악해서 푼게 위 코드고 잘 나옴.

이게 맞는것 같은데, 내가 지금까지 하던게 대부분 쓸모없어진 것 같아 마음이 아프다.

 

 

밑에가 지금까지의 dfs네 뭐네 하며 고전적으로 푼 방법

 

import sys

sys.setrecursionlimit(10 ** 9)

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

answer = 0


def is_answer(A):
    for i in range(len(A) - 1):
        if A[i][1] != A[i + 1][3]:
            return False
    return True


def dfs(step, now, A):
    global answer
    if now == len(A):
        if is_answer(A):
            answer = min(answer, step)
        return

    # nothing
    dfs(step, now + 1, A)

    original = A[now][:]
    # clock
    A[now] = original[3] + original[:3]
    dfs(step + 1, now + 1, A)

    # clockwise
    A[now] = original[1:4] + original[0]
    dfs(step + 1, now + 1, A)

    # opposite
    A[now] = original[2:] + original[:2]
    dfs(step + 2, now + 1, A)

    A[now] = original


def solution(A):
    global answer
    # Implement your solution here
    # print(A)
    answer = 987654321
    dfs(0, 0, A)
    return answer

문제에 대한 답은 정확하지만 효율성(속도)에서 개쳐발린걸 볼 수 있다.

진짜 그 문제만을 위한 풀이를 해야된다.

'코딩테스트 > Python' 카테고리의 다른 글

1335. Minimum Difficulty of a Job Schedule 풀이 python  (1) 2023.12.30
파이썬 시간이 줄어드는 요소들 정리한거  (0) 2021.07.08
백준 input = sys.stdin.readline 차이  (0) 2021.07.02
'코딩테스트/Python' 카테고리의 다른 글
  • 1335. Minimum Difficulty of a Job Schedule 풀이 python
  • 파이썬 시간이 줄어드는 요소들 정리한거
  • 백준 input = sys.stdin.readline 차이
용나리
용나리
  • 용나리
    티스토리 블로그
    용나리
  • 전체
    오늘
    어제
    • 분류 전체보기 (334)
      • 과거의 것들 (93)
        • AI Tech boostcamp (92)
      • 생각정리(고찰) (2)
      • 기술 글 (1)
      • 코딩테스트 (4)
        • C++ (0)
        • Python (4)
      • CS (121)
        • 컴퓨터 시스템 (4)
        • 코틀린 인 액션 (13)
        • 김영한 스프링 강의 (104)
      • 일지 남기기용 (11)
        • 운동 (10)
      • 개발 배포 해보기 (1)
      • 프로그래밍 언어 및 기타 (32)
        • Spring Boot (9)
        • Python (9)
        • Kotlin (1)
        • Flutter (2)
        • SQL (4)
        • Docker (3)
        • 공통 (4)
      • os (4)
        • Linux (4)
      • 기술 (17)
        • PyTorch (6)
        • Computer Vision (6)
        • NLP (1)
        • 기타 (4)
      • 제품 후기 (0)
      • 게임 (0)
        • Human Resource Machine (0)
      • 강의 (26)
        • fullstackdeeplearning_sprin.. (9)
        • 부캠 안드로이드 학습정리 (17)
      • 개인 메모 (10)
      • IT 기타 (5)
      • 논문 읽기 연습 (5)
        • Computer Vision (1)
        • NLP (0)
        • 공통 (2)
        • 그냥 메모 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    pip install killed
    파이썬 실행경로
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
용나리
codility의 PrettyTiling 문제를 보며 느낀점
상단으로

티스토리툴바