본문 바로가기

Programmers

[프로그래머스][JAVA]Lv. 2 - 할인 행사

https://school.programmers.co.kr/learn/courses/30/lessons/131127

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

10일 동안 원하는 제품과 수량이 할인하는 날짜가 일치하는 경우를 찾는다

그런 경우가 몇일인지를 찾는 문제

HashMap<String, Integer> map = new HashMap<>();
HashMap<String, Integer> answer_map = new HashMap<>();
for (int i = 0; i < want.length; i++) {
answer_map.put(want[i], number[i]);
}
for (int i = 0; i < 10; i++) {
if (!map.containsKey(discount[i])) {
map.put(discount[i], 1);
} else {
map.put(discount[i], map.get(discount[i])+1);
}
}

HashMap을 이용, 먼저 원하는 물건 리스트와 수량을 answer_map에 넣어주었다

그리고 할인 목록인 discount의 1~10일까지의 물건들을 map에 물건, 수량식으로 넣어주었다

private int getAnswer(String[] want, int answer, HashMap<String, Integer> map, HashMap<String, Integer> answer_map) {
for (int i = 0; i < want.length; i++) {
if (!Objects.equals(map.get(want[i]), answer_map.get(want[i]))) break;
else if (i == want.length-1 && Objects.equals(map.get(want[i]),answer_map.get(want[i]))) {
answer++;
}
}
return answer;
}

그다음 원하는 물건의 수량과 할인하는 수량이 같은지를, 그리고 모든 물건이 할인 가능하다면 조건에 맞으므로 answer를 증가시켰다

private void putOnMap(String[] discount, HashMap<String, Integer> map, int n) {
if (!map.containsKey(discount[n])) {
map.put(discount[n], 1);
map.put(discount[n-10], map.get(discount[n-10])-1);
return;
} else {
map.put(discount[n], map.get(discount[n])+1);
map.put(discount[n-10], map.get(discount[n-10])-1);
return;
}
}

그런 다음 11일 차의 할인하는 물건을 map에 넣고 1일의 물건을 빼주는 식으로 map을 수정하였다

while (n != discount.length) {
putOnMap(discount, map, n);
answer = getAnswer(want, answer, map, answer_map);
n++;
}

그렇게 계속 반복하여 조건에 맞는 경우 answer를 증가시키고 정답을 출력하였다

 

아래는 전체 코드

import java.util.HashMap;
import java.util.Objects;
class Solution {
public int solution(String[] want, int[] number, String[] discount) {
int answer = 0;
HashMap<String, Integer> map = new HashMap<>();
HashMap<String, Integer> answer_map = new HashMap<>();
for (int i = 0; i < want.length; i++) {
answer_map.put(want[i], number[i]);
}
for (int i = 0; i < 10; i++) {
if (!map.containsKey(discount[i])) {
map.put(discount[i], 1);
} else {
map.put(discount[i], map.get(discount[i])+1);
}
}
answer = getAnswer(want, answer, map, answer_map);
int n = 10;
while (n != discount.length) {
putOnMap(discount, map, n);
answer = getAnswer(want, answer, map, answer_map);
n++;
}
return answer;
}
private int getAnswer(String[] want, int answer, HashMap<String, Integer> map, HashMap<String, Integer> answer_map) {
for (int i = 0; i < want.length; i++) {
if (!Objects.equals(map.get(want[i]), answer_map.get(want[i]))) break;
else if (i == want.length-1 && Objects.equals(map.get(want[i]),answer_map.get(want[i]))) {
answer++;
}
}
return answer;
}
private void putOnMap(String[] discount, HashMap<String, Integer> map, int n) {
if (!map.containsKey(discount[n])) {
map.put(discount[n], 1);
map.put(discount[n-10], map.get(discount[n-10])-1);
return;
} else {
map.put(discount[n], map.get(discount[n])+1);
map.put(discount[n-10], map.get(discount[n-10])-1);
return;
}
}
}