#lec 53: Intro To Trees
#lec 54: Tree Prob Solving
🔹What is Tree DS, and Structure, Real Life usecase
🔹Inorder, PreOrder, PostOrder, Level Order
🔹Size Of BT
🔹Sum of BT
🔹Count of Leaf & Non Leaf
🔹Height Of BT
🔹Largest at each level
🔹Search in BST
@rohit_negi9@CoderArmy
796.Rotate String
🔹Rotation = leftmost char goes to end (left shift).
🔹At most n distinct rotations exist for string length n.
🔹way1 : rotate up to n times + compare ⇒ O(n²), O(1).
🔹way2: if goal is in (s+s) ⇒ rotation exists⇒O(n) time.
https://t.co/TyaX7xDOlh
796.Rotate String
🔹Rotation = leftmost char goes to end (left shift).
🔹At most n distinct rotations exist for string length n.
🔹way1 : rotate up to n times + compare ⇒ O(n²), O(1).
🔹way2: if goal is in (s+s) ⇒ rotation exists⇒O(n) time.
https://t.co/TyaX7xDOlh
Initialize count = 0
For each x from 1 to n:Set changed = false
While x > 0:d = x % 10
If d is 3 or 4 or 7: mark invalid, stop and say “not good”
If d is 2 or 5 or 6 or 9: set changed = true
x = x / 10
If number was valid AND changed == true: count++
Return count
Initialize count = 0
For each x from 1 to n:Set changed = false
While x > 0:d = x % 10
If d is 3 or 4 or 7: mark invalid, stop and say “not good”
If d is 2 or 5 or 6 or 9: set changed = true
x = x / 10
If number was valid AND changed == true: count++
Return count
396. Rotate Function
• We need the max of F(k) = sum(i * arr_k[i]) over all rotations.
• Derive: F(k+1) = F(k) + Sum - n *lastElement(arr_k).
• Without rotating, lastElement(arr_k) = nums[n-1-k].
• Get S and F0 once, then iterate to get all F(k) in O(n) and track the maximum.
396. Rotate Function
• We need the max of F(k) = sum(i * arr_k[i]) over all rotations.
• Derive: F(k+1) = F(k) + Sum - n *lastElement(arr_k).
• Without rotating, lastElement(arr_k) = nums[n-1-k].
• Get S and F0 once, then iterate to get all F(k) in O(n) and track the maximum.