[Programmers] 체육복

Time Lapse :30min 2sec

solution.cpp

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
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int solution(int n, vector<int> lost, vector<int> reserve) {
int answer = 0;
bool losted[32] = {false, };
bool reserved[32] = {false, };
for(int i = 0; i < lost.size();i++)
losted[lost[i]] = true;
for(int i = 0; i < reserve.size();i++)
reserved[reserve[i]] = true;
for(int i=1;i<=n;i++){
if(!reserved[i])
continue;
if(losted[i])
losted[i] = reserved[i] = false;
}
for(int i=1;i<=n;i++){
if(!reserved[i])
continue;
if(losted[i-1]&&losted[i+1])
continue;
else if(losted[i-1])
losted[i-1] = reserved[i] = false;
else if(losted[i+1])
losted[i+1] = reserved[i] = false;
}
for(int i=1;i<=n;i++){
if(!reserved[i])
continue;
if(losted[i-1])
losted[i-1] = reserved[i] = false;
else if(losted[i+1])
losted[i+1] = reserved[i] = false;
}
for(int i=1;i<=n;i++)
if(losted[i])
++answer;

return n-answer;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/gymSuit/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.