Two Pointers for a Linear Solution VI (and the power of LLM to debug code)
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