밍비
프로그램의 편린
밍비
전체 방문자
오늘
어제
  • 분류 전체보기 (64)
    • Spring (2)
    • TIL (23)
    • 프로그래머스 (12)
    • Udemy (16)
    • Typescript (2)
    • MERN (1)
    • AWS (7)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • state 끌어올리기
  • 분산저장소
  • 서비스아키텍처
  • 함수형 update
  • Availability Zones
  • 한입크기로잘라먹는리액트
  • react
  • useState
  • State 합치기
  • 리액트 생애주기
  • Edge Locations
  • DOM
  • 컴포넌트트리
  • state 관리
  • useRef
  • overflow-wrap
  • 한입 크기로 잘라먹는 리액트
  • 리액트 프로젝트 만들기
  • 수평 스케일링
  • API 호출
  • 리스트 조회
  • useNavigate
  • 네이버커넥트
  • AWS Regions
  • 데이터 수정
  • Points of Presence
  • Page Moving
  • 리액트 reducer
  • 리액트
  • useParams

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
밍비
프로그래머스

키패드 누르기

키패드 누르기
프로그래머스

키패드 누르기

2022. 6. 23. 01:33
728x90

https://programmers.co.kr/learn/courses/30/lessons/67256

def solution(numbers, hand):
    answer = ''
    ans_arr = []
    dist_2 = [1,0,1,2,1,2,3,2,3,4,3,4]
    dist_5 = [2,1,2,1,0,1,2,1,2,3,2,3]
    dist_8 = [3,2,3,2,1,2,1,0,1,2,1,2]
    dist_0 = [4,3,4,3,2,3,2,1,2,1,0,1]
    left_now = 10
    right_now = 12
    for i in numbers:
        if i in [1,4,7]:
            ans_arr.append('L')
            left_now = i
        elif i in [3,6,9]:
            ans_arr.append('R')
            right_now = i
        elif i==2:
            if dist_2[left_now-1] < dist_2[right_now-1] or (dist_2[left_now-1]==dist_2[right_now-1] and hand=='left'):
                ans_arr.append('L')
                left_now = i
            if dist_2[left_now-1]>dist_2[right_now-1] or (dist_2[left_now-1]==dist_2[right_now-1] and hand=='right'):
                ans_arr.append('R')
                right_now = i
        elif i==5:
            if dist_5[left_now-1] < dist_5[right_now-1] or (dist_5[left_now-1]==dist_5[right_now-1] and hand=='left'):
                ans_arr.append('L')
                left_now = i
            if dist_5[left_now-1]>dist_5[right_now-1] or (dist_5[left_now-1]==dist_5[right_now-1] and hand=='right'):
                ans_arr.append('R')
                right_now = i
        elif i==8:
            if dist_8[left_now-1] < dist_8[right_now-1] or (dist_8[left_now-1]==dist_8[right_now-1] and hand=='left'):
                ans_arr.append('L')
                left_now = i
            if dist_8[left_now-1]>dist_8[right_now-1] or (dist_8[left_now-1]==dist_8[right_now-1] and hand=='right'):
                ans_arr.append('R')
                right_now = i
        elif i==0:
            if dist_0[left_now-1] < dist_0[right_now-1] or (dist_0[left_now-1]==dist_0[right_now-1] and hand=='left'):
                ans_arr.append('L')
                left_now = 11
            if dist_0[left_now-1]>dist_0[right_now-1] or (dist_0[left_now-1]==dist_0[right_now-1] and hand=='right'):
                ans_arr.append('R')
                right_now = 11

    answer = ''.join(ans_arr)

    return answer
print(solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0],"right"))

이게 맞나...돌아가긴함

공간복잡도 조건 없길래 일단 이렇게 했는데 다른문제에서 조건 생기면 좀더 생각해봐야겠다...

 

또 소소한 팁

출처:https://www.pymoon.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%AC%EC%97%90%EC%84%9C-%EB%AC%B8%EC%9E%90%EC%97%B4-%ED%95%A9%EC%B9%98%EA%B8%B0

그렇다고 한다...문자열 다룰 때는 리스트로 처리하고 마지막에 문자열로 변환하자!

728x90

'프로그래머스' 카테고리의 다른 글

오픈채팅방  (0) 2022.07.01
문자열 압축  (0) 2022.07.01
숫자 문자열과 영단어  (0) 2022.06.23
신규 아이디 추천  (0) 2022.06.22
로또의 최고 순위와 최저 순위  (0) 2022.06.22
    밍비
    밍비

    티스토리툴바

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.