Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1985] Comments on The Choice of Sorting #8

Open
charmhe opened this issue Mar 9, 2022 · 0 comments
Open

[1985] Comments on The Choice of Sorting #8

charmhe opened this issue Mar 9, 2022 · 0 comments

Comments

@charmhe
Copy link

charmhe commented Mar 9, 2022

Based on the LCP string comparison algorithm you provided, it should be more clear to use a selection algorithm instead of the sorting on the whole array.

string kthLargestNumber(vector<string>& nums, int k) {
    nth_element(nums.begin(), nums.begin() + k - 1, nums.end(), [](auto& a, auto& b) {
        // return true if a is lexicographical larger than b.
        int N = a.size(), M = b.size();
        if (N > M) return true;
        if (N < M) return false;
        for (int i = 0; i < N; ++i) {
            if (a[i] != b[i])
                return a[i] > b[i];
        }
        return false;
    });
    return nums[k-1];    
}
@charmhe charmhe changed the title [1985] Comments [1985] Comments on The Choice of Sorting Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant