O(n) loop and execute a trade if and only if you can afford it, and also you can gain profit from it.
For any profitable but not yet affordable Business, put them in a minimum Heap.
Finally, check the heap for anything that you can afford and execute anything that's possible.
At last, you can no longer possibly afford anything else, thus return the latest K right away.
Read full article from Algorithms, Data Structures and Big Data Hadoop Recommender System: LintCode 970. Big Business - Weekly13 - LintCode
No comments:
Post a Comment