https://school.programmers.co.kr/learn/courses/30/lessons/42895
๋ฌธ์ ์ค๋ช
์๋์ ๊ฐ์ด 5์ ์ฌ์น์ฐ์ฐ๋ง์ผ๋ก 12๋ฅผ ํํํ ์ ์์ต๋๋ค.
12 = 5 + 5 + (5 / 5) + (5 / 5)
12 = 55 / 5 + 5 / 5
12 = (55 + 5) / 5
5๋ฅผ ์ฌ์ฉํ ํ์๋ ๊ฐ๊ฐ 6,5,4 ์
๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด์ค ๊ฐ์ฅ ์์ ๊ฒฝ์ฐ๋ 4์
๋๋ค.
์ด์ฒ๋ผ ์ซ์ N๊ณผ number๊ฐ ์ฃผ์ด์ง ๋, N๊ณผ ์ฌ์น์ฐ์ฐ๋ง ์ฌ์ฉํด์ ํํ ํ ์ ์๋ ๋ฐฉ๋ฒ ์ค N ์ฌ์ฉํ์์ ์ต์๊ฐ์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํ์ธ์.
์ ํ์ฌํญ
- N์ 1 ์ด์ 9 ์ดํ์ ๋๋ค.
- number๋ 1 ์ด์ 32,000 ์ดํ์ ๋๋ค.
- ์์์๋ ๊ดํธ์ ์ฌ์น์ฐ์ฐ๋ง ๊ฐ๋ฅํ๋ฉฐ ๋๋๊ธฐ ์ฐ์ฐ์์ ๋๋จธ์ง๋ ๋ฌด์ํฉ๋๋ค.
- ์ต์๊ฐ์ด 8๋ณด๋ค ํฌ๋ฉด -1์ return ํฉ๋๋ค.
์ด๋ ค์ด dp๋ฌธ์ ... ์ฌ์ค dfs๋ก๋ ํ ์ ์์ ๊ฒ ๊ฐ์๋๋ฐ ๋ถ๋ฅ๊ฐ dp๊ธธ๋ dp๋ก ํ๋ ค๊ณ ์ต์ ์ ๋คํ์ผ๋
์คํจํ๊ณ ๊ทธ๋ฅ ๊ตฌ๊ธ๋งํด๋ณด์๋ค
๊ทผ๋ฐ ์ค๋ช ๋ณด๊ณ ๋ ์ดํด๊ฐ ์๋ผ์ ํ์ฐธ์ ๋์ ์ด๋ฉด์ ์ดํดํ๋ ค๊ณ ๋ ธ๋ ฅํจ. ๊ทธ๋์ ํ 80ํผ ์ ๋ ์ดํดํ๋ค....

์๋ ์์ฌ์๋ฐ
4์ค for๋ฌธ์ ๋๋ฌดํ๊ฑฐ ์๋์ค>?
๋์ ํ์ด
#include <string>
#include <vector>
#include <set>
using namespace std;
int solution(int N, int number) {
int answer = -1;
set<int> dp[8];
int tmp=0;
for(int i=0; i<8; ++i){
tmp = tmp*10 + N;
dp[i].insert(tmp);
}
for(int i=1; i<8; ++i){
for(int j=0; j<i; ++j){
for(int a : dp[j]){
for(int b : dp[i-j-1]){
dp[i].insert(a+b);
dp[i].insert(a-b);
dp[i].insert(a*b);
if(b>0) dp[i].insert(a/b);
}
}
}
}
for(int i=0; i<8; ++i){
if(dp[i].count(number)){
answer = i+1;
break;
}
}
return answer;
}
'๐ ์๊ณ ๋ฆฌ์ฆ > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++/PGS] Lv.1 : ๊ฐ์ธ์ ๋ณด ์์ง ์ ํจ๊ธฐ๊ฐ (์นด์นด์ค ๊ธฐ์ถ) (0) | 2023.04.12 |
---|---|
[C++/PGS] Lv.5 : ๋ฐฉ์ ๊ฐ์ (๊ทธ๋ํ) (0) | 2023.04.12 |
[C++/PGS] Lv.3 : ์์ (๊ทธ๋ํ/Floyd-Warshall) (2) | 2023.03.03 |
[C++/PGS] Lv.3 : ๊ฐ์ฅ ๋จผ ๋ ธ๋ (๊ทธ๋ํ/bfs) (1) | 2023.03.03 |
[C++/PGS] Lv.2 : H-index (์ ๋ ฌ) (0) | 2023.03.03 |