๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Coding Test/Algorithms

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ• ์ธํ–‰์‚ฌ/Java - Lv.2

by The Future Engineer, Lucy 2024. 10. 21.
728x90
๋ฐ˜์‘ํ˜•

โ“๋ฌธ์ œ

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 141 MB, ์‹œ๊ฐ„: 111.64 ms

๋ฌธ์ œ ์„ค๋ช…

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

์ž…์ถœ๋ ฅ ์˜ˆ

want number discount result
["banana", "apple", "rice", "pork", "pot"] [3, 2, 2, 2, 1] ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"] 3
["apple"] [10] ["banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana"] 0

โœ๐Ÿปํ’€์ด

want = ["banana", "apple", "rice", "pork", "pot"], number = [3, 2, 2, 2, 1]
์›ํ•˜๋Š” ์ œํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰์„ ํ•ด์‹œ๋งต์œผ๋กœ ๋งŒ๋“ ๋‹ค.

discount = ["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork",
"rice", "pot", "banana", "apple", "banana"]
1~10, 2~11, 3~12, 4~13, 5~14 ์ค‘ ์—ฐ์† 10์ผ ๋™์•ˆ ์›ํ•˜๋Š” ์ œํ’ˆ ์ „๋ถ€๋ฅผ ํ• ์ธํ•ด์„œ ์‚ด ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
1~10์ผ์„ ๋ณด๋ฉด ์Œ€๊ณผ ์‚ฌ๊ณผ ์ด์™ธ์—๋Š” ํ• ์ธํ•ด์„œ ์‚ด ์ˆ˜ ์—†๋‹ค.
2~11์„ ๋ณด๋ฉด ๋ฐ”๋‚˜๋‚˜๋งŒ ์‚ด ์ˆ˜ ์žˆ๋‹ค.
3~12์ผ์„ ๋ณด๋ฉด ๋ชจ๋‘ ํ• ์ธํ•ด์„œ ์‚ด ์ˆ˜ ์žˆ๋‹ค.
์ด๋Ÿฐ ์‹์œผ๋กœ ํ• ์ธํ•ด์„œ ์‚ด ์ˆ˜ ์žˆ๋Š” ์ œํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ๊ณ„์‚ฐํ–ˆ์„ ๋•Œ,
์›ํ•˜๋Š” ์ œํ’ˆ์˜ ์ˆ˜๋Ÿ‰๊ณผ ํ• ์ธํ•ด์„œ ์‚ด ์ œํ’ˆ์˜ ์ˆ˜๋Ÿ‰์ด ๋งž๋‹ค๋ฉด answer + 1์„ ํ•œ๋‹ค.

๐Ÿ’ป์ฝ”๋“œ

import java.util.*;

class Solution {
    public int solution(String[] want, int[] number, String[] discount) {
        int answer = 0;
        int days = 10;
        HashMap<String, Integer> m = new HashMap<>();
        for(int i = 0; i < want.length; i++)
            m.put(want[i], number[i]);

        for(int i = 0; i < discount.length - 9; i++){
            HashMap<String, Integer> d = new HashMap<>();
            for(int j = 0; j < 10; j++){
                d.put(discount[i + j], d.getOrDefault(discount[i + j], 0) + 1);
            }

            boolean sign = true;
            for(String k : m.keySet())
                if(d.get(k) != m.get(k)){
                    sign = false;
                    break;
                }

            answer += sign ? 1 : 0;
        }

        return answer;
    }
}
728x90
๋ฐ˜์‘ํ˜•