Posts

Showing posts from May, 2024

Pre-Order and Post-Order Traversals in the same Problem II

Image
An interesting problem (which I guarantee you'll never see in real life) to turn a tree upside down (yeah, guaranteed). Three steps taken here: 1. A pre-order approach to find the new root (just a loop going thru the left nodes) 2. A post-order approach to turn every node upside down using the given rules 3. Make the original root a leaf node Code is down below, cheers, ACC. Binary Tree Upside Down - LeetCode 156. Binary Tree Upside Down Medium 258 346 Add to List Share Given the  root  of a binary tree, turn the tree upside down and return  the new root . You can turn a binary tree upside down with the following steps: The original left child becomes the new root. The original root becomes the new right child. The original right child becomes the new left child. The mentioned steps are done level by level. It is  guaranteed  that every right node has a sibling (a left node with the same parent) and has no children.   Example 1: Input: root = [1,2,3,4,5] Output: [4,5,2,null,null,

Two Pointers for a Linear Solution VI (and the power of LLM to debug code)

Image
This is problem 11 on LeetCode, and the solution is a 2-pointer one, always moving the pointer pointing to the lower wall. The reason can be explained mathematically. Suppose that the current area is: Min(L,R) * N You want to maximize the first term (Min(L,R)) since the second one (N) will always decrease as you move the pointers. Therefore, move the one pointing to minimum height in order to guarantee that the next Min(L,R) will be greater than the previous one. The code is down below, but more interesting is to play with Copilot to debug the code. Purposedly, I introduced a bug where I forgot the brackets in the calculation of N. With the proper prompt engineering, you can ask the Copilot to pinpoint the issue and the fix. Both OpenAI ChatGPT and Bing Chat solved the problem flawlessly, see below. And the correct code is below too. Cheers, ACC. Container With Most Water - LeetCode 11. Container With Most Water Medium 28669 1710 Add to List Share You are given an integer array  heigh

Classic Dynamic Programming VIII

Image
This one was a straightforward DP, usually problems related to Min/Max with a constraint can be solved efficiently with DP. To make this post more interesting and to celebrate Mother's Day, here is a poem for all Mothers using Dynamic Programming (thanks to GAI): Mom's Dynamic Love Mom's love, like an algorithm refined, Dynamic and recursive, forever entwined. Her hugs are the base case, warm and secure, While her laughter loops infinitely, that's for sure. In the matrix of memories, she's the constant term, Guiding us through life's loops, each twist and turn. Her kisses, a recursive function, never-ending, Adding joy to our hearts, forever ascending. Mom's patience, an optimal solution we seek, As we debug our lives, sometimes feeling weak. She patches our errors with love's sweet embrace, And her smile—oh, that's the perfect base case! Her advice, a dynamic array of wisdom, Growing with each iteration, never random. She sorts out our troubles, lik