λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€/1단계

[ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ Lv1] : 체윑볡

by Jaeguk 2022. 6. 21.
문제 링크

https://programmers.co.kr/learn/courses/30/lessons/42862#

 

μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅ - 체윑볡

μ μ‹¬μ‹œκ°„μ— 도둑이 λ“€μ–΄, 일뢀 학생이 μ²΄μœ‘λ³΅μ„ λ„λ‚œλ‹Ήν–ˆμŠ΅λ‹ˆλ‹€. λ‹€ν–‰νžˆ μ—¬λ²Œ 체윑볡이 μžˆλŠ” 학생이 μ΄λ“€μ—κ²Œ μ²΄μœ‘λ³΅μ„ 빌렀주렀 ν•©λ‹ˆλ‹€. ν•™μƒλ“€μ˜ λ²ˆν˜ΈλŠ” 체격 순으둜 맀겨져 μžˆμ–΄, λ°”λ‘œ μ•žλ²ˆ

programmers.co.kr

μ²΄μœ‘λ³΅μ„ λ„λ‚œλ‹Ήν•œ 학생듀이 μ΅œλŒ€ν•œ μ²΄μœ‘λ³΅μ„ λΉŒλ €μ„œ μ²΄μœ‘μˆ˜μ—…μ„ 듀을 수 μžˆλ„λ‘ ν•΄μ£Όμž.

 

풀이
  • λ‚˜μ˜ μ•Œκ³ λ¦¬μ¦˜

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

μ΄λ ‡κ²Œ ν•˜λ©΄ 훨씬 더 κ°„κ²°ν•˜κ²Œ μž‘μ„±ν•  수 μžˆλ‹€.

728x90