| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- Stack
- tableau
- python
- lv4
- time complexity
- import re
- sql
- LeetCode
- 코딩테스트
- 완전탐색
- 코딩
- Queue
- AARRR
- lambda
- coding test
- 코테
- 프로그래머스
- programmers
- join
- counter
- Level2
- level4
- 파이썬
- itertools
- collections
- Growth hacking
- mysql
- cte
- 조합
- coding
Archives
- Today
- Total
ror_coding
[Programmers] 햄버거 만들기 - 133502 본문
728x90
시간 초과로 stack 이용해서 O(n)로 풀기.
Question
상수에게 전해지는 재료의 정보를 나타내는 정수 배열 ingredient가 주어졌을 때, 상수가 포장하는 햄버거의 개수를 return 하도록 solution 함수를 완성하시오.
Code : Time Complexity O(n)
def solution(ingredient):
answer = 0
stack = []
for i in ingredient: # O(n)
stack.append(i) # O(1)
if stack[-4:] == [1,2,3,1] : answer += 1; del stack[-4:] # O(1)
return answer
Code 2 : Time Complexity O(n^2)
def solution(ingredient):
answer = 0
hamberger = "1231"
str_ing = ''.join(map(str,ingredient))
while hamberger in str_ing : # in O(n)
answer += 1
str_ing = str_ing.replace(hamberger,'',1) # O(n)
return answer

now me
On my github
728x90
'Coding Test > Python' 카테고리의 다른 글
| [Programmers] 귤 고르기 - 138476 (0) | 2024.10.24 |
|---|---|
| [Programmers] 구명보트 - 42885 (1) | 2024.10.24 |
| [Programmers] 공원 산책 - 172928 (3) | 2024.10.23 |
| [Programmers] 숫자 짝꿍 - 131128 (0) | 2024.10.22 |
| [Programmers] 체육복 - 42862 (1) | 2024.10.17 |