https://school.programmers.co.kr/learn/courses/30/lessons/131127
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
문제 설명
XYZ 마트는 일정한 금액을 지불하면 10일 동안 회원 자격을 부여합니다. XYZ 마트에서는 회원을 대상으로 매일 한 가지 제품을 할인하는 행사를 합니다. 할인하는 제품은 하루에 하나씩만 구매할 수 있습니다. 알뜰한 정현이는 자신이 원하는 제품과 수량이 할인하는 날짜와 10일 연속으로 일치할 경우에 맞춰서 회원가입을 하려 합니다.
예를 들어, 정현이가 원하는 제품이 바나나 3개, 사과 2개, 쌀 2개, 돼지고기 2개, 냄비 1개이며, XYZ 마트에서 14일간 회원을 대상으로 할인하는 제품이 날짜 순서대로 치킨, 사과, 사과, 바나나, 쌀, 사과, 돼지고기, 바나나, 돼지고기, 쌀, 냄비, 바나나, 사과, 바나나인 경우에 대해 알아봅시다. 첫째 날부터 열흘 간에는 냄비가 할인하지 않기 때문에 첫째 날에는 회원가입을 하지 않습니다. 둘째 날부터 열흘 간에는 바나나를 원하는 만큼 할인구매할 수 없기 때문에 둘째 날에도 회원가입을 하지 않습니다. 셋째 날, 넷째 날, 다섯째 날부터 각각 열흘은 원하는 제품과 수량이 일치하기 때문에 셋 중 하루에 회원가입을 하려 합니다.
정현이가 원하는 제품을 나타내는 문자열 배열 want와 정현이가 원하는 제품의 수량을 나타내는 정수 배열 number, XYZ 마트에서 할인하는 제품을 나타내는 문자열 배열 discount가 주어졌을 때, 회원등록시 정현이가 원하는 제품을 모두 할인 받을 수 있는 회원등록 날짜의 총 일수를 return 하는 solution 함수를 완성하시오. 가능한 날이 없으면 0을 return 합니다.
제한사항
- 1 ≤ want의 길이 = number의 길이 ≤ 10
- 1 ≤ number의 원소 ≤ 10
- number[i]는 want[i]의 수량을 의미하며, number의 원소의 합은 10입니다.
- 10 ≤ discount의 길이 ≤ 100,000
- want와 discount의 원소들은 알파벳 소문자로 이루어진 문자열입니다.
- 1 ≤ want의 원소의 길이, discount의 원소의 길이 ≤ 12
입출력 예
풀이
슬라이딩 윈도우 방식
for (int i = 0; i <= discount.length - 10; i++) { ... }
- 할인 목록 `discount` 배열에서 길이가 10인 구간마다 탐색하도록 시작 인덱스 설정
for (int i = 0; i <= discount.length - 10; i++) {
HashMap<String, Integer> currentQuantity = new HashMap<>();
// 10일 단위로 탐색
for (int j = i; j < i + 10; j++) {
currentQuantity.put(discount[j],
currentQuantity.getOrDefault(discount[j], 0) + 1);
}
...
}
- 10일간의 할인 목록을 `currentQuantity` HashMap에 초기화
- Key: 상품
- Value: 개수
// 10일 연속 부분배열 찾기
boolean isSatisfied = true;
for (String product : wantQuantity.keySet()) {
// 현재 10일 단위의 HashMap(currentQuantity)에 저장된 개수가
// 원하는 할인 상품 개수(wantQuantity)보다 작으면
// 다음 10일을 탐색하도록 break
if (currentQuantity.getOrDefault(product, 0) < wantQuantity.get(product)) {
isSatisfied = false;
break;
}
}
if (isSatisfied) answer++;
- 조건 확인
- 10일 동안 상품 목록이 원하는 상품 수량을 만족하는지 확인
- 만족한다면, answer ++
풀이 코드
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
String[] want = {"banana", "apple", "rice", "pork", "pot"};
int[] number = {3, 2, 2, 2, 1};
String[] discount = {"chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"};
int answer = 0;
HashMap<String, Integer> wantQuantity = new HashMap<>();
for (int i = 0; i < want.length; i++) {
wantQuantity.put(want[i], number[i]);
}
for (int i = 0; i <= discount.length - 10; i++) {
HashMap<String, Integer> currentQuantity = new HashMap<>();
// 10일 단위로 탐색
for (int j = i; j < i + 10; j++) {
currentQuantity.put(discount[j],
currentQuantity.getOrDefault(discount[j], 0) + 1);
}
// 10일 연속 부분배열 찾기
boolean isSatisfied = true;
for (String product : wantQuantity.keySet()) {
// 현재 10일 단위의 HashMap(currentQuantity)에 저장된 개수가
// 원하는 할인 상품 개수(wantQuantity)보다 작으면
// 다음 10일을 탐색하도록 break
if (currentQuantity.getOrDefault(product, 0) < wantQuantity.get(product)) {
isSatisfied = false;
break;
}
}
if (isSatisfied) answer++;
}
System.out.println(answer);
}
}
제출
import java.util.HashMap;
class Solution {
public int solution(String[] want, int[] number, String[] discount) {
int answer = 0;
HashMap<String, Integer> wantQuantity = new HashMap<>();
for (int i = 0; i < want.length; i++) {
wantQuantity.put(want[i], number[i]);
}
for (int i = 0; i <= discount.length - 10; i++) {
HashMap<String, Integer> currentQuantity = new HashMap<>();
// 10일 단위로 탐색
for (int j = i; j < i + 10; j++) {
currentQuantity.put(discount[j],
currentQuantity.getOrDefault(discount[j], 0) + 1);
}
// 10일 연속 부분배열 찾기
boolean isSatisfied = true;
for (String product : wantQuantity.keySet()) {
// 현재 10일 단위의 HashMap(currentQuantity)에 저장된 개수가
// 원하는 할인 상품 개수(wantQuantity)보다 작으면
// 다음 10일을 탐색하도록 break
if (currentQuantity.getOrDefault(product, 0) < wantQuantity.get(product)) {
isSatisfied = false;
break;
}
}
if (isSatisfied) answer++;
}
return answer;
}
}
'Algorithm Solving > Java' 카테고리의 다른 글
[programmers] Java Lv.2 - 기능개발 (0) | 2025.01.27 |
---|---|
[programmers] Java Lv.2 - 의상 (0) | 2025.01.22 |
[programmers] Java Lv.2 - 행렬의 곱셈 (1) | 2025.01.16 |
[programmers] Java Lv.2 - n^2 배열 자르기 (0) | 2025.01.15 |
[programmers] Java Lv.2 - H-Index (0) | 2025.01.14 |