forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.cpp
More file actions
38 lines (36 loc) Β· 1.52 KB
/
8.cpp
File metadata and controls
38 lines (36 loc) Β· 1.52 KB
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
#include <bits/stdc++.h>
using namespace std;
int solution(int n, vector<int> weak, vector<int> dist) {
// κΈΈμ΄λ₯Ό 2λ°°λ‘ λλ €μ 'μν'μ μΌμ ννλ‘ λ³κ²½
int length = weak.size();
for (int i = 0; i < length; i++) {
weak.push_back(weak[i] + n);
}
// ν¬μ
ν μΉκ΅¬ μμ μ΅μκ°μ μ°ΎμμΌ νλ―λ‘ len(dist) + 1λ‘ μ΄κΈ°ν
int answer = dist.size() + 1;
// 0λΆν° length - 1κΉμ§μ μμΉλ₯Ό κ°κ° μμμ μΌλ‘ μ€μ
for (int start = 0; start < length; start++) {
// μΉκ΅¬λ₯Ό λμ΄νλ λͺ¨λ κ²½μ° κ°κ°μ λνμ¬ νμΈ
do {
int cnt = 1; // ν¬μ
ν μΉκ΅¬μ μ
// ν΄λΉ μΉκ΅¬κ° μ κ²ν μ μλ λ§μ§λ§ μμΉ
int position = weak[start] + dist[cnt - 1];
// μμμ λΆν° λͺ¨λ μ·¨μ½ν μ§μ μ νμΈ
for (int index = start; index < start + length; index++) {
// μ κ²ν μ μλ μμΉλ₯Ό λ²μ΄λλ κ²½μ°
if (position < weak[index]) {
cnt += 1; // μλ‘μ΄ μΉκ΅¬λ₯Ό ν¬μ
if (cnt > dist.size()) { // λ ν¬μ
μ΄ λΆκ°λ₯νλ€λ©΄ μ’
λ£
break;
}
position = weak[index] + dist[cnt - 1];
}
}
answer = min(answer, cnt); // μ΅μκ° κ³μ°
} while(next_permutation(dist.begin(), dist.end()));
}
if (answer > dist.size()) {
return -1;
}
return answer;
}