230112 TIL

William Parker·2023년 1월 11일

Today I Learned

Coding Test Questions

Problem Description Sang-soo, who works at a hamburger shop, packs hamburgers. When other employees who work together cook the ingredients for the hamburgers, they are piled up in front of the constants in the order they were cooked, and the constants are piled up in order, and the completed hamburgers are moved and packed separately. The store where Sang-soo works packs only the hamburgers stacked in a set order (from the bottom, bread - vegetables - meat - bread). Because Sangsu is very fast, there is no additional material coming in while Sangsu is packing, and the height of the ingredients is ignored, so there is no case where the ingredients pile up high and make work difficult. For example, when the order of ingredients stacked in front of the constant is [vegetable, bread, bread, vegetable, meat, bread, vegetable, meat, bread], the constant is the third to sixth ingredient when the sixth ingredient is stacked. When the ninth ingredient is stacked, the second ingredient and the seventh through ninth ingredients are used to wrap the hamburger. That is, you will pack 2 hamburgers. Complete the solution function so that it returns the number of hamburgers that the constant wraps when given an integer array ingredient representing the ingredient information passed to the constant.

Restriction
1 ≤ length of ingredient ≤ 1,000,000

The element of ingredient is one of the values 1, 2, or 3, meaning bread, vegetables, or meat, in that order.

I/O example

I/O Example Description
I/O Example Same as problem example #1.
I/O Example #2 There is no hamburger that the constant can wrap.

My answer

def solution(ingredient):
    de = deque(ingredient)
    appendlist = []
    hamburgercount=0
    for i in range(len(ingredient)):
        appendlist.append(de.popleft())
        if appendlist[-4:] == [1,2,3,1]:
            hamburgercount+=1
            for j in range(4):
                appendlist.pop()
    return hamburgercount
profile
Developer who does not give up and keeps on going.

0개의 댓글