Uploaded July 2023 | Updated November 2026, 3 days ago
๐ Full video - backtobackswe.com/platform/content/approximating-time-complexities-of-recursive-functions
A useful tool for understanding recursive algorithms is the recurrence.
A recurrence allows us to succinctly say exactly what a recursive algorithm does in terms of work and analyze it.
๐ Free 5-Day Course - backtobackswe.com/five-day
๐น Intuitive Video Explanations
๐ Run Code As You Learn
๐พ Save Progress
โNew Unseen Questions
๐ Get All Solutions
๐ Full video - backtobackswe.com/platform/content/approximating-time-complexities-of-recursive-functions
A useful tool for understanding recursive algorithms is the recurrence.
A recurrence allows us to succinctly say exactly what a recursive algorithm does in terms of work and analyze it.
๐ Free 5-Day Course - backtobackswe.com/five-day
๐น Intuitive Video Explanations
๐ Run Code As You Learn
๐พ Save Progress
โNew Unseen Questions
๐ Get All Solutions



![Max Contiguous Subarray Sum - Cubic Time To Kadanes Algorithm (Maximum Subarray on LeetCode)
Code & Problem Statement @ https://backtobackswe.com/platform/content/max-contiguous-subarray-sum/video?utm_source=youtube&utm_medium=video
Free 5-Day Mini-Course: https://backtobackswe.com
Try Our Full Platform: https://backtobackswe.com/pricing
๐น Intuitive Video Explanations
๐ Run Code As You Learn
๐พ Save Progress
โNew Unseen Questions
๐ Get All Solutions
Question: Given an integer array, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Approaches Covered:
- Approach 1 O(n^3) Time Solution
- Approach 2 O(n^2) Time Solution
- Approach 3 O(n) Solution (Kadanes Algorithm)
- - - maxSum[i] = max( A[i], A[i] + maxSum[i - 1] )
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: https://www.youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: https://www.youtube.com/user/tusharroy2525
GeeksForGeeks: https://www.youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: https://www.youtube.com/user/VSympathyV
Success In Tech: https://www.youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw Max Contiguous Subarray Sum - Cubic Time To Kadanes Algorithm (Maximum Subarray on LeetCode)](https://i.ytimg.com/vi/2MmGzdiKR9Y/mqdefault.jpg)
![Generate All Palindromic Decompositions Of A String (Palindrome Partitioning on Leetcode)
Free 5-Day Mini-Course: https://backtobackswe.com
Try Our Full Platform: https://backtobackswe.com/pricing
๐น Intuitive Video Explanations
๐ Run Code As You Learn
๐พ Save Progress
โNew Unseen Questions
๐ Get All Solutions
Question: Given a string, return all decompositions of the string that consist of only palindromes.
Example:
Input: aab
Output: [ [aa,b], [a,a,b] ]
Why is this problem backtracking?
It is backtracking because we will be expressing all of the possibilities of a search space given constraints. When we need to represent many decisions and possibilities, backtracking is the tool you should pull from your pocket.
Approach 1
Generate all decompositions and then only add the palindromic decompositions to our answer.
This is highly exponential in time since there are 2^(n - 1) palindromic decompositions.
This wastes a lot of time since we will continue to build on a decomposition that immediately disqualifies as palindromic.
Approach 2
We will take snapshots of snippets as we advance through the string and see if they can add to the decomposition that we want to build.
The 3 Keys To Backtracking:
Our Choice
The start and the end of a snapshot that we want to add to a decomposition we are working on.
Our Constraints
Each piece of the decomposition must be a palindrome, we cannot choose and advance on a non-palindrome snippet.
Our Goal
Decompose the whole string. When our decomposition progress index is the length of the array then we know that we have achieved this.
Complexities
Time: O(n * (2^n))
Worst case, but much better than approach 1 since this is a rare worst case where all decompositions turn out to be palindromic (a string of all 1 character).
Our best case becomes greatly improved.
We are basically taking subsets so (2^n) and the O(n) time to copy array to our answer.
Space: O(n)
At worst we will always go n stack frames deep in our recursion since an all single character decomposition is always a palindromic decomposition. This does not include the output array in space.
But let us imagine that we do include the output space.
We will have (2 ^ (n-1)) decompositions of the string.
We will certainly not have (2^n) PALINDROMIC decompositions for the string in a normal case.
The length of each decomposition will very likely not be n in the normal case (n/2 is probably a better approximation but Im honestly not sure).
So approximate length for each decomposition: (n/2).
MAXIMUM decompositions we could ever make: (2 ^ (n-1)).
So this yields a VERY loose upper bound on the space with the output array included:
O( (n/2) * (2 ^ (n-1)) ) = O( n * 2^n)
REMEMBER THAT n IS THE # OF CHARACTERS IN THE STRING PASSED IN TO US.
I just want to get you thinking about this, it is all about demonstrating that you can think along the right lines and get back on track whether you are exactly right or not.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: https://www.youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: https://www.youtube.com/user/tusharroy2525
GeeksForGeeks: https://www.youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: https://www.youtube.com/user/VSympathyV
Success In Tech: https://www.youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is number 16.7 in Elements of Programming Interviews by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash. Generate All Palindromic Decompositions Of A String (Palindrome Partitioning on Leetcode)](https://i.ytimg.com/vi/4ykBXGbonlA/mqdefault.jpg)




![The Balanced Parentheses Problem - Classic Stack Problem (Valid Parentheses on Leetcode)
Code - https://backtobackswe.com/platform/content/the-balanced-parentheses-problem/solutions
Free 5-Day Mini-Course: https://backtobackswe.com
Try Our Full Platform: https://backtobackswe.com/pricing
๐น Intuitive Video Explanations
๐ Run Code As You Learn
๐พ Save Progress
โNew Unseen Questions
๐ Get All Solutions
Question: Write a program that tests if a string made up of the characters (, ), {, }, [, and ] is balanced.
Examples
[ () () () ] { } { [ ( ) ] } valid
{ } { ) [ ] invalid
This is another textbook stack problem. Very widely known.
Notice how your brain tries to solve this problem subconsciously. Your eyes will try to scan from left to right keeping track of 2 critical things.
1.) The parentheses that are open
2.) The type of bracket that is open
When the matching closing parenthesis is found you realize that opening bracket is ok, it has been matched, and your mind then worries about the rest of the open brackets.
The Approach
We will scan the string left to right.
The key is that all points we will keep track somehow of the brackets that have been opened (left brackets) but not closed.
When we see a closing bracket we want to be sure that the most recent open bracket is of the same type (and that there even is a left bracket that it is matching).
A stack is perfect for this problem. (the LIFO property is a good fit)
Our Algorithm
-) When we see an open bracket we will push the bracket to the stack, it is now being tracked. It must be closed by the appropriate bracket at some point or else we have a problem.
-) When we see a closing bracket, we peek the top of the stack to make sure it is the correct type of opening bracket.
-) If it is another closing bracket or the wrong type or there is no bracket then we will return false, the parentheses set is not balanced
-) If we get through the whole string without a mismatch then the set is balanced
Time & Space Complexities
Let n be the length of the parentheses of the string.
Time: O( n )
We will traverse the whole string (if it is well formed and execution does not stop later).
The string is length n (has n brackets).
Space:
Worst Case (for valid string)
O( n / 2 ) = O( n )
Worst case, if your string is well-formed, we have to store half of the string if we have all open brackets followed by all matching closing brackets.
Example: ( ( ( ( ) ) ) )
n = 8
The stack will hold maximum 4 items at one point in time.
Worse Case (for invalid string)
O( n )
Worst case, if the string is not well-formed, we at worst could get n open brackets that see no matches at all in the string.
n = 8
( ( ( ( ( ( ( (
The stack will hold maximum 8 items at one point in time.
The Code
All credit for the code in this and many of my other videos go to the authors of Elements of Programming Interviews Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: https://www.youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: https://www.youtube.com/user/tusharroy2525
GeeksForGeeks: https://www.youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: https://www.youtube.com/user/VSympathyV
Success In Tech: https://www.youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is number 9.3 in Elements of Programming Interviews by Adnan Aziz The Balanced Parentheses Problem - Classic Stack Problem (Valid Parentheses on Leetcode)](https://i.ytimg.com/vi/CCyEXcNamC4/mqdefault.jpg)
