주녘공부일지

[프로그래머스 C#] Lv.2 할인 행사 본문

Programmers - C#/CodingTest Lv.2

[프로그래머스 C#] Lv.2 할인 행사

주녘 2023. 11. 12. 21:20
728x90

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

 

프로그래머스

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

programmers.co.kr

1. 정답코드 및 핵심 아이디어, 유의사항

원하는 제품을 모두 할인상품으로 구매할 수 있는 날의 숫자를 구하는 문제

- 원하는 제품들의 개수의 총합과 연속으로 일치해야 하는 날짜의 수가 같으므로, 모든 제품을 10일의 기간동안 원하는 개수만큼 딱 맞게 구매해야 함

- Dictionary<key, value> 활용 ( key : 원하는 제품, value : 원하는 제품 수량 )

https://godgjwnsgur7.tistory.com/46

 

[C#] 자료구조 ( 제네릭 컬렉션 )

자료구조(Data Structure)란? 데이터를 효율적으로 접근하고 조작할 수 있게 데이터 구조를 만들어 관리하는 것 - Collections은 C#에서 지원하는 자료구조 클래스 using System.Collections.Generic; 제네릭 컬렉

godgjwnsgur7.tistory.com

주석참조

    using System;
    using System.Collections.Generic;

    public class Solution
    {
        public int solution(string[] want, int[] number, string[] discount)
        {
            int answer = 0;
            // 과일이름 want, 과일 수 number를 담을 딕셔너리
            Dictionary<string, int> dict = new Dictionary<string, int>();

            // 0번 인덱스부터 10개씩 확인
            for (int i = 0; i < discount.Length - 9; i++)
            {
                // 딕셔너리 값 초기화
                for (int j = 0; j < want.Length; j++)
                    dict.Add(want[j], number[j]);

                // 원하는 과일 구매
                for (int j = i; j < i + 10; j++)
                    if (dict.ContainsKey(discount[j]))
                        dict[discount[j]]--;

                // 원하는 모든 과일을 다 구매헀는지 판단
                int maxValue = 0;
                foreach (int dictValue in dict.Values)
                    if (dictValue > maxValue)
                        maxValue = dictValue;

                // 원하는 모든 과일을 구매했다면 10일 연속 일치
                if (maxValue == 0)
                    answer++;

                dict.Clear();
            }

            return answer;
        }
    }
728x90