Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 프로그래머스
- Lv. 0
- 다이나믹프로그래밍
- 브루트포스
- Lv. 1
- softeer
- level3
- bfs
- 타입스크립트
- Python
- Dynamic Programming
- group by
- 파이썬
- programmers
- C언어
- javascript
- SQL 고득점 KIT
- Typescript
- Stack
- Lv. 2
- dfs
- 소프티어
- Java
- 자바스크립트
- 문자열
- 너비 우선 탐색
- level 3
- Lv. 3
- 스택
- DP
Archives
- Today
- Total
몸과 마음이 건전한 SW 개발자
프로그래머스 [Lv. 2] 할인 행사 {언어 : Python} 본문
문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/131127
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
정답 코드
from collections import defaultdict
def solution(want, number, discount):
answer = 0
tot = sum(number)
lenW = len(want)
lenD = len(discount)
for i in range(lenD-tot+1):
tenDict = defaultdict(int)
isValid = True
# 저장
for j in range(i, min(i+10, lenD)):
d = discount[j]
tenDict[d] += 1
# 확인
for k in range(lenW):
w = want[k]
cntW = number[k]
cntD = tenDict.get(w, 0)
if cntW > cntD:
isValid = False
break
if isValid:
answer += 1
return answer
풀이 방법
- 10일 분량의 dictionary를 만들어서 discount 항목을 저장한다.
- defaultdict를 사용했기 때문에 해당 항목에 +1을 해주면서 수량을 파악할 수 있다.
- 저장이 끝나면 want항목과 비교한다.
- 이 과정에서 유효한 경우만 answer +1을 한다.
- answer를 반환한다.
느낀점
- 가장 단순한 접근이 때론 가장 효율적인 풀이일 수 있다.
'알고리즘' 카테고리의 다른 글
프로그래머스 [Lv. 2] 괄호 회전하기 {언어 : JavaScript} (0) | 2024.05.03 |
---|---|
프로그래머스 [Lv. 2] 2개 이하로 다른 비트 {언어 : Python} (0) | 2024.05.02 |
프로그래머스 [Lv. 2] n^2 배열 자르기 {언어 : JavaScript} (0) | 2024.04.28 |
프로그래머스 [Lv. 2] 주차 요금 계산 {언어 : Python} (0) | 2024.04.27 |
프로그래머스 [Lv. 2] 혼자 놀기의 달인 {언어 : Python} (0) | 2024.04.27 |