사용언어: 자바(Java)
할인행사(Lv. 2)
https://school.programmers.co.kr/learn/courses/30/lessons/131127#
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
import java.util.*;
class Solution {
public int solution(String[] want, int[] number, String[] discount) {
int answer = 0;
HashMap<String, Integer> map = resetMap(want, number);
int idx = 0;
while(9 + idx < discount.length){ // 길이 내에서 코드가 실행되도록
map = resetMap(want, number); // 매번 첫번째 순서가 바뀌면 map 재구성
// map을 매번 재구성하지 않고
// 구입한 목록의 map을 새로 만들어 비교하는 방식을 사용할 수도 있음
for(int i = 0; i < 10; i++){
if(map.containsKey(discount[i+idx])){
if(map.get(discount[i+idx]) == 1){
map.remove(discount[i+idx]);
}
else{
map.put(discount[i+idx], map.get(discount[i+idx])-1);
}
}
if(map.size() == 0){ // 모든 품목이 겹치면 모든 품목의 할인이므로 answer++
answer++;
}
}
idx++;
}
return answer;
}
public HashMap<String, Integer> resetMap(String[] want, int[] number){
HashMap<String, Integer> tempMap = new HashMap<>();
for(int i = 0; i < want.length; i++){
tempMap.put(want[i], number[i]);
}
return tempMap;
}
}
'알고리즘 > 코테 문제풀이' 카테고리의 다른 글
[프로그래머스] 대장균들의 자식의 수 구하기(SQL, SELECT) (0) | 2024.07.27 |
---|---|
[프로그래머스] 베스트앨범 (정렬/Comparator) (0) | 2024.07.25 |
[프로그래머스] 두 개 뽑아서 정렬하기 (0) | 2024.07.25 |
[백준] 01002. 터렛 (1) | 2024.06.15 |
[프로그래머스] 12914. 멀리 뛰기 & 12945. 피보나치 수 (0) | 2023.05.22 |