Skip to content

Conversation

@JooKangsan
Copy link
Collaborator

백트랙킹

md 파일로 추가했습니다!

📌 푼 문제

문제이름 문제링크
Permutations [🔗 LeetCode](https://leetcode.com/problems/permutations/)
Combinations [🔗 LeetCode](https://leetcode.com/problems/combinations/)
Subsets [🔗 LeetCode](https://leetcode.com/problems/subsets/)
Generate Parentheses [🔗 LeetCode](https://leetcode.com/problems/generate-parentheses/)
Combination Sum [🔗 LeetCode](https://leetcode.com/problems/combination-sum/)


📝 간단한 풀이 과정

Permutations

var permute = function(nums) {
  const result = [];

  const backtrack = (current, remaining) => {
      if (remaining.length === 0) {
          result.push([...current]);
          return;
      }
      
      for (let i = 0; i < remaining.length; i++) {
          current.push(remaining[i]);
          
          const nextRemaining = remaining.filter((_, index) => index !== i);
          backtrack(current, nextRemaining);
          
          current.pop();
      }
  };
  
  backtrack([], nums);
  return result;
};

Combinations

var combinationSum = function(candidates, target) {
    const result = [];
    
    const backtrack = (start, current, remaining) => {
        if (remaining === 0) {
            result.push([...current]);
            return;
        }
        
        if (remaining < 0) {
            return;
        }
        
        for (let i = start; i < candidates.length; i++) {
            current.push(candidates[i]);

            backtrack(i, current, remaining - candidates[i]);

            current.pop();
        }
    };
    
    backtrack(0, [], target);
    return result;
};

Subsets

var subsets = function(nums) {
    const result = [];
    
    const backtrack = (start, current) => {
        result.push([...current]);
        
        for (let i = start; i < nums.length; i++) {
            current.push(nums[i]);

            backtrack(i + 1, current);
            
            current.pop();
        }
    };
    
    backtrack(0, []);
    return result;
};

Generate Parentheses

var generateParenthesis = function(n) {
  const result = [];
  
  const backtrack = (open, close, current) => {
      if (current.length === 2 * n) {
          result.push(current);
          return;
      }

      if (open < n) {
          backtrack(open + 1, close, current + '(');
      }

      if (close < open) {
          backtrack(open, close + 1, current + ')');
      }
  };
  
  backtrack(0, 0, '');
  return result;
};

Combination Sum

var combinationSum = function(candidates, target) {
    const result = [];
    
    const backtrack = (start, current, remaining) => {
        if (remaining === 0) {
            result.push([...current]);
            return;
        }
        
        if (remaining < 0) {
            return;
        }
        
        for (let i = start; i < candidates.length; i++) {
            current.push(candidates[i]);

            backtrack(i, current, remaining - candidates[i]);

            current.pop();
        }
    };
    
    backtrack(0, [], target);
    return result;
};

@JooKangsan JooKangsan self-assigned this Mar 3, 2025
Copy link
Collaborator

@Moonjonghoo Moonjonghoo left a comment

Choose a reason for hiding this comment

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

leetcode 문제위주로 풀어주셨군요!
순열과조합등 기본수식을 재귀형태의 백트래킹으로잘풀이하신거같습니다! 한주간고생하셨어요

@Moonjonghoo Moonjonghoo merged commit 5438731 into main Mar 10, 2025
3 checks passed
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.

4 participants