Posts

Showing posts from May, 2024

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

Don't be ashamed of N^4 (well, in some cases)

Image
Polynomial solutions are of course exponentially better than non-polynomial solutions (pun-intended). Comparing an N^2 with a 2^N is a no brainer, same for LogN with a SQRT(N) (the former is way better than the latter). The value of N clearly matters, though. The problem below is solved with an N^4 solution, where N=100. Now it is pushing the limits of LC, which won't ever accept a 10^9 solution (billion), but in some cases 100M is tractable. That's the case here. Best way to solve it IMO is to have a function to check whether the boundaries of the sub-matrix form a valid square. That one is tricky, requires some careful calculations. It runs in O(N). Once you have it, then you still need an N^3 nested-loops to check each sub-matrix. If N is relatively small, don't be ashamed of N^4. There must be a way to do it faster, but this is the best that your friend here could come up with. Cheers, ACC. Largest 1-Bordered Square - LeetCode 1139. Largest 1-Bordered Square Medium 700