CTCI 4.9 BST Sequences - Java

  Рет қаралды 746

Kacy Codes

Kacy Codes

Күн бұрын

Пікірлер: 4
@PradeepSingh-vm1gl
@PradeepSingh-vm1gl 2 жыл бұрын
Great. Thank you.
@Mohammad-wo7yi
@Mohammad-wo7yi 2 жыл бұрын
Thanks for the explanation! What do you think the time complexity is? Intrestingly enough the CTCI solution ignored the time complexity discussion altogether.
@KacyCodes
@KacyCodes 2 жыл бұрын
Yeah I noticed that too. I think because it involves combinatorics math. Roughly off the top of my head, a balanced tree is the worst case for total # of combinations (that’s intuitive because the more unbalanced it is the closer it gets to being a linked list which only has 1 combination). So then you’d have to figure out the time complexity of each function call assuming the input is a balanced tree, and multiply that by the number of combinations. Now how do you figure out the total number of combinations? I would do the leetcode 1569 and try to come up with a time complexity for that problem, then come back to this one. If you want to figure out the time complexity for yourself that’s great, but if you’re doing it because you want to know for an interview, chances are you’re going to be one of the few that can even solve this particular problem, so knowing the time complexity would just be icing on the cake.
@OhYamz
@OhYamz Жыл бұрын
So at each height/level you're going to have 2^height nodes (roughly). So let's say nodesAtLevel = 2^height. You know at each level then, that the number of permutations for that level of the BST will be roughly nodesAtLevel! (factorial). Would it be possible to do a breadth first search pass through the tree, at each level simply build new lists from the previous permutations: root => 1! => [9] level2 => 2! => [9] + [3, 15] , [9] + [15, 3] level3 => ~4! => [9, 3, 15] + [all permutations of nodes at level3], .......... , [9, 15, 3] + [all permutations of nodes at level3 again] allSequences would essentially be [1!] + [2!] + [4!] + ... + [nodesAtLevel!] I haven't thought it all the way through. Not quite sure how you would store all the lists and start new lists when needed.
K-th element of two sorted Arrays - O( log(min(n, m)) )
23:08
Kacy Codes
Рет қаралды 2,6 М.
LeetCode 1192. Critical Connections in a Network
17:54
Kacy Codes
Рет қаралды 611
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 5 МЛН
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 25 МЛН
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
AVL Trees & Rotations (Self-Balancing Binary Search Trees)
20:38
Back To Back SWE
Рет қаралды 351 М.
LeetCode 818. Race Car - BFS - O( target * log(target) )
21:28
Kacy Codes
Рет қаралды 2,8 М.
LeetCode 2055. Plates Between Candles - O(n + q)
16:31
Kacy Codes
Рет қаралды 4,4 М.
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Brian Will
Рет қаралды 2,1 МЛН
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 197 М.
LeetCode 269. Alien Dictionary (DFS - Java)
19:21
Kacy Codes
Рет қаралды 319
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 403 М.
LeetCode 2104. Sum of Subarray Ranges
24:37
Kacy Codes
Рет қаралды 10 М.