์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ์๊ณ ๋ฆฌ์ฆ
- redux-saga
- ๋ฆฌ์กํธ
- redux-toolkit
- SW
- axios
- C++
- Python
- ํญํดํ๋ฌ์ค
- react
- ๋งค์ผ๋ฉ์ผ
- createSlice
- redux
- java
- useDispatch
- react-router
- ์ฝ๋ฉํ ์คํธํฉ๊ฒฉ์๋๊ธฐ
- ์ด์ฝํ
- sw expert academy
- ํ๋ก๊ทธ๋๋จธ์ค
- maeil-mail
- Get
- programmers
- react-redux
- ํ ์ฝํ ์ฝ
- Algorithm
- ์๋ฐ
- JavaScript
- ํญํด99
- json-server
- Today
- Total
Binary Journey
[ํ ์ฝํ ์ฝ1.5 1-09] ๋ถ๋ ๊ฐ๊ธฐ ๋ณธ๋ฌธ
[ํ ์ฝํ ์ฝ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 ≤
- 1 ≤
health
≤ 1,000 - 1 ≤
attacks
์ ๊ธธ์ด ≤ 100attacks[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;
}
}
'Algorithm > ํ ์ฝํ ์ฝ1.5(2025.01)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ ์ฝํ ์ฝ1.5 1-11] ์ด์ํ ์นธ (0) | 2025.01.14 |
---|---|
[ํ ์ฝํ ์ฝ1.5 1-10] ๊ฐ์ฅ ๋ง์ด ๋ฐ์ ์ ๋ฌผ (0) | 2025.01.14 |
[ํ ์ฝํ ์ฝ1.5 1-08] ๊ณต์ (0) | 2025.01.14 |
[ํ ์ฝํ ์ฝ1.5 1-07] ์งํ ์ ๊ธฐ (0) | 2025.01.12 |
[ํ ์ฝํ ์ฝ1.5 1-06] ๋๋ค์ ๊ท์น (0) | 2025.01.12 |