220v
젝무의 개발새발
220v
전체 방문자
오늘
어제
  • 분류 전체보기 (255)
    • AI (35)
      • ML, DL 학습 (30)
      • 논문 리뷰 (4)
      • 실습 및 프로젝트 (1)
    • Algorithm (145)
      • LeetCode (13)
      • 프로그래머스 (35)
      • 백준 (96)
      • 알고리즘, 문법 정리 (1)
    • Mobile, Application (17)
      • Flutter (10)
      • iOS, MacOS (7)
    • BackEnd (7)
      • Flask (1)
      • Node.js (5)
      • Spring, JSP..etc (1)
    • Web - FrontEnd (18)
      • JavaScript, JQuery, HTML, C.. (12)
      • React (6)
    • DataBase (1)
      • MySQL (1)
      • Firebase Firestore (0)
      • Supabase (0)
    • Git (1)
    • 기타 툴 및 오류 해결 (3)
    • 강의 (5)
      • Database (3)
      • 암호학 (2)
      • 알고리즘 (0)
    • 후기와 회고 (2)
    • 블로그 꾸미기 (1)
    • 일상과 이것저것 (20)
      • 맛집 (12)
      • 세상사는일 (4)
      • 도서리뷰 (1)
      • 이런저런 생각들 (잡글) (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • brute-Force
  • 오블완
  • top-down
  • 티스토리챌린지
  • Priority Queue
  • bitmasking
  • dp
  • REACT
  • Minimum Spanning Tree
  • Lis
  • topological sort
  • Greedy
  • Mathematics
  • Dynamic Programming
  • binary search
  • two pointer
  • BFS
  • Prefix Sum
  • 프로그래머스
  • IMPLEMENT
  • disjoint set
  • 구현
  • simulation
  • 위상 정렬
  • Backtracking
  • 다익스트라
  • 백준
  • implementation
  • dfs
  • union-find

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
220v

젝무의 개발새발

Algorithm/프로그래머스

[프로그래머스] 42584. 주식가격

2022. 7. 12. 21:01

Programmers - 42584. 주식가격

접근1

맘같아선 싹 다 리스트에 박아버리고 for문 돌리고 싶다.

한번 해볼까

def solution(prices):
    answer = [0]*len(prices)

    for i in range(len(prices)):
        for j in range(i):
            if answer[j] == 0:
                if prices[j] > prices[i]:
                    answer[j] = i-j

    for i in range(len(answer) - 1):
        if answer[i] == 0:
            answer[i] = len(answer) - 1 - i
            
    return answer

 

네 당연히 잘 돌아가구요

존나 오래 걸립니다

 

접근2

from collections import deque
def solution(prices):
    answer = [0]*len(prices)

    deq = deque(prices)

    for i in range(len(prices)-1, -1, -1):
        popPrice = deq.pop()
        for j in range(i):
            if popPrice < prices[j]:
                answer[j] = i-j

    leng = len(answer)
    for i in range(leng-1):
        if answer[i] == 0:
            answer[i] = leng - 1 - i

            
    return answer

똑같은 알고리즘으로 될 거라고 생각한 내가 병신임

 

접근3

그런데 이제 개선을 곁들인

def solution(prices):
    answer = [0]*len(prices)
    leng = len(prices)

    for i in range(leng):
        for j in range(i+1, leng):
            if prices[i] > prices[j]:
                answer[i] = j - i
                break
        if answer[i] == 0:
            answer[i] = leng-1 - i

    return answer

필요 없는 반복계산을

숭덩숭덩 잘라내니까

해결이 됐어요 와!

    'Algorithm/프로그래머스' 카테고리의 다른 글
    • [프로그래머스] 42587. 프린터
    • [프로그래머스] 42586. 기능개발
    • [프로그래머스] 42583. 다리를 지나는 트럭
    • [프로그래머스] 42579. 베스트앨범
    220v
    220v
    DGU CSE 20 / Apple Developer Academy @ POSTECH 2nd Jr.Learner.

    티스토리툴바