λ¬Έμ λ§ν¬
https://programmers.co.kr/learn/courses/30/lessons/42862#
체μ‘볡μ λλλΉν νμλ€μ΄ μ΅λν 체μ‘볡μ λΉλ €μ 체μ‘μμ μ λ€μ μ μλλ‘ ν΄μ£Όμ.
νμ΄
- λμ μκ³ λ¦¬μ¦
1. μμ΄λ²λ¦° νμλ€ μ€μ μ¬λ²μ·μ κ°μ§κ³ μλ νμλ€μ λΉλ €μ£Όμ§ λͺ»νκ³ μκΈ°κ° μ μ΄μΌ νλ―λ‘
λ³ΈμΈ μ¬λ²μ·μ μ λλ€.
2. 체μ‘볡μ λΉλ €μ€ μ μλ νμλ€ μ€μ 2λͺ μ λΉλ €μ€ μ μλ νμμ΄ μλ, 1λͺ λ§ λΉλ €μ€ μ μλ νμλ€ λ¨Όμ λλ λΉν νμλ€μκ² μ²΄μ‘볡μ λΉλ €μ€λ€.
μ΄μ λ??
=> λ§μ½ λλλΉν νμ iμκ² 2λͺ μ νμ A,Bκ° μ²΄μ‘볡μ λΉλ €μ€ μ μλ€κ³ νμ.
μ΄λ νμ Aλ i μΈμλ λΉλ €μ€ νμμ΄ μκ³ , νμ Bλ i μΈμλ λ€λ₯Έ νμμκ² ν λͺ λ λΉλ €μ€ μ μλ€λ©΄.
νμ Aκ° iμκ² λΉλ €μ£Όλ©΄, Bλ λ€λ₯Έ νμμκ² λΉλ €μ€ μ μμΌλ―λ‘ λλλΉν νμ 2λͺ μ΄ μμ μ μ°Έκ°ν μ μκ²λλ€.
λ§μ½ Bκ° iμκ² λΉλ €μ€ λ²λ¦¬λ©΄ Aκ° κ°μ§κ³ μλ μ¬λ²μ·μ 무μ©μ§λ¬Όμ΄λ€.
3. 체μ‘볡μ λΉλ €μ€ μ μλ νμλ€ μ€ 2λ² κ³Όμ μμ λΉλ €μ£Όμ§ λͺ»ν νμλ€μ΄ λ¨μ λλ λΉν νμλ€μκ² λΉλ €μ€λ€.
μ½λ μμ±
|
#include <string>
#include <vector>
#include <iostream>
using namespace std;
const int MAX_N = 30 + 5;
bool is_lost[MAX_N];
int solution(int n, vector<int> lost, vector<int> reserve) {
int answer = n - lost.size();
for(int i = 0; i<lost.size(); i++)
is_lost[lost[i]] = true;
for(int i = 0; i<reserve.size(); i++){
int student = reserve[i];
if(is_lost[student]){ //μκΈ°κ° μ
μ΄μΌν΄μ λͺ»λΉλ €μ€
reserve[i] = -1;
answer++;
is_lost[student] = false;
}
}
for(int i = 0; i<reserve.size(); i++){
int student = reserve[i];
if(student == -1)
continue;
int can_rent = 0;
if(is_lost[student - 1])
can_rent++;
if(is_lost[student + 1])
can_rent++;
if(can_rent == 2 || can_rent == 0)
continue;
if(is_lost[student - 1]){
reserve[i] = -1;
answer++;
is_lost[student - 1] = false;
continue;
}
else if(is_lost[student + 1]){
reserve[i] = -1;
answer++;
is_lost[student + 1] = false;
}
}
for(int i = 0; i<reserve.size(); i++){
if(reserve[i] == -1)
continue;
int student = reserve[i];
if(is_lost[student - 1]){
answer++;
is_lost[student - 1] = false;
continue;
}
else if(is_lost[student + 1]){
answer++;
is_lost[student + 1] = false;
}
}
return answer;
}
|
cs |
1, 2, 3λ² κ³Όμ μ λ°λ‘ μμ±νλ€ λ³΄λκΉ μ΄μ© μ μμ΄ μ½λκ° κΈΈμ΄μ‘λ€.
- λ€λ₯Έ λΆμ μκ³ λ¦¬μ¦
ACλ₯Ό λ°μ νμ λ€λ₯Έ λΆλ€μ μκ³ λ¦¬μ¦μ ν λ² λ΄€λλ, λ΄ μ½λμ μ λ°λ μλλ μμ΄μλ€.
κ·Έ λΆλ€μ μμ΄λμ΄λ
int student[] λΌλ λ°°μ΄μ λ§λ€μ΄μ νμλ€λ§λ€ κ°μ§κ³ μλ 체μ‘볡μ μμ μ μλ€.
λ°°μ΄μ μ΄κΈ°κ°μ λͺ¨λ λ³ΈμΈ μ²΄μ‘볡μ κ°κ³ μμΌλ―λ‘ 0μ΄λ€.
λλ λΉν νμλ€μ λ°°μ΄ κ°μ -1, 체μ‘λ³΅μ΄ λ¨λ νμλ€μ +1μ ν΄μ€λ€.
μ΄λ κ² νλ©΄ λλ λΉνμ§λ§ λ³ΈμΈμ΄ μ¬λ²μ·μ κ°μ§κ³ μλ νμλ€μ -1 ν νμ +1μ νκΈ°λλ¬Έμ κ²°κ΅ κ°μ΄ 0μ΄λλ€.
μ΄μ νμλ€ μ€μ λ°°μ΄μ κ°μ΄ 1μΈ νμλ€λ§ 체μ‘볡μ λΉλ €μ€ μ μλ€.
μ΄λ, λλ λΉν νμμ κΈ°μ€μΌλ‘ μ, λ·μ¬λμκ² λͺ¨λ 체μ‘볡μ λΉλ¦΄ μ μλ κ²½μ°μ μμ¬λμκ² λΉλ¦¬λ κ²μ μ°μ μΌλ‘ μκ°νλ€λ©΄ μ΅λν λ§μ νμλ€μ΄ 체μ‘볡μ λΉλ¦΄ μ μκ²λλ€.
μ΄λ κ² νλ©΄ ν μ¬λμκ² 2λͺ μ νμμ΄ μ²΄μ‘볡μ λΉλ €μ€ μ μλ μν©μ΄ μμ λ, μ΅λν λ§μ νμλ€μ΄ 체μ‘볡μ λΉλ¦΄ μ μκ² λλ€.
μ½λ μμ±
|
#include <string>
#include <vector>
using namespace std;
int student[35];
int solution(int n, vector<int> lost, vector<int> reserve) {
int answer = 0;
for(int i : reserve) student[i] += 1;
for(int i : lost) student[i] += -1;
for(int i = 1; i <= n; i++) {
if(student[i] == -1) {
if(student[i-1] == 1)
student[i-1] = student[i] = 0;
else if(student[i+1] == 1)
student[i] = student[i+1] = 0;
}
}
for(int i = 1; i <=n; i++)
if(student[i] != -1) answer++;
return answer;
}
|
cs |
μ΄λ κ² νλ©΄ ν¨μ¬ λ κ°κ²°νκ² μμ±ν μ μλ€.
'νλ‘κ·Έλλ¨Έμ€ > 1λ¨κ³' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[νλ‘κ·Έλλ¨Έμ€ Lv1] : μ κ· μμ΄λ μΆμ² (0) | 2022.06.13 |
---|---|
[νλ‘κ·Έλλ¨Έμ€ Lv1] : λ‘λμ μ΅κ³ μμμ μ΅μ μμ (0) | 2022.06.13 |
[νλ‘κ·Έλλ¨Έμ€ Lv1] : μ κ³ κ²°κ³Ό λ°κΈ° (0) | 2022.06.13 |