본문 바로가기
SW/Algorithm Problem (Coding)

1640. Check Array Formation Through Concatenation(Easy)

by 라꾸스떼(YR) 2020. 11. 5.
반응형
 

Check Array Formation Through Concatenation - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

단순 배열을 잘 돌아다니는 For문 문제다.

2트만에 Accepted가 되었다... easy인데... 처음 제출했을때 문제를 잘못 이해하고 있었다.

pieces는 순서를 바꿔서 합칠수 있다!

easy 문제는 풀지 말아야되나...

 

class Solution:
    def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool:
        idx_i = 0
        idx_pieces = -1
        idx_j = 0
        for idx in range(0,len(pieces)):
            if pieces[idx][idx_j] == arr[idx_i]:
                idx_pieces = idx
                break
        if idx_pieces == -1:
            return False
        
        while True:
            arr_elem = arr[idx_i]
            pieces_elem = pieces[idx_pieces][idx_j]
            # print("arr : {} - pieces : {}".format(arr_elem, pieces_elem))
            if arr_elem != pieces_elem:
                return False
            else:
                idx_i += 1
                if idx_i == len(arr): # sum(pieces[i].length) == arr.length
                    return True
                if idx_j == len(pieces[idx_pieces])-1:
                    idx_j = 0
                    idx_pieces = -1
                    for idx in range(0,len(pieces)):
                        if pieces[idx][idx_j] == arr[idx_i]:
                            idx_pieces = idx
                            break
                    if idx_pieces == -1:
                        return False
                else:
                    idx_j += 1
        return False
반응형

댓글