Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

img_4.png

img_5.png

์„ค๋ช…

์„ธ ์ข…๋ฅ˜์˜ ๊ณก๊ดญ์ด๊ฐ€ ์žˆ๊ณ , ๊ด‘๋ฌผ์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๊ฐ ๊ณก๊ดญ์ด์˜ ํ”ผ๋กœ๋„๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์ฑ…์ •๋˜์–ด ์žˆ๋‹ค. ๊ฐ ๊ณก๊ดญ์ด์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋ฐฐ์—ด๋กœ ์ฃผ์–ด์ง€๊ณ  ๊ด‘๋ฌผ์˜ ์ข…๋ฅ˜๊ฐ€ ๋ฐฐ์—ด๋กœ ์ฃผ์–ด์งˆ๋•Œ ๊ณก๊ดญ์ด๋ฅผ ๋ชจ๋‘ ์†Œ๋ชจํ•˜๊ฑฐ๋‚˜, ๊ด‘๋ฌผ์„ ๋ชจ๋‘ ์บ๋Š” ๊ฒฝ์šฐ์˜ ํ”ผ๋กœ๋„ ์ตœ์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ

ํ’€์ด

  1. ๊ทธ๋ฃนํ™” : ํ•œ ๊ณก๊ดญ์ด ๋‹น ์–ด๋–ค ๊ด‘๋ฌผ์ด ๋˜์—ˆ๋“ ๊ฐ„์— 5๋ฒˆ๋ฐ–์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๊ด‘๋ฌผ์„ 5๊ฐœ์”ฉ ๊ทธ๋ฃน์„ ๋‚˜๋ˆ  ๊ฐ ๊ทธ๋ฃน์—์„œ์˜ ๊ฐ€์žฅ ํšจ์œจ์ ์ธ(ํ”ผ๋กœ๋„๊ฐ€ ๋‚ฎ์€) ๊ณก๊ดญ์ด๋ฅผ ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฒซ๋ฒˆ์งธ ์˜ˆ์‹œ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ทธ๋ฃนํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค.
    1
    2
    
    group1 = ["diamond", "diamond", "diamond", "iron", "iron"]
    group2 = ["diamond", "iron", "stone"]
    
  2. ๊ฐ€์ค‘์น˜ ๊ณ„์‚ฐ : ์ด์ œ ์ € ๊ทธ๋ฃน๋“ค์„ ์†Œํ™”ํ•  ๊ณก๊ดญ์ด๋งŒ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ์„ ํƒ๊ธฐ์ค€์€ ๊ด‘๋ฌผ ๊ฐ€์ค‘์น˜๊ฐ€ ์ œ์ผ ํฐ ๊ทธ๋ฃน์— ๊ฐ•๋ ฅํ•œ ๊ณก๊ดญ์ด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ ๊ทธ๋ฃน์˜ ๊ด‘๋ฌผ ๊ฐ€์ค‘์น˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง€๋Š” ๊ณก๊ดญ์ด ํ”ผ๋กœ๋„๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“  ๋’ค, ๊ทธ๋ฃน๋งˆ๋‹ค ๋‹ค์ด์•„๋ชฌ๋“œ ๊ณก๊ดญ์ด๊ฐ€ ์†Œ๋ชจํ•˜๋Š” ํ”ผ๋กœ๋„, ์ฒ  ๊ณก๊ดญ์ด๊ฐ€ ์†Œ๋ชจํ•˜๋Š” ํ”ผ๋กœ๋„, ๋Œ ๊ณก๊ดญ์ด๊ฐ€ ์†Œ๋ชจํ•˜๋Š” ํ”ผ๋กœ๋„๋ฅผ ํ•ฉ์‚ฐํ•ด sum ์— ์ €์žฅ์‹œ์ผœ ๋‘”๋‹ค. 5๊ฐœ์”ฉ ๋ช‡๊ฐœ์˜ ๊ทธ๋ฃน์ด ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด 5๋กœ ๋‚˜๋ˆˆ ๋ชซ ๋งŒํผ ๋ฐ˜๋ณต์„ ํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๊ด‘๋ฌผ๋“ค์— ๋Œ€ํ•ด์„œ๋„ ๊ฐ€์ค‘์น˜๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์ €์žฅํ•œ๋‹ค.

  3. ์ตœ์†Œ ํ”ผ๋กœ๋„ ์ถœ๋ ฅ : ๊ทธ๋ฃน ๋ณ„ ์ด ํ”ผ๋กœ๋„(๊ฐ€์ค‘์น˜) ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” sum ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋Œ๋ฆฌ๋ฉด์„œ ๊ฐ ๊ทธ๋ฃน์— ๊ณก๊ดญ์ด๋ฅผ ํ• ๋‹น ํ•ด์•ผ ํ•œ๋‹ค.
    • ๊ฐ€์žฅ ํฐ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ€์ง„ ๊ทธ๋ฃน์— ๋‹ค์ด์•„๋ชฌ๋“œ ๊ณก๊ดญ์ด๋ถ€ํ„ฐ ํ• ๋‹นํ•˜๊ธฐ ์œ„ํ•ด sum ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ ๋’ค ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฐ๋‹ค.
  4. ๋ฌธ์ œ ์ œํ•œ ์‚ฌํ•ญ ํ™•์ธ : ์ฒ˜์Œ์— int length = minerals.length ๋กœ ์ œ์ถœํ–ˆ์—ˆ๋Š”๋ฐ 8๋ฒˆ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. ๋ฌธ์ œ์— ๋” ์‚ฌ์šฉํ•  ๊ณก๊ดญ์ด๊ฐ€ ์—†๊ฑฐ๋‚˜ ๊ด‘์‚ฐ์— ์žˆ๋Š” ๋ชจ๋“  ๊ด‘๋ฌผ์„ ์บ˜ ๋•Œ๊นŒ์ง€ ๊ณผ์ •์„ ๋ฐ˜๋ณต ํ•˜๋ผ๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ด‘๋ฌผ์˜ ๊ฐฏ์ˆ˜ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ˜„์žฌ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณก๊ดญ์ด๋กœ ์บ˜ ์ˆ˜ ์žˆ๋Š” ๊ด‘๋ฌผ์˜ ๊ฐฏ์ˆ˜๋„ ๊ฐ™์ด ์ƒ๊ฐํ•ด์•ผ ํ•œ๋‹ค.
    • ๊ด‘๋ฌผ์„ ๋ชจ๋‘ ์บ” ๊ฒฝ์šฐ : minerals.length
    • ๋ชจ๋“  ๊ณก๊ดญ์ด๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ : (picks[0] + picks[1] + picks[2]) * 5
    • ์œ„ ๋‘๊ฐ€์ง€ ์ผ€์ด์Šค ์ค‘ ๋” ์ž‘์€ ์ˆ˜ ๋งŒํผ๋ฐ–์— ์ž‘์—…์„ ํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ Math.min(minerals.length, (picks[0] + picks[1] + picks[2]) * 5) ๋กœ length ๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Solution {
  static class Pick implements Comparable<Pick> {
    int diamondPick;
    int ironPick;
    int stonePick;

    public Pick(int diamondPick, int ironPick, int stonePick) {
      this.diamondPick = diamondPick;
      this.ironPick = ironPick;
      this.stonePick = stonePick;
    }

    @Override
    public int compareTo(Pick o) {
      return (o.diamondPick + o.ironPick + o.stonePick) - (this.diamondPick + this.ironPick + this.stonePick);
    }
  }

  public int solution(int[] picks, String[] minerals) {
    int answer = 0;
    int[][] fatigue = new int[3][3];
    fatigue[0] = new int[]{1, 1, 1};
    fatigue[1] = new int[]{5, 1, 1};
    fatigue[2] = new int[]{25, 5, 1};


    int length = Math.min(minerals.length, (picks[0] + picks[1] + picks[2]) * 5);
    int q = length / 5;
    int r = length % 5;
    ArrayList<Pick> sum = new ArrayList<>();

    for (int i = 0; i < q; i++) {
      int diamondPick = 0, ironPick = 0, stonePick = 0;
      for (int j = 0; j < 5; j++) {
        if (minerals[i * 5 + j].equals("diamond")) {
          diamondPick += fatigue[0][0];
          ironPick += fatigue[1][0];
          stonePick += fatigue[2][0];
        } else if (minerals[i * 5 + j].equals("iron")) {
          diamondPick += fatigue[0][1];
          ironPick += fatigue[1][1];
          stonePick += fatigue[2][1];
        } else {
          diamondPick += fatigue[0][2];
          ironPick += fatigue[1][2];
          stonePick += fatigue[2][2];
        }
      }
      sum.add(new Pick(diamondPick, ironPick, stonePick));
    }

    if (r != 0) {
      int diamondPick = 0, ironPick = 0, stonePick = 0;
      for (int i = 0; i < r; i++) {
        if (minerals[q * 5 + i].equals("diamond")) {
          diamondPick += fatigue[0][0];
          ironPick += fatigue[1][0];
          stonePick += fatigue[2][0];
        } else if (minerals[q * 5 + i].equals("iron")) {
          diamondPick += fatigue[0][1];
          ironPick += fatigue[1][1];
          stonePick += fatigue[2][1];
        } else {
          diamondPick += fatigue[0][2];
          ironPick += fatigue[1][2];
          stonePick += fatigue[2][2];
        }
      }
      sum.add(new Pick(diamondPick, ironPick, stonePick));
    }

    Collections.sort(sum);

    for (Pick p : sum) {
      if (picks[0] > 0) {
        answer += p.diamondPick;
        picks[0]--;
      } else if (picks[1] > 0) {
        answer += p.ironPick;
        picks[1]--;
      } else if (picks[2] > 0) {
        answer += p.stonePick;
        picks[2]--;
      } else {
        break;
      }
    }
    return answer;
  }
}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ