Skip to content

Conversation

@seolsis
Copy link
Contributor

@seolsis seolsis commented Nov 28, 2024

✔ 문제 제목: 부분집합 구하기

✔ 문제 유형: DFS

✔ 문제 풀이 날짜: 11.28

💡 문제 분석 요약

부분집합 모두 출력

💡 알고리즘 설계

  • 현재 원소 번호가 n+1이면, 즉 모든 원소를 탐색한 경우, answer에 부분집합 push
  • 그 외의 경우 현재 원소 번호를 subset에 push하고 DFS(L+1)
  • subset pop 후 DFS(L+1)

💡코드

function solution(n) {
  let answer = [];
  let subset = []; // 현재까지 선택된 부분 집합

  // L: 현재 원소 번호
  function DFS(L) {
    if (L === n + 1) {
      // 모든 원소를 탐색한 경우
      answer.push([...subset]);
    } else {
      subset.push(L);
      DFS(L + 1);

      subset.pop();
      DFS(L + 1);
    }
  }

  DFS(1);
  return answer;
}

console.log(solution(3));

💡 시간복잡도

경우의 수 : 2^n
한 번의 탐색 시간 : O(N)
O(2^n*N)

💡 느낀 점 or 기억할 정보

@seolsis seolsis self-assigned this Nov 28, 2024
Copy link
Contributor

@devwqc devwqc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋아요~ 💯

빈 배열도 들어가는 이슈가 있는 확인해 주시면 될 거 같아요!
고생하셨습니다. 👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉👍🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants