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
- telegraf
- 완전탐색
- typescript
- python
- 코딩테스트 연습
- monorepo
- 점화식
- algorhtim
- configfile
- pymodbus
- modbus
- Server monitoring
- 알고리즘
- React
- Two Pointer
- algorighm
- javascript
- Algorithm
- Stack
- frontend
- 스택/큐
- 파이썬
- 프로그래머스
- InfluxDB
- Vite
- pnpm
- Jest
- 슬라이딩 윈도우
- queue
- PCCP
Archives
- Today
- Total
DM Log
[연습문제] 할인 행사 - Python 본문
문제 링크 - https://school.programmers.co.kr/learn/courses/30/lessons/131127
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
[문제 간단 요약]
1. 고객(정현)이 원하는 제품을 연속으로 할인가로 살 수 있는 회원가입 일자를 구하는 문제
2. 회원 기간은 10일이고 해당 제품 전부를 구매할 수 있어야함
[문제 해결 방안]
1. 슬라이딩 윈도우 알고리즘을 활용해서 문제 해결
2. 회원 시작 기간 10개 범위에서 딕셔너리를 설정
3. 10개의 범위를 하나씩 이동하며 계산
[문제 해결 코드 - python]
def check_subp(dic):
for val in dic.values():
if val > 0:
return False
return True
def solution(want, number, discount):
answer = 0
cust_product = dict()
for i in range(len(want)):
cust_product[want[i]] = number[i]
for i in range(0,10):
if discount[i] in cust_product:
cust_product[discount[i]] -= 1
answer += check_subp(cust_product)
for i in range(0, len(discount)-10):
if discount[i] in cust_product:
cust_product[discount[i]] += 1
if discount[i+10] in cust_product.keys():
cust_product[discount[i+10]] -= 1
answer += check_subp(cust_product)
return answer
[다른 문제 해결 코드 - python]
from collections import Counter
def solution(want, number, discount):
answer = 0
dic = {}
for i in range(len(want)):
dic[want[i]] = number[i]
for i in range(len(discount)-9):
if dic == Counter(discount[i:i+10]):
answer += 1
return answer
성능 및 메모리 효율 비교
첫 번째 코드 (슬라이딩 윈도우) | 두 번째 코드 (Counter 사용) | |
시간 복잡도 | O(N) | O(NM) |
공간 복잡도 | O(M) | O(M) |
장점 | 빠름, 슬라이딩 윈도우 활용 | 코드가 간결함 |
단점 | check_subp(dic)에서 values() 순회 비용 존재 | Counter를 매번 새로 생성하여 비효율적 |
[문제 해결 코드 - javascript]
function check_subp(dic) {
for (let val of Object.values(dic)) {
if (val > 0) {
return false
}
}
return true
}
function solution(want, number, discount) {
var answer = 0;
let cust_product = {}
for (let i=0; i < want.length; i++) {
cust_product[want[i]] = number[i]
}
for (let i = 0; i < 10; i++) {
if ((discount[i] in cust_product)) {
cust_product[discount[i]] -= 1
}
}
answer += check_subp(cust_product)
for (let i=0; i < discount.length-10; i++) {
if (discount[i] in cust_product) {
cust_product[discount[i]] += 1
}
if (discount[i+10] in cust_product) {
cust_product[discount[i+10]] -= 1
}
answer += check_subp(cust_product)
}
return answer;
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[연습문제] 행렬의 곱셈 - Python / JavaScript (0) | 2025.03.22 |
---|---|
[월간 코드 챌린지 시즌3] n^2 배열 자르기 - Python / JavaScript (0) | 2025.03.19 |
[월간 코드 챌린지 시즌2] 괄호 회전하기 - Python (0) | 2025.03.11 |
[연습문제] 연속 부분 수열 합의 개수 - Python / JavaScript (0) | 2025.03.05 |
[2017 팁스타운] 예상 대진 - Python / JavaScript (0) | 2025.03.04 |