๊ด€๋ฆฌ ๋ฉ”๋‰ด

Binary Journey

[ํ…Œ์ฝ”ํ…Œ์ฝ”1.5 1-09] ๋ถ•๋Œ€ ๊ฐ๊ธฐ ๋ณธ๋ฌธ

Algorithm/ํ…Œ์ฝ”ํ…Œ์ฝ”1.5(2025.01)

[ํ…Œ์ฝ”ํ…Œ์ฝ”1.5 1-09] ๋ถ•๋Œ€ ๊ฐ๊ธฐ

binaryJournalist 2025. 1. 14. 15:37
๋ฐ˜์‘ํ˜•
๐Ÿ’กํ…Œ์ฝ”ํ…Œ์ฝ” ์‹œ์ฆŒ 1.5 1ํšŒ ๋ชจ์ž„ off-site ๋ฌธ์ œ ํ’€์ด์ž…๋‹ˆ๋‹ค. (2025.01.12)

 

๋ฌธ์ œ

 

์ถœ์ฒ˜: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - PCCP ๊ธฐ์ถœ๋ฌธ์ œ01. ๋ถ•๋Œ€๊ฐ๊ธฐ

 

๋‚ด์šฉ

 

์–ด๋–ค ๊ฒŒ์ž„์—๋Š” ๋ถ•๋Œ€ ๊ฐ๊ธฐ๋ผ๋Š” ๊ธฐ์ˆ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋ถ•๋Œ€ ๊ฐ๊ธฐ๋Š” t์ดˆ ๋™์•ˆ ๋ถ•๋Œ€๋ฅผ ๊ฐ์œผ๋ฉด์„œ 1์ดˆ๋งˆ๋‹ค x๋งŒํผ์˜ ์ฒด๋ ฅ์„ ํšŒ๋ณตํ•ฉ๋‹ˆ๋‹ค. t์ดˆ ์—ฐ์†์œผ๋กœ ๋ถ•๋Œ€๋ฅผ ๊ฐ๋Š” ๋ฐ ์„ฑ๊ณตํ•œ๋‹ค๋ฉด y๋งŒํผ์˜ ์ฒด๋ ฅ์„ ์ถ”๊ฐ€๋กœ ํšŒ๋ณตํ•ฉ๋‹ˆ๋‹ค. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ์—๋Š” ์ตœ๋Œ€ ์ฒด๋ ฅ์ด ์กด์žฌํ•ด ํ˜„์žฌ ์ฒด๋ ฅ์ด ์ตœ๋Œ€ ์ฒด๋ ฅ๋ณด๋‹ค ์ปค์ง€๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์ˆ ์„ ์“ฐ๋Š” ๋„์ค‘ ๋ชฌ์Šคํ„ฐ์—๊ฒŒ ๊ณต๊ฒฉ์„ ๋‹นํ•˜๋ฉด ๊ธฐ์ˆ ์ด ์ทจ์†Œ๋˜๊ณ , ๊ณต๊ฒฉ์„ ๋‹นํ•˜๋Š” ์ˆœ๊ฐ„์—๋Š” ์ฒด๋ ฅ์„ ํšŒ๋ณตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ชฌ์Šคํ„ฐ์—๊ฒŒ ๊ณต๊ฒฉ๋‹นํ•ด ๊ธฐ์ˆ ์ด ์ทจ์†Œ๋‹นํ•˜๊ฑฐ๋‚˜ ๊ธฐ์ˆ ์ด ๋๋‚˜๋ฉด ๊ทธ ์ฆ‰์‹œ ๋ถ•๋Œ€ ๊ฐ๊ธฐ๋ฅผ ๋‹ค์‹œ ์‚ฌ์šฉํ•˜๋ฉฐ, ์—ฐ์† ์„ฑ๊ณต ์‹œ๊ฐ„์ด 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.

๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์„ ๋ฐ›์œผ๋ฉด ์ •ํ•ด์ง„ ํ”ผํ•ด๋Ÿ‰๋งŒํผ ํ˜„์žฌ ์ฒด๋ ฅ์ด ์ค„์–ด๋“ญ๋‹ˆ๋‹ค. ์ด๋•Œ, ํ˜„์žฌ ์ฒด๋ ฅ์ด 0 ์ดํ•˜๊ฐ€ ๋˜๋ฉด ์บ๋ฆญํ„ฐ๊ฐ€ ์ฃฝ์œผ๋ฉฐ ๋” ์ด์ƒ ์ฒด๋ ฅ์„ ํšŒ๋ณตํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

๋‹น์‹ ์€ ๋ถ•๋Œ€๊ฐ๊ธฐ ๊ธฐ์ˆ ์˜ ์ •๋ณด, ์บ๋ฆญํ„ฐ๊ฐ€ ๊ฐ€์ง„ ์ตœ๋Œ€ ์ฒด๋ ฅ๊ณผ ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ ํŒจํ„ด์ด ์ฃผ์–ด์งˆ ๋•Œ ์บ๋ฆญํ„ฐ๊ฐ€ ๋๊นŒ์ง€ ์ƒ์กดํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

๋ถ•๋Œ€ ๊ฐ๊ธฐ ๊ธฐ์ˆ ์˜ ์‹œ์ „ ์‹œ๊ฐ„, 1์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰, ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰์„ ๋‹ด์€ 1์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด bandage์™€ ์ตœ๋Œ€ ์ฒด๋ ฅ์„ ์˜๋ฏธํ•˜๋Š” ์ •์ˆ˜ health, ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ ์‹œ๊ฐ„๊ณผ ํ”ผํ•ด๋Ÿ‰์„ ๋‹ด์€ 2์ฐจ์› ์ •์ˆ˜ ๋ฐฐ์—ด attacks๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ชจ๋“  ๊ณต๊ฒฉ์ด ๋๋‚œ ์งํ›„ ๋‚จ์€ ์ฒด๋ ฅ์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”. ๋งŒ์•ฝ ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์„ ๋ฐ›๊ณ  ์บ๋ฆญํ„ฐ์˜ ์ฒด๋ ฅ์ด 0 ์ดํ•˜๊ฐ€ ๋˜์–ด ์ฃฝ๋Š”๋‹ค๋ฉด -1์„ return ํ•ด์ฃผ์„ธ์š”.

 

๊ธฐ๋กํ•˜๊ธฐ

๐Ÿ’ก ์–ด๋””๊นŒ์ง€ ์ƒ๊ฐํ•ด๋ดค๋Š”์ง€ ๋‹จ๊ณ„์ ์œผ๋กœ ๊ธฐ๋กํ•ด๋ด…๋‹ˆ๋‹ค.

 

์ ์šฉ ๊ทผ๊ฑฐ๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”?

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ ์„ค๋ช…๋Œ€๋กœ ๊ตฌํ˜„ํ•œ๋‹ค. ์•ผ๋งค ๊ฒŒ์ž„์ฒ˜๋Ÿผ ๋งŒ๋“ค์–ด ๋ณธ๋‹ค.

 

 

ํ’€์ด

ํ’€์ด ์‹œ๊ฐ„

์‹œ์ž‘ ์‹œ๊ฐ ์ข…๋ฃŒ ์‹œ๊ฐ ์ด ์†Œ์š” ์‹œ๊ฐ„
00:02 01:15 73๋ถ„

 

 

๋ฌธ์ œ ๋ถ„์„

 

์ œ์•ฝ ์‚ฌํ•ญ ํŒŒ์•… & ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ž‘์„ฑ

 

  • bandage๋Š” [์‹œ์ „ ์‹œ๊ฐ„, ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰, ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰] ํ˜•ํƒœ์˜ ๊ธธ์ด๊ฐ€ 3์ธ ์ •์ˆ˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
    • 1 ≤ ์‹œ์ „ ์‹œ๊ฐ„ = t ≤ 50
    • 1 ≤ ์ดˆ๋‹น ํšŒ๋ณต๋Ÿ‰ = x ≤ 100
    • 1 ≤ ์ถ”๊ฐ€ ํšŒ๋ณต๋Ÿ‰ = y ≤ 100
  • 1 ≤ health ≤ 1,000
  • 1 ≤ attacks์˜ ๊ธธ์ด ≤ 100
    • attacks[i]๋Š” [๊ณต๊ฒฉ ์‹œ๊ฐ„, ํ”ผํ•ด๋Ÿ‰] ํ˜•ํƒœ์˜ ๊ธธ์ด๊ฐ€ 2์ธ ์ •์ˆ˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
    • attacks๋Š” ๊ณต๊ฒฉ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.
    • attacks์˜ ๊ณต๊ฒฉ ์‹œ๊ฐ„์€ ๋ชจ๋‘ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
    • 1 ≤ ๊ณต๊ฒฉ ์‹œ๊ฐ„ ≤ 1,000
    • 1 ≤ ํ”ผํ•ด๋Ÿ‰ ≤ 100

 

์ž…๋ ฅ๊ฐ’ ๋ถ„์„

๐Ÿ’ก ์ž…๋ ฅ๊ฐ’์„ ๋ถ„์„ํ•˜๋ฉด ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ„์ ‘์ ์œผ๋กœ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 
์ž…์ถœ๋ ฅ ์˜ˆ
bandage health attacks result
[5, 1, 5] 30 [[2, 10], [9, 15], [10, 5], [11, 5]] 5
[3, 2, 7] 20 [[1, 15], [5, 16], [8, 6]] -1
[4, 2, 7] 20 [[1, 15], [5, 16], [8, 6]] -1
[1, 1, 1] 5 [[1, 2], [3, 2]] 3
 
 
์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

 

์ž…์ถœ๋ ฅ ์˜ˆ #1

๋ชฌ์Šคํ„ฐ์˜ ๋งˆ์ง€๋ง‰ ๊ณต๊ฒฉ์€ 11์ดˆ์— ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค. 0์ดˆ๋ถ€ํ„ฐ 11์ดˆ๊นŒ์ง€ ์บ๋ฆญํ„ฐ์˜ ์ƒํƒœ๋Š” ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์‹œ๊ฐ„ ํ˜„์žฌ ์ฒด๋ ฅ(๋ณ€ํ™”๋Ÿ‰) ์—ฐ์† ์„ฑ๊ณต ๊ณต๊ฒฉ ์„ค๋ช…
0 30 0 X ์ดˆ๊ธฐ ์ƒํƒœ
1 30(+0) 1 X ์ตœ๋Œ€ ์ฒด๋ ฅ ์ด์ƒ์˜ ์ฒด๋ ฅ์„ ๊ฐ€์งˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
2 20(-10) 0 O ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์œผ๋กœ ์—ฐ์† ์„ฑ๊ณต์ด ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.
3 21(+1) 1 X  
4 22(+1) 2 X  
5 23(+1) 3 X  
6 24(+1) 4 X  
7 30(+6) 5 → 0 X 5์ดˆ ์—ฐ์† ์„ฑ๊ณตํ•ด ์ฒด๋ ฅ์„ 5๋งŒํผ ์ถ”๊ฐ€ ํšŒ๋ณตํ•˜๊ณ  ์—ฐ์† ์„ฑ๊ณต์ด ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.
8 30(+0) 1 X ์ตœ๋Œ€ ์ฒด๋ ฅ ์ด์ƒ์˜ ์ฒด๋ ฅ์„ ๊ฐ€์งˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
9 15(-15) 0 O ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์œผ๋กœ ์—ฐ์† ์„ฑ๊ณต์ด ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.
10 10(-5) 0 O ๋ชฌ์Šคํ„ฐ์˜ ๊ณต๊ฒฉ์œผ๋กœ ์—ฐ์† ์„ฑ๊ณต์ด ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.
11 5(-5) 0 O ๋ชฌ์Šคํ„ฐ์˜ ๋งˆ์ง€๋ง‰ ๊ณต๊ฒฉ์ž…๋‹ˆ๋‹ค.

 

๋ชฌ์Šคํ„ฐ์˜ ๋งˆ์ง€๋ง‰ ๊ณต๊ฒฉ ์งํ›„ ์บ๋ฆญํ„ฐ์˜ ์ฒด๋ ฅ์€ 5์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 5์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

์˜์‚ฌ ์ฝ”๋“œ ์ž‘์„ฑ

๐Ÿ’ก ์˜์‚ฌ ์ฝ”๋“œ๋Š” ๋™์ž‘ ์ค‘์‹ฌ์œผ๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก ์˜์‚ฌ ์ฝ”๋“œ๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ ์ˆœ์„œ๋กœ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก ์˜์‚ฌ ์ฝ”๋“œ๋ฅผ ์ถฉ๋ถ„ํžˆ ํ…Œ์ŠคํŠธํ•ด๋ด…๋‹ˆ๋‹ค.

solution(int[] bandage, int health, int[][] attacks) {
    //
}

// ์Šคํ…Œ์ด์ง€
class Stage {
    private int start;
    private int duration;
    private int[] bandage;
    private int health;
    private int damage;
    private int serialSuccess;
    private Queue<Integer[]> attacks;

    public stage(int[] bandage, int health, int[][] attacks) {
        this.duration = attacks[attacks.length - 1][0];
        this.bandage = bandage;
        this.health = health;
        this.damage = 0;
        this.serialSuccess = 0;
        this.attacks = new LinkdedList<>();
        for (int[] attack : attacks) {
            this.attacks.add(attack);
        }
    }

    public int start() {
        return this.duration;
    }

    // ๊ณต๊ฒฉ
    public boolean attack(int time) {
        int[] monster = attacks.peek();
        if (monster != null && this.time == monster[0]) {
            this.damage += monster[1]
            return true;
        }
        return false;
    }

    // ์—ฐ์† ์„ฑ๊ณต ์ฐจ๋‹จ
    public void blockSerialSuccess() {
        this.serialSuccess = 0;
    }

    // ํšŒ๋ณต
    public void heal(int damage) {
        this.damage -= bandage[1];
        this.serialSuccess++;
        if (this.serialSuccess == bandage[0]) {
            this.serialSuccess -= bandage[2];
        }
    }

    public int getHp() {
        return this.health - Math.min(this.health, this.damage);
    }
}

 

 

๊ตฌํ˜„

import java.util.Queue;
import java.util.LinkedList;
import java.util.Arrays;

class Solution {
    public int solution(int[] bandage, int health, int[][] attacks) {
        Stage stage = new Stage(bandage, health, attacks);
        int duration = stage.start();
        for (int time = 1; time <= duration; time++) {
            boolean hasAttack = stage.attack(time);
            if (!hasAttack) {
                stage.heal();
            } else {
                stage.blockSerialSuccess();
            }
            // System.out.println("hasAttack: " + hasAttack + ", status: " + Arrays.toString(stage.getStatus(time)));
        }
        int hp = stage.getHp();
        return hp == 0 ? -1 : hp;
    }

    // ์Šคํ…Œ์ด์ง€
    class Stage {
        private int start;
        private int duration;
        private int[] bandage;
        private int health;
        private int damage;
        private int serialSuccess;
        private Queue<int[]> attacks;

        public Stage(int[] bandage, int health, int[][] attacks) {
            this.duration = attacks[attacks.length - 1][0];
            this.bandage = bandage;
            this.health = health;
            this.damage = 0;
            this.serialSuccess = 0;
            this.attacks = new LinkedList<>();
            for (int[] attack : attacks) {
                this.attacks.add(attack);
            }
        }

        // ์‹œ์ž‘ ์‹œ ๊ฒฝ๊ณผ ์˜ˆ์ƒ ์‹œ๊ฐ„ ๋ฐ˜ํ™˜
        public int start() {
            return this.duration;
        }

        // ๊ณต๊ฒฉ
        public boolean attack(int time) {
            int[] monster = this.attacks.peek();
            if (monster != null && time == monster[0]) {
                this.damage = Math.min(this.health, this.damage + monster[1]);
                this.attacks.poll();
                return true;
            }
            return false;
        }

        // ์—ฐ์† ์„ฑ๊ณต ์ฐจ๋‹จ
        public void blockSerialSuccess() {
            this.serialSuccess = 0;
        }

        // ํšŒ๋ณต
        public void heal() {
            if (this.damage >= this.health) {
                return;
            }
            this.damage = Math.max(0, this.damage - bandage[1]);
            this.serialSuccess++;
            if (this.serialSuccess == bandage[0]) {
                this.serialSuccess -= bandage[0];
                this.damage = Math.max(0, this.damage - bandage[2]);
            }
        }

        public int[] getStatus(int time) {
            int[] status = {time, this.health - this.damage, this.serialSuccess, this.damage};
            return status;
        }

        public int getHp() {
            return this.health - Math.min(this.health, this.damage);
        }
    }
}

 

 

๋ณต๊ธฐํ•˜๊ธฐ

๋‹ต์•ˆ๊ณผ ๋‚˜์˜ ํ’€์ด๋ฅผ ๋น„๊ตํ•ด๋ณด์„ธ์š”

 

์ถ”์ฒœ์ด ๊ฐ€์žฅ ๋งŽ์•˜๋˜ ํ’€์ด

import java.util.*;

class Solution {

    public int solution(int[] bandage, int health, int[][] attacks) {
        int cnt = bandage[0]; // ์ถ”๊ฐ€ ์ฒด๋ ฅ ๊ธฐ์ค€
        int now = health; // ํ˜„์žฌ ์ฒด๋ ฅ
        int std = 0; // ๋งˆ์ง€๋ง‰์œผ๋กœ ๊ณต๊ฒฉ๋‹นํ•œ ์‹œ๊ฐ„

        int v1, v2; // ์ถ”๊ฐ€ ์ฒด๋ ฅ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‚˜?
        for (int[] atk: attacks) {
            if (now <= 0) {
                return -1;
            }

            v1 = atk[0] - std - 1; // ์‹œ๊ฐ„ ์ฐจ์ด
            v2 = v1 / cnt; // ์ถ”๊ฐ€ ์ฒด๋ ฅ ํšŒ์ˆ˜

            // ๋งž๊ธฐ ์ง์ „๊นŒ์ง€์˜ ์ฒด๋ ฅ ์ •์‚ฐ
            std = atk[0];
            now = Math.min(health, now + (v1 * bandage[1]));
            now = Math.min(health, now + (v2 * bandage[2]));

            now -= atk[1];
        }        

        return now <= 0 ? -1 : now;
    }
}
๋ฐ˜์‘ํ˜•