๐Ÿ”ฅ[99ํด๋Ÿฝ ์ฝ”ํ…Œ ์Šคํ„ฐ๋””] 39์ผ์ฐจ TIL - ๊ด‘๋ฌผ ์บ๊ธฐ

HOONSSACยท2024๋…„ 8์›” 29์ผ
1

99Club Coding Test Study

๋ชฉ๋ก ๋ณด๊ธฐ
39/41
post-thumbnail

โณ๋ฌธ์ œ

๋งˆ์ธ์€ ๊ณก๊ดญ์ด๋กœ ๊ด‘์‚ฐ์—์„œ ๊ด‘์„์„ ์บ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ธ์€ ๋‹ค์ด์•„๋ชฌ๋“œ ๊ณก๊ดญ์ด, ์ฒ  ๊ณก๊ดญ์ด, ๋Œ ๊ณก๊ดญ์ด๋ฅผ ๊ฐ๊ฐ 0๊ฐœ์—์„œ 5๊ฐœ๊นŒ์ง€ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ, ๊ณก๊ดญ์ด๋กœ ๊ด‘๋ฌผ์„ ์บ˜ ๋•Œ๋Š” ํ”ผ๋กœ๋„๊ฐ€ ์†Œ๋ชจ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ณก๊ดญ์ด๋กœ ๊ด‘๋ฌผ์„ ์บ˜ ๋•Œ์˜ ํ”ผ๋กœ๋„๋Š” ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ฒ  ๊ณก๊ดญ์ด๋Š” ๋‹ค์ด์•„๋ชฌ๋“œ๋ฅผ ์บ˜ ๋•Œ ํ”ผ๋กœ๋„ 5๊ฐ€ ์†Œ๋ชจ๋˜๋ฉฐ, ์ฒ ๊ณผ ๋Œ์„ ์บ˜๋•Œ๋Š” ํ”ผ๋กœ๋„๊ฐ€ 1์”ฉ ์†Œ๋ชจ๋ฉ๋‹ˆ๋‹ค. ๊ฐ ๊ณก๊ดญ์ด๋Š” ์ข…๋ฅ˜์— ์ƒ๊ด€์—†์ด ๊ด‘๋ฌผ 5๊ฐœ๋ฅผ ์บ” ํ›„์—๋Š” ๋” ์ด์ƒ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋งˆ์ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์„ ์ง€ํ‚ค๋ฉด์„œ ์ตœ์†Œํ•œ์˜ ํ”ผ๋กœ๋„๋กœ ๊ด‘๋ฌผ์„ ์บ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

  • ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ณก๊ดญ์ด์ค‘ ์•„๋ฌด๊ฑฐ๋‚˜ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•ด ๊ด‘๋ฌผ์„ ์บก๋‹ˆ๋‹ค.
  • ํ•œ ๋ฒˆ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ•œ ๊ณก๊ดญ์ด๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • ๊ด‘๋ฌผ์€ ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ๋งŒ ์บ˜ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ด‘์‚ฐ์— ์žˆ๋Š” ๋ชจ๋“  ๊ด‘๋ฌผ์„ ์บ๊ฑฐ๋‚˜, ๋” ์‚ฌ์šฉํ•  ๊ณก๊ดญ์ด๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ ๊ด‘๋ฌผ์„ ์บก๋‹ˆ๋‹ค.

์ฆ‰, ๊ณก๊ดญ์ด๋ฅผ ํ•˜๋‚˜ ์„ ํƒํ•ด์„œ ๊ด‘๋ฌผ 5๊ฐœ๋ฅผ ์—ฐ์†์œผ๋กœ ์บ๊ณ , ๋‹ค์Œ ๊ณก๊ดญ์ด๋ฅผ ์„ ํƒํ•ด์„œ ๊ด‘๋ฌผ 5๊ฐœ๋ฅผ ์—ฐ์†์œผ๋กœ ์บ๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ, ๋” ์‚ฌ์šฉํ•  ๊ณก๊ดญ์ด๊ฐ€ ์—†๊ฑฐ๋‚˜ ๊ด‘์‚ฐ์— ์žˆ๋Š” ๋ชจ๋“  ๊ด‘๋ฌผ์„ ์บ˜ ๋•Œ๊นŒ์ง€ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋งˆ์ธ์ด ๊ฐ–๊ณ  ์žˆ๋Š” ๊ณก๊ดญ์ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด picks์™€ ๊ด‘๋ฌผ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด minerals๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋งˆ์ธ์ด ์ž‘์—…์„ ๋๋‚ด๊ธฐ๊นŒ์ง€ ํ•„์š”ํ•œ ์ตœ์†Œํ•œ์˜ ํ”ผ๋กœ๋„๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


โœ๏ธํ’€์ด

ํ”ผ๋กœ๋„์˜ ๋ฒ”์œ„์™€ ๊ณก๊ดญ์ด์˜ ์ •๋ณด๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๊ณ , ์ตœ์„ ์˜ ์„ ํƒ์„ ๊ตฌํ•ด์•ผ ํ•˜๊ธฐ ๋–„๋ฌธ์— ๊ทธ๋ฆฌ๋”” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ์— ์ ‘๊ทผ์„ ํ•˜์˜€๋‹ค.

๊ณก๊ดญ์ด ํ•˜๋‚˜๋กœ ๊ด‘๋ฌผ 5๊ฐœ๋ฅผ ์—ฐ์†์œผ๋กœ ์บ๊ธฐ ๋•Œ๋ฌธ์—, ๊ด‘๋ฌผ์„ 5๊ฐœ์”ฉ ๊ทธ๋ฃน์ง€์–ด ๊ฐ ๊ทธ๋ฃน์˜ ํ”ผ๋กœ๋„ ์ดํ•ฉ์„ ๊ณ„์‚ฐํ•ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„, ๊ฐ€์žฅ ํ”ผ๋กœ๋„๊ฐ€ ๋†’์€ ๊ทธ๋ฃน๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด์•˜๋‹ค.

๊ด‘๋ฌผ ๊ทธ๋ฃนํ™” ๋ฐ ํ”ผ๋กœ๋„ ๊ณ„์‚ฐ

for (int i = 0; i < Math.min(totalPicks * 5, totalMinerals); i += 5) {
	int sum = 0; // ํ˜„์žฌ ๊ทธ๋ฃน์˜ ํ”ผ๋กœ๋„
	int damage = 0; // ๊ฐ ๊ด‘๋ฌผ์— ๋Œ€ํ•œ ํ”ผ๋กœ๋„

	for (int j = i; j < Math.min(i + 5, totalMinerals); j++) {
		switch (minerals[j]) {
			case "diamond":
				damage = 25;
				break;
			case "iron":
				damage = 5;
				break;
			case "stone":
				damage = 1;
				break;
		}
        sum += damage; // ํ”ผ๋กœ๋„ ์ดํ•ฉ ์—…๋ฐ์ดํŠธ
        mineralsGroup[i / 5][j % 5 + 1] = damage; // ๊ฐ ๊ด‘๋ฌผ์˜ ํ”ผ๋กœ๋„ ์ €์žฅ
    }
	mineralsGroup[i / 5][0] = sum; // ํ˜„์žฌ ๊ทธ๋ฃน์˜ ์ด ํ”ผ๋กœ๋„ ์ €์žฅ
}

์ฃผ์–ด์ง„ minerals ๋ฐฐ์—ด์„ 5๊ฐœ์”ฉ ๊ทธ๋ฃน์ง€์–ด ํ”ผ๋กœ๋„๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๋Š”๋ฐ,
์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ ์ „์ฒด ๊ณก๊ดญ์ด์˜ ๊ฐœ์ˆ˜์— 5๋ฅผ ๊ณฑํ•œ ์ˆ˜์™€ minerals์˜ ์ „์ฒด ๊ธธ์ด ์ค‘ ๋” ์ž‘์€ ๊ฐ’์ด ์บ˜ ์ˆ˜ ์žˆ๋Š” ๊ด‘๋ฌผ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, ๋งŒ์•ฝ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ณก๊ดญ์ด๊ฐ€ ๋งŽ๋”๋ผ๋„, ์‹ค์ œ๋กœ ์บ์•ผ ํ•  ๊ด‘๋ฌผ์ด ์—†๋‹ค๋ฉด ๋ฃจํ”„๋Š” ๊ทธ๋งŒํผ๋งŒ ๋ฐ˜๋ณต๋˜์–ด์•ผ ํ•œ๋‹ค.

switch๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ด‘๋ฌผ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ํ”ผ๋กœ๋„๋ฅผ ์„ค์ •ํ•˜๊ณ , sum์— ํ”ผ๋กœ๋„๋ฅผ ๋ˆ„์ ํ•ด mineralsGroup[i / 5][0]์— ํ˜„์žฌ ๊ทธ๋ฃน์˜ ์ด ํ”ผ๋กœ๋„๋ฅผ ์ €์žฅํ•˜๊ณ , mineralsGroup[i / 5][j % 5 + 1]์— ๊ฐ ๊ด‘๋ฌผ์˜ ํ”ผ๋กœ๋„๋ฅผ ์ €์žฅํ•ด์ค€๋‹ค.

ํ”ผ๋กœ๋„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ

Arrays.sort(mineralsGroup, (o1, o2) -> o2[0] - o1[0]);

mineralsGroup์„ ์ด ํ”ผ๋กœ๋„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ€์žฅ ํ”ผ๋กœ๋„๊ฐ€ ๋†’์€ ๊ทธ๋ฃน๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ณก๊ดญ์ด ์‚ฌ์šฉ ๋ฐ ํ”ผ๋กœ๋„ ๊ณ„์‚ฐ

for (int i = 0; i < totalPicks; i++) {
      int[] temp = mineralsGroup[i];
      String pick = "";
            
      if (dia > 0) {
           dia -= 1;
           pick = "diamond";
      }
      else if (iron > 0) {
           iron -= 1;
           pick = "iron";
      }
      else if (stone > 0) {
           stone -= 1;
           pick = "stone";
      }

์ด์ œ ๊ฐ ๊ณก๊ดญ์ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ณก๊ดญ์ด ์ค‘์—์„œ ๋‹ค์ด์•„๋ชฌ๋“œ ๊ณก๊ดญ์ด๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ณ , ๊ทธ๋‹ค์Œ์œผ๋กœ ์ฒ  ๊ณก๊ดญ์ด, ๋Œ ๊ณก๊ดญ์ด ์ˆœ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
์‚ฌ์šฉํ•œ ๊ณก๊ดญ์ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๊ณ , ์‚ฌ์šฉํ•œ ๊ณก๊ดญ์ด๋ฅผ pick์— ์ €์žฅํ•œ๋‹ค.

๊ด‘๋ฌผ๋ณ„ ํ”ผ๋กœ๋„ ๊ณ„์‚ฐ

for (int j = 1; j < 6; j++) {
	switch (pick) {
         case "diamond":
          	answer += (int) Math.ceil(temp[j] / 25.0);
          	break;
         case "iron":
            answer += (int) Math.ceil(temp[j] / 5.0);
            break;
         case "stone":
            answer += temp[j];
            break;
         }
    }

์‚ฌ์šฉํ•œ ๊ณก๊ดญ์ด์— ๋”ฐ๋ผ ๊ฐ ๊ด‘๋ฌผ์˜ ํ”ผ๋กœ๋„๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
Math.ceil์„ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ๊ณก๊ดญ์ด์˜ ํŠน์„ฑ์— ๋”ฐ๋ผ ํ”ผ๋กœ๋„๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๐Ÿ‘พ์ „์ฒด ์ฝ”๋“œ

import java.util.*;

class Solution {
    public int solution(int[] picks, String[] minerals) {
        int answer = 0;
        
        int dia = picks[0]; // ๋‹ค์ด์•„ ๊ณก๊ดญ์ด ๊ฐœ์ˆ˜
        int iron = picks[1]; // ์ฒ  ๊ณก๊ดญ์ด ๊ฐœ์ˆ˜
        int stone = picks[2]; // ๋Œ ๊ณก๊ดญ์ด ๊ฐœ์ˆ˜

        // ์ „์ฒด ๊ณก๊ดญ์ด์˜ ๊ฐœ์ˆ˜
        int totalPicks = dia + iron + stone;

        // ๊ด‘์„ ์ด ๊ฐœ์ˆ˜
        int totalMinerals = minerals.length;

        // 5๊ฐœ์”ฉ ๊ทธ๋ฃนํ™”ํ•ด์„œ ํ”ผ๋กœ๋„ ๊ณ„์‚ฐ
        int[][] mineralsGroup = new int[totalPicks][6];

        for (int i = 0; i < Math.min(totalPicks * 5, totalMinerals); i += 5) {
            int sum = 0; // ํ˜„์žฌ ๊ทธ๋ฃน์˜ ํ”ผ๋กœ๋„
            int damage = 0; // ๊ฐ ๊ด‘๋ฌผ์— ๋Œ€ํ•œ ํ”ผ๋กœ๋„

            for (int j = i; j < Math.min(i + 5, totalMinerals); j++) {
                switch (minerals[j]) {
                    case "diamond":
                        damage = 25;
                        break;
                    case "iron":
                        damage = 5;
                        break;
                    case "stone":
                        damage = 1;
                        break;
                }
                sum += damage; // ํ”ผ๋กœ๋„ ์ดํ•ฉ ์—…๋ฐ์ดํŠธ
                mineralsGroup[i / 5][j % 5 + 1] = damage; // ๊ฐ ๊ด‘๋ฌผ์˜ ํ”ผ๋กœ๋„ ์ €์žฅ
            }
            mineralsGroup[i / 5][0] = sum; // ํ˜„์žฌ ๊ทธ๋ฃน์˜ ์ด ํ”ผ๋กœ๋„ ์ €์žฅ
        }

        // ํ”ผ๋กœ๋„ ํ•ฉ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
        Arrays.sort(mineralsGroup, (o1, o2) -> o2[0] - o1[0]);

        for (int i = 0; i < totalPicks; i++) {
            int[] temp = mineralsGroup[i];
            String pick = "";
            
            // ์‚ฌ์šฉํ•œ ๊ณก๊ดญ์ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์ด๊ณ , ์–ด๋–ค ๊ณก๊ดญ์ด๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š” ์ง€ ์ €์žฅ
            if (dia > 0) {
                dia -= 1;
                pick = "diamond";
            }
            else if (iron > 0) {
                iron -= 1;
                pick = "iron";
            }
            else if (stone > 0) {
                stone -= 1;
                pick = "stone";
            }

            // ๊ด‘๋ฌผ๋ณ„ ํ”ผ๋กœ๋„ ๊ณ„์‚ฐ
            for (int j = 1; j < 6; j++) {
                switch (pick) {
                    case "diamond":
                        answer += (int) Math.ceil(temp[j] / 25.0);
                        break;
                    case "iron":
                        answer += (int) Math.ceil(temp[j] / 5.0);
                        break;
                    case "stone":
                        answer += temp[j];
                        break;
                }
            }
        }        
        return answer;
    }
}

๐Ÿ”—๋ฌธ์ œ ๋งํฌ
๐Ÿ’ปRepository

profile
ํ›ˆ์‹น์˜ ๊ฐœ๋ฐœ์—ฌํ–‰

0๊ฐœ์˜ ๋Œ“๊ธ€