653. Two Sum IV - Input is a BST
solution 1: use set.
solution 2: use the fact that a sorting array is given.
solution 1: use set.
solution 2: use the fact that a sorting array is given.
No explainaton needed. It is a good code template for BS.
It has some problems with test cases so can’t get it ac. Whatever, I have the correct code anyway…
Related problem: #378, # 668. The difference is how to count how many smaller. To count quickly, you need to:
Similar to #378: http://localhost:4000/2018/06/21/LeetCode-Challenge-log-378/, except now we can count how many smaller faster in O(n) time! There is also two tricks to achieve 99% beaten:
Similar to #373: https://marcopolocai.github.io/2018/06/20/LeetCode-Challenge-log-373/.
However, because all we want is just the kth number, we can use a binary-search-like approach to make a guess and count if it has k-1 lower numbers. It is a faster approach but very tricky to implement, as you has to be very careful on the boundary.
When reading the python docs and source codes, I see there is lots of usage of “___” in naming and other places. So I google a bit and find two great articles for explanation. Here are the links and thank Chevalier and Shahriar for their great summaries :
Tried many methods. Take it as a review to heap and piority queue…