programmers_할인 행사_java

2023. 4. 7. 17:40Algorithm/Programmers

728x90

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;
        
        Map<String, Integer> indexMap = new HashMap<>();
        
        int wantLength = want.length;
        for(int i = 0; i<wantLength; i++){
            indexMap.put(want[i], i);
        }
        
        int discountLength = discount.length;
        for(int i = 0; i<discountLength; i++){
            if(indexMap.containsKey(discount[i])){ // 해당 값을 가지고 있는 경우
                number[indexMap.get(discount[i])]--; // 값 하나 줄이기  
            }
            
            if(i >= 9){   
                boolean isSuccess = true;
                for(int j = 0; j<wantLength; j++){
                    if(number[j]>0){
                        isSuccess = false;
                        break;
                    }
                }
                if(isSuccess) answer++;
                if(indexMap.containsKey(discount[i-9])){ // 해당 값을 가지고 있는 경우
                    number[indexMap.get(discount[i-9])]++; // 값 하나 늘리기
                }
            }
        }
        
        return answer;
    }
}

'Algorithm > Programmers' 카테고리의 다른 글

programmers_[3차] n진수 게임_java  (0) 2023.04.10
programmers_[3차] 압축_java  (0) 2023.04.10
programmers_피로도_java  (0) 2023.04.06
programmers_타겟 넘버_java  (0) 2023.04.06
programmers_타겟 넘버_java  (0) 2023.04.06