[Codility/Lesson7]Stonewall(python)

zzarbttoo·2021년 9월 19일
0

코딜리티

목록 보기
18/29

| 1트

import collections
def solution(H):
    
    queue = collections.deque([])
    count = 0

    for hei in H:
        if queue:
            if hei < queue[0]:
                before_num = 0
                temp_count = 0
                temp_hash = collections.defaultdict(int)
                while queue:
                    if queue[0] > before_num:
                        temp_count += 1
                    elif queue[0] < before_num and temp_hash[queue[0]] == 0:
                        temp_count += 1
                    before_num = queue.popleft()
                    temp_hash[before_num] = 1
                count += temp_count
            queue.append(hei)
        else:
            queue.append(hei)

    temp_hash = collections.defaultdict(int)
    before_num = 0
    while queue:
        if queue[0] > before_num:
            count += 1
        elif queue[0] < before_num and temp_hash[queue[0]] == 0 :
            count += 1
        before_num = queue.popleft()
        temp_hash[before_num] = 1

    return count 

  • 기준을 min값으로 고정시키고 풀어서 풀이가 복잡하고 놓치는 부분이 있었다

결과는 여기서

| 2트

def solution(H):

    stack = []
    count = 0

    for hei in H:
        while stack and stack[-1] > hei:
            stack.pop()

        if not stack or stack[-1] < hei:
            stack.append(hei)        
            count += 1

    return count
  • 기준을 그때그때 변경하도록 했다
  • 위처럼 풀면 같은 값에 대해서는 고려하지 않아도 된다

결과는 여기

profile
나는야 누워있는 개발머신

0개의 댓글