clueless coding // TODO: be smarter

LeetCode Kth Largest Element in an Array: CPP Solution



TL;DR Code Solution


     int findKthLargest(vector<int>& nums, int k) 
	{
		size_t len = nums.size();
		if(len < k) return 0;

		priority_queue<int, std::vector<int>, std::greater<int>> q;
		for(auto &v : nums)
		{
			if(q.size() < k)
			{
				q.push(v);
			}
			else if (v > q.top())
			{
				q.pop();
				q.push(v);
			}
		}
        return q.top();
    }