0% found this document useful (0 votes)
16 views3 pages

LeetxodeDay 27

Uploaded by

dhruuvnaik5702
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

LeetxodeDay 27

Uploaded by

dhruuvnaik5702
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

GDSC-DBIT DSA Leetcode 45 (Day-27)

Question 1: 77. Combinations


Tags: Backtracking

 PseudoCode:-

Function combine(n, k):

ans = [] // List to store combinations


backtrack(1, n, k, [], ans)
return ans

Function backtrack(i, n, k, list, ans):


// Base case: If k becomes 0, add the current combination to the result
if k == 0:
ans.add(list.copy())
return

// Iterate from i to n to ensure valid combinations


for j from i to n:
// Include the current element j in the combination
list.add(j)
// Recursive call with updated parameters
backtrack(j + 1, n, k - 1, list, ans)
// Backtrack: Remove the last added element to explore other possibilities
list.removeLast()

Time Complexity: O(k*nCk), here nCk means the binomial coefficient of picking k
elements out of n elements.

Space Complexity: O(K) -> Depth of Recursion tree + Size of List


Question 2: 22. Generate Parentheses
Tags:String Dynamic Programming Backtracking

 PseudoCode:-

Function generateParenthesis(n):
ans = [] // List to store valid parentheses combinations
function(n, ans, 0, 0, "") // Start the recursion with initial values
return ans

Function function(n, ans, open, close, current):


// Base case: If the length of the current string reaches 2*n, add it to the result
if length of current == 2 * n:
ans.add(current)
return

// Recursive calls for adding '(' and ')'


if open < n:
// Add '(' if the count of open parentheses is less than n
function(n, ans, open + 1, close, current + "(")
if close < open:
// Add ')' if the count of close parentheses is less than the count of open parentheses
function(n, ans, open, close + 1, current + ")")

time complexity : (O(2^N)), where (N) is the number of pairs of parentheses ((2^N) represents
the total number of recursive calls).

space complexity : (O(2^N)), where (N) is the number of pairs of parentheses.


Question 3: 46. Permutations
Tags:Array Backtracking

 PseudoCode:-
Function permute(nums):
ans = [] // List to store permutations
function(0, ans, nums)
return ans

Function function(ind, ans, nums):


// Base case: If the index 'ind' reaches the length of the array, add the current permutation to the result
if ind == length of nums:
ds = [] // List to store the current permutation
for i from 0 to length of nums - 1:
ds.add(nums[i])
ans.add(ds)
return

// Iterate from 'ind' to the end of the array to generate permutations


for i from ind to length of nums - 1:
// Swap elements at indices 'i' and 'ind'
swap(i, ind, nums)
// Recursive call with the updated index
function(ind + 1, ans, nums)
// Backtrack: Swap elements back to their original positions
swap(i, ind, nums)

Function swap(i, j, nums):


// Swap elements at indices 'i' and 'j' in the array 'nums'
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp

Time Complexity: O(n! * n)


Space Complexity: O(1)

You might also like