2024-TEAM-05 / algorithm-for-kakao

์นด์นด์˜ค ๊ธฐ์ถœ ๋ฌธ์ œ ๊ฐ€์ฆˆ์•„๐Ÿฃ
0 stars 0 forks source link

[2022 KAKAO BLIND RECRUITMENT] ์–‘๊ถ๋Œ€ํšŒ #21

Open uijin-j opened 1 month ago

uijin-j commented 1 month ago

๐Ÿ”— ์–‘๊ถ๋Œ€ํšŒ

hye-on commented 1 month ago

์ฝ”๋“œ ํ’€์ด

```cpp #include #include #include #include using namespace std; //08:52 //0์€ ์–‘, 1์€ ๋Š‘๋Œ€ // visit์ฒ˜๋ฆฌ ๋•œ์— dfs //์–‘ ์ชฝ์˜ ์ตœ๋Œ€ ์–‘์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐœ์ˆ˜ #include #include #include #include using namespace std; //k์ ์— ๋งžํžŒ ์„ ์ˆ˜๊ฐ€ k ์ ์„ ๊ฐ€์ ธ๊ฐ. ๊ฐ™์œผ๋ฉด ์–ดํ”ผ์น˜๊ฐ€ ๊ฐ€์ ธ๊ฐ //์ข… ์ ์ˆ˜๊ฐ€ ๊ฐ™์„ ๊ฒฝ์šฐ ์–ดํ”ผ์น˜๋ฅผ ์šฐ์Šน์ž๋กœ ๊ฒฐ์ • //๋ผ์ด์–ธ์ด ๊ฐ€์žฅ ํฐ ์ ์ˆ˜ ์ฐจ์ด๋กœ ์šฐ์Šนํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ผ ๊ฒฝ์šฐ, ๊ฐ€์žฅ ๋‚ฎ์€ ์ ์ˆ˜๋ฅผ ๋” //๋งŽ์ด ๋งžํžŒ ๊ฒฝ์šฐ //ํˆฌ ํฌ์ธํ„ฐ? // ๋ฐฑํŠธ๋ž˜ํ‚น! int p[11]; int sum =55; int mySum =0; int maxS; vector ans(11); vector result(11); vector info; void checkAns(vector num){ int yourS =0; int myS =0; for(int i=0;i<11;i++){ int score = 10-i; if(info[i]=num[i] && info[i]>=1){ yourS+=score; } } if(myS>yourS){ if(myS-yourS>maxS){ //๊ฐ€์žฅ ํฐ ์ ์ˆ˜ ์ฐจ์ด maxS = myS-yourS; ans = num; }else if(myS-yourS == maxS){ for(int i=10;i>=0;i--){ if(ans[i]>num[i]) return; else if(ans[i]num){ if(rn<0){ return; } if(depth==11){ //๋‹ต์ด ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ์ฒดํฌ if(rn>0) num[10] =rn; checkAns(num); return; } int t = info[depth]+1; num[depth] = t; recursion(depth +1 ,rn-t , num); //๊ณผ๋… ๋งž์ถค num[depth] =0; recursion(depth +1 ,rn , num); // ๊ณผ๋… ๋ชป๋งž์ถค } vector solution(int n, vector _info) { vector answer; info = _info; recursion(0,n,result); for(int i=0;i<11;i++) if(ans[i]>=1) return ans; answer.push_back(-1); return answer; } ```

์ฝ”๋ฉ˜ํŠธ

- ํˆฌํฌ์ธํ„ฐ๋กœ ์ ‘๊ทผํ–ˆ๋‹ค๊ฐ€ ์•„๋‹ˆ์˜€๊ณ  ๊ฒฐ๊ตญ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•œ ์™„ํƒ + ์กฐ๊ฑด ๊ผผ๊ผผํžˆ ์ฝ๊ธฐ ์˜€๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
- ๊ฐ€์žฅ ํฐ ์ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ <๊ฐ€์žฅ ํฐ ์ ์ˆ˜์ฐจ์ด>๋ฅผ ๋†“์ณค์—ˆ๋„ค์š”.. ์นด์นด์˜ค๋Š” ๋ฌธ์ œ ๊ผผ๊ผผํžˆ..
uijin-j commented 1 month ago

๐Ÿ“‘ ๋Œ“๊ธ€ ํ…œํ”Œ๋ฆฟ

์ฝ”๋“œ ํ’€์ด

```java import java.util.*; // 16:42 START! // ์–ด๋ ต๋‹น.. class Solution { /** * ๋ณด์ž๋งˆ์ž ๋ฐฐ๋‚ญ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฐ๋‚จ! (์ด๋“์„ ์–ป๊ธฐ ์œ„ํ•ด ์†์‹ค๋„ ์žˆ์Œ) * - ์จ์•ผํ•˜๋Š” ํ™”์‚ด => ์†์‹ค => ๋ฐฐ๋‚ญ ๋ฌธ์ œ์—์„œ ๋ฌด๊ฒŒ * - ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ ์ˆ˜ => ์ด๋“ => ๋ฐฐ๋‚ญ ๋ฌธ์ œ์—์„œ ๊ฐ€์น˜ * * ๊ฒฐ๊ตญ ํ’€์ด๋ฅผ ๋ดค๋”๋‹ˆ.. ์™„ํƒ๋ฌธ์ œ์˜€๋‹ค.. */ int[] ryan = new int[11]; int[] apeach = new int[11]; int[] answer = new int[11]; int maxDiff = 0; public int[] solution(int n, int[] info) { apeach = info; dfs(0, n); return maxDiff == 0 ? new int[]{-1} : answer; } private void dfs(int level, int remain) { if(level == 10) { ryan[10] = remain; // ๋‚จ์€ ํ™”์‚ด์€ 0์— ์˜๊ธฐ int rScore = 0; int aScore = 0; for(int i = 0; i <= 10; ++i) { if(ryan[i] > apeach[i]) rScore += 10 - i; else if(apeach[i] > 0) aScore += 10 - i; } if(rScore - aScore > maxDiff) { maxDiff = rScore - aScore; answer = copy(ryan); } if(rScore - aScore == maxDiff) { for(int i = 10; i >= 0; --i) { if(ryan[i] > answer[i]) { answer = copy(ryan); break; } if(answer[i] > ryan[i]) break; } } return; } // level ๊ณผ๋…์„ ๋ผ์ด์–ธ์ด ๋งž์ถค if(apeach[level] + 1 <= remain) { ryan[level] = apeach[level] + 1; dfs(level + 1, remain - (apeach[level] + 1)); ryan[level] = 0; } // level ๊ณผ๋…์„ ๋ผ์ด์–ธ์ด ์•ˆ ๋งž์ถค dfs(level + 1, remain); } public int[] copy(int[] arr) { int[] answer = new int[11]; for(int i = 0; i <= 10; ++i) { answer[i] = arr[i]; } return answer; } } ```

์ฝ”๋ฉ˜ํŠธ

- n์ด ์ž‘์•„์„œ ์™„ํƒ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ดœํžˆ ๋” ์–ด๋ ต๊ฒŒ ์ ‘๊ทผํ–ˆ๋„ค์š”๐Ÿฅฒ ํ•ญ์ƒ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋จผ์ € ๊ณ„์‚ฐํ•ด์•ผ ๋  ๊ฒƒ ๊ฐ™๋„ค์šฉ..