99클럽 코테 스터디 11일차 TIL + 정렬

박지원·2024년 8월 1일

99클럽 코테 스터디

목록 보기
7/25

오늘의 학습 키워드

정렬

공부한 내용 본인의 언어로 정리하기

프로그래머스 159994

문자열로 이루어진 배열 cards1, cards2와 원하는 단어 배열 goal이 매개변수로 주어질 때, cards1과 cards2에 적힌 단어들로 goal를 만들 있다면 "Yes"를, 만들 수 없다면 "No"를 return하는 solution 함수를 완성해주세요.

어떤 문제가 있었고, 나는 어떤 시도를 했는지

def solution(cards1, cards2, goal):
    idx1=0
    idx2=0
    for word in goal:
        if  word == cards1[idx1]:
            idx1 += 1
        elif  word == cards2[idx2]:
            idx2 += 1
        else:
            return "No" 
    return "Yes"
  • Index Error 발생
def solution(cards1, cards2, goal):
    idx1=0
    idx2=0
    for word in goal:
        if idx1 < len(cards1) and word == cards1[idx1]:
            idx1 += 1
        elif idx2 < len(cards2) and word == cards2[idx2]:
            idx2 += 1
        else:
            return "No" 
    return "Yes"
  • len 을 체크하는 로직을 and 조건문으로 줘서 index 체크 후 조건 확인하도록 구현

다른 사람의 풀이

def solution(cards1, cards2, goal):
    answer = 'No'
    cards1_subset = []
    cards2_subset = []
    for w in goal:
        if w in cards1:
            cards1_subset.append(w)
        elif w in cards2:
            cards2_subset.append(w)
    if cards1_subset == cards1[:len(cards1_subset)] and cards2_subset == cards2[:len(cards2_subset)]:
        answer = 'Yes'

    return answer
  • 내가 처음에 생각했던 로직
  • index 를 체크하는 부분을 따로 줬는데 , 인덱스 슬라이싱을 통해 조건을 준게 인상적

무엇을 새롭게 알았는지

  • 조건문을 줄때 index 체크 로직도 같이 주려면 and 앞에 줘야한다

학습할 것은 무엇인지

  • heapq 개념 정리 및 활용 공부
  • 시간복잡도

0개의 댓글