LeetCode - Combinations

깽깽이·2024년 1월 21일
0

단순 순열과 조합 계열의 문제로 접근하여 std::next_permutation을 통한 구현 시 최적화 관점에서 비효율적일 가능성이 높다.

재귀적으로 k번만큼의 depth를 dfs처럼 접근하고, 이전 접근 값이 i일 경우 depth + 1은 i+1부터 N까지 탐색하는 방향으로 구현하는 방향이 효율적이다.

void calculate(int idx, int depth, std::vector<int> v){
   v.emplace_back(idx);

  if(depth == 0){
       ret.emplace_back(v);
       return;
   } 
   
   for(int i = idx + 1; i <= N; i++){
       calculate(i, depth - 1, v);
   }
   
   return;
}
profile
당신의 연주에 틀린 음은 없다. 그 다음의 음이 결정한다.

0개의 댓글