-
Notifications
You must be signed in to change notification settings - Fork 2
18-mj010504 #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
18-mj010504 #69
Conversation
|
μ λ μ‘°ν©μ λ§λ€ λ κ·Έλκ·Έλ μν©μ λ§κ² λ€μν λ°©λ²μ μλνλ €κ³ νμ§λ§, λ체μ μΌλ‘λ μ¬κ·ν¨μλ₯Ό μ¬μ©νλ κ² κ°μ΅λλ€. μ΄λ² λ¬Έμ μμλ κ²°κ΅ λͺ¨λ μΌμ΄μ€λ₯Ό λ§λ ν λΈλ£¨νΈν¬μ€λ‘ μ΄ μΌμ΄μ€κ° κ°λ₯νμ§μ λν΄ μ²΄ν¬νλ κ³Όμ μ΄ νμνλ°, νμ°μ μΌλ‘ nC5 μ°μ°μ ν μλ°μ μμ΅λλ€. μ¬κ·λ₯Ό μΈ λλ νλΌλ―Έν°λ‘ μ¬λ¬ λ³μλ₯Ό μ€μΌ νκ±°λ, κ·Έκ² μ«λ€λ©΄ λ³μλ₯Ό μ μμΌλ‘ λΉΌλ²λ¦° νμ μ¬μ©νλ κ³Όμ μ΄ νμνκΈ΄ νμ§λ§ μ΄ λ°©λ²μ΄ λͺΈμ μ΅μ΄μ μμ£Ό μ¬μ©νκ² λλ κ² κ°μ΅λλ€. void comb(vector<int> combination,int start)
{
if(combination.size() == 5)
{
check(combination);
return;
}
for(int i = start; i <= n; i++)
{
combination.push_back(i);
comb(combination,i + 1);
combination.pop_back();
}
}
λ¬Έμ μμ μ¬μ©ν μ‘°ν© μ½λμ λλ€. λ¨μνκ² μ΄λ² κ°μ μ¬μ©νλ€λ©΄ λ€μ κΉμ΄μμλ κ·Έ κ°λΆν° λ°λ³΅λ¬Έμ μμνλ λ‘μ§μ λλ€. μλλ μ μΆ μ½λμ λλ€. λ¬Έμ νΈμλλΌ κ³ μνμ ¨μ΄μ! λΉλ° μ½λ ν΄λ /c++#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<int>> q;
vector<int> ans;
int answer;
void check(vector<int> comb)
{
for(int i = 0; i < q.size(); i++)
{
int checkcount = 0;
vector<bool> used(n + 1,false);
for(int j = 0; j < 5; j++)
{
used[q[i][j]] = true;
}
for(int j = 0; j < 5; j++)
{
if(used[comb[j]])
{
checkcount++;
}
}
if(checkcount != ans[i])
{
return;
}
}
answer++;
return;
}
void comb(vector<int> combination,int start)
{
if(combination.size() == 5)
{
check(combination);
return;
}
for(int i = start; i <= n; i++)
{
combination.push_back(i);
comb(combination,i + 1);
combination.pop_back();
}
}
int solution(int n_in, vector<vector<int>> q_in, vector<int> ans_in) {
n = n_in;
q = q_in;
ans = ans_in;
vector<int> combination;
comb(combination,1);
return answer;
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5κ°λ§ λ½λ κ±°λκΉ 5μ€ for λ¬ΈκΉμ§λ μμ§ν ν΄λ³Όλ§νκΈ° λλ¬Έμ κ°λ¨ν κ±°λ©΄ forλ¬ΈμΌλ‘ νκ±°λ, λλ νλΉ λμ²λΌ μ¬κ·ν¨μλ‘ μ§κ±°λ, itertoolsμ combinations λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©ν΄μ μμ£Ό νλλ€!
(μ°Έκ³ λ‘ itertoolsμλ μμ΄, μ€λ³΅μμ΄, μ‘°ν©, μ€λ³΅μ‘°ν©μ λͺ¨λ μ§μν©λλ€.)
μΌλ¨ nμ΄ λ μ μλ μ΅λκ°μ΄ 30μ΄κ³ κ·Έ μ€ 5κ°λ₯Ό λ½λ κ²½μ°μ μλ
λΉλ°μ½λκ° λ μ μλ λͺ¨λ κ²½μ°μ μλ₯Ό λ½κ³ κ·Έ ν보λ§λ€ μ
λ ₯ν μ μμ μΌμΉνλ κ°μκ° μ£Όμ΄μ§ κ²κ³Ό μΌμΉνλ μ§λ₯Ό νλνλ λΉκ΅νμ΅λλ€.
μ¬κΈ°μ λΉλ°μ½λμ q[i] κ° μ€λ¦μ°¨μμΌλ‘ μ λ ¬λμ΄ μλ€λ κ²λ ν° ννΈμΈλ° λ λ°°μ΄μ΄ μ΄λ―Έ μ λ ¬λ¨μ΄ 보μ₯λλ©΄ λ λ°°μ΄μμ μΌμΉνλ μμμ κ°μλ₯Ό ꡬνλ κ²μ ν¬ν¬μΈν°λ₯Ό μ΄μ©ν΄μ
νμ΄μ¬ νμ΄
from itertools import combinations
def check(x, y):
i = j = 0
count = 0
while i < len(x) and j < len(y):
if x[i] == y[j]:
count += 1
i += 1
j += 1
elif x[i] < y[j]:
i += 1
else:
j += 1
return count
def solution(n, q, ans):
answer = 0
cands = combinations(list(range(1, n+1)), 5)
for cand in cands:
found = True
for q1, a1 in zip(q, ans):
if check(cand, q1) != a1:
found = False
break
if found:
answer += 1
return answerμ½λλ₯Ό μ½λ€κ° zip ν¨μκ° λμ§? νκ³ μκ°ν μ μλλ° λ§€λ² q[i]μ ans[i]λ₯Ό λ°λ‘ λ°λ‘ μ μ§μκ³ νμ€λ‘ μΈ μ μκ²νλ νμ΄μ¬ ν¨μλΌκ³ μκ°νμλ©΄ λ©λλ€.
νμ΄μ¬μ΄λΌμ μ‘°ν© λ λ¨Ήνλ€νκ³ μκ°ν μλ μλλ° c++μλ next_permutation μ΄λΌλ λΌμ΄λΈλ¬λ¦¬ ν¨μκ° μμ΅λλ€. μ΄κ±΄ μμ΄λ§ λ½μμ£ΌκΈ°λ νλλ° μ΄λ»κ² μ μ‘°μνλ©΄ μ‘°ν©μΌλ‘λ μ¬μ©ν μ μμ΅λλ€. μ°Έκ³ νμλ©΄ μ’μ κ±° κ°μ΅λλ€!
μ΅κ·Όμ νμλ 리λͺ¨μ»¨ λ¬Έμ λ λΉμ·ν λ°©λ²μ΄λΌμ νμ΄λ²μ΄ μ½κ² λ μ€λ₯Έ κ±° κ°μ΅λλ€. νμ λ¬Έμ ν λλ λΈλ£¨νΈν¬μ€λ‘ λ¨Όμ ν리λμ§ νμΈνλ μ΅κ΄μ΄ μ€μν κ±° κ°μ΅λλ€!
μκ³ νμ ¨μ΅λλ€.
μΆκ°(set μλ£νμ μ΄μ©ν νμ΄)
λ€λ₯Έ μ¬λμ΄ νΌ νμ΄λ₯Ό μ΄ν΄λ³΄λλ° μμ²λ νμ΄κ° μμ΄μ κ°μ Έμμ΅λλ€.
import itertools
def solution(n, q, ans):
f = list(itertools.combinations(range(1, n + 1), 5))
for g, cnt in zip(q, ans):
f = [code for code in f if len(set(code) & set(g)) == cnt]
return len(f)μ κ° μ κΈ°νλ€ λκΌλ 건 λ λ°°μ΄μμ μΌμΉνλ μμμ κ°μλ₯Ό ꡬνλ λ‘μ§μ len(set(code) & set(g)) == cntλ‘ μ²λ¦¬ν λΆλΆμ΄μμ΅λλ€. νμ΄μ¬μμ μ 곡νλ μΈνΈ(μ§ν©) μλ£νμ μ΄μ©ν΄ λ λ°°μ΄μ μ§ν©μΌλ‘ λ§λ€κ³ κ΅μ§ν©μ κ°μλ‘ μ²λ¦¬νμ΅λλ€.
ν¬ ν¬μΈν° νμ΄κ° λ μ€λ₯΄κΈ° μ΄λ €μ΄ κ²μ μλμ§λ§ μκ°μ μ μ½νκΈ° μν΄μλ μ΄λ° λ°©λ²λ μμλλ©΄ λμμ§ μμ κ±° κ°μ΅λλ€!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ μμ νμμΌλ‘ νμ΄λ³΄μμ΅λλ€.
λ무 볡μ‘νκ³ μ΄λ €μ΄ λ¬Έμ λ€μ γ
γ
λ¬Έμ νΈλλΌ κ³ μνμ
¨μ΅λλ€.
μ½λ/c++
#include <string>
#include <vector>
using namespace std;
int solution(int n, vector<vector<int>> q, vector<int> ans) {
int answer = 0;
int q_rows = (int)q.size();
int ans_len = (int)ans.size();
for (int a = 1; a <= n - 4; a++) {
for (int b = a + 1; b <= n - 3; b++) {
for (int c = b + 1; c <= n - 2; c++) {
for (int d = c + 1; d <= n - 1; d++) {
for (int e = d + 1; e <= n; e++) {
bool inCand[31] = { false };
inCand[a] = true;
inCand[b] = true;
inCand[c] = true;
inCand[d] = true;
inCand[e] = true;
bool ok = true;
for (int i = 0; i < q_rows; i++) {
int cnt = 0;
bool seen[31] = { false };
for (int j = 0; j < (int)q[i].size(); j++) {
int v = q[i][j];
if (1 <= v && v <= n) {
if (!seen[v]) {
if (inCand[v]) {
cnt++;
}
seen[v] = true;
}
}
}
if (i < ans_len) {
if (cnt != ans[i]) {
ok = false;
break;
}
} else {
ok = false;
break;
}
}
if (ok) {
answer++;
}
}
}
}
}
}
return answer;
}
π λ¬Έμ λ§ν¬
λΉλ° μ½λ ν΄λ
https://school.programmers.co.kr/learn/courses/30/lessons/388352
βοΈ μμλ μκ°
1μκ° 30λΆ
β¨ μλ μ½λ
μλ μ½λ
μ§λ¬Έ
π μλ‘κ² μκ²λ λ΄μ©