How to solve 2, 3, and 4 sum?

Step 1: switch to Python 3, this can avoid using BigInteger, and read the questions.

Step 2: sort the input, so that the output is not redundant.

Step 3: write down the brute force approaches, they can pass a lot of test cases if not all.

Step 4: have a set to keep track of visited numbers, like in the graph traversals to pass all test cases.

My idea is that if I do not need to use divide and conquer, then I will not. Similarly, I am not going to swap keys and values in a map because a map is supposed to be a map to keep track of something. Likewise, I will not use quick select just because the problem says if I can avoid using sort. Not using sort is a challenge in itself. But I am aware that divide and conquer is a hard technique.

Last but not least, if both a greedy algorithm and not a greedy algorithm are accepted, then I will choose a non-greedy algorithm. If the problem accepts only a greedy algorithm, then I will use a greedy algorithm. In the last case, the so-called correct algorithm will produce unexpected outputs. So, in my opinion, the hardest algorithm is the greedy algorithm. This is just for interviews.

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务