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.

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 17:58
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-20 21:22
已编辑
门头沟学院 Java
纯真的河老师在喝茶:答应了就跑啊,实习随便跑啊,别被pua了,md就是找个廉价劳动力,还平稳过度正式工,到时候跟你说没转正
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务