Find K Closest Elements | Leetcode 658 Solution | Searching and Sorting
Vložit
- čas přidán 3. 05. 2021
- Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. For a better experience and more exercises, VISIT: www.pepcoding.com/resources/o...
Have a look at our result: www.pepcoding.com/placements
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education
wonderful explanation taking all cases into considereation. Truly loved it. Thank you!!
Glad it was helpful!
salute u mam.. the way u teach, hats off mam..
Hope you like the video.
For better experience and precisely organised content
visit- nados.pepcoding.com
Don't forget to follow us on Instagram
instagram.com/pepcoding/
madam god level smjati hai
Gr8 explanation
well explained!
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
If you like our efforts, we request a review
g.page/Pepcoding/review?rc
You can subscribe to our channel here
czcams.com/users/Pepcodingabout?view_as=subscriber
@@Pepcoding yes of course. I have reviewed already.
Acha explanation tha mam!
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
If you like our efforts, we request a review
g.page/Pepcoding/review?rc
You can subscribe to our channel here
czcams.com/users/Pepcodingabout?view_as=subscriber
Mam, in the Else block when size of PQ is > k you have compared gap of peek element with current element, what if it was same then we should compare on basis of val too, right?
yeah
mam iska binary search k through soln bhi aayega bcz interview mai to O(log(n) + k*logk) hi mangte hai
Ji, wo optimised approach ki video alag se bnayi hai. wo bhi upload hogi.
Sure Jatin, Thank you.
Op in the chat
This was asked in microsoft
Well explained mam specially analysis of time complexity at last was damn good
I am glad you liked it. I also hope that you are watching till end and trying to understand the what, how and especially why of the problem.
If you like our efforts, we request a review
g.page/Pepcoding/review?rc
You can subscribe to our channel here
czcams.com/users/Pepcodingabout?view_as=subscriber
Manisha Mam ka explanation super heii😊 ..kaash hamari coolege ki mam ese padha pati 😪
Glad to know that you liked the content and thank you for appreciating.
The love and respect which I get from you people keep me highly motivated and the same I am able to forward It to you people through my videos.
So, keep motivating, keep learning and keep loving Pepcoding😊
i don't get it if we are using pq then why compare for largest just pop the topmost for k+1 ele
Where is this pepcoding question page?
have we done operator overloading here?
Simple Java Solution
----------------------------------
static class Pair implements Comparable{
int gap;
int val;
Pair(int gap, int val) {
this.gap = gap;
this.val = val;
}
@Override
public int compareTo(Pair o) {
if(this.gap == o.gap){
return this.val - o.val;
}
else{
return this.gap - o.gap;
}
}
}
static ArrayList findClosest(int[] arr, int x, int k) {
ArrayList al = new ArrayList();
PriorityQueue pq = new PriorityQueue(Collections.reverseOrder());
for (int i : arr) {
pq.add(new Pair(Math.abs(i - x), i));
if(pq.size() > k){
pq.poll();
}
}
while(!pq.isEmpty()){
al.add(pq.poll().val);
}
Collections.sort(al);
return al;
}
Python:
class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
heap = []
for i in arr:
gap = abs(i - x)
if len(heap) < k:
heapq.heappush(heap, [-abs(i-x), i])
elif gap < -heap[0][0]:
heapq.heappop(heap)
heapq.heappush(heap, [-gap, i])
elif gap == -heap[0][0] and i < heap[0][1]:
heapq.heappop(heap)
heapq.heappush(heap, [-gap, i])
return [x[1] for x in sorted(heap, key = lambda x:x[1])]
d = [(i, abs(i-x)) for i in arr]
d.sort(key= lambda x:x[1])
return sorted([i[0] for i in d[:k]])
Ma'am ye python sol Kaisa h ?
mam plz explain the code of cpp as well................
mam ap ne jab gap same hoga usko manage krna bhul gye hai...
one small question the priority queue could also had been created by using comparator also right?..please guide if yes, then how?
For better experience and well-organised content
Visit - nados.pepcoding.com
You can post your query on community tab.
Don't forget to follow us on Instagram
instagram.com/pepcoding/
GFG par TLE aa raha is solution se
mera to WA aa rha h , can u share the code?
If someone stuck at any point , you can take help from my code
vector findClosestElements(vector& arr, int k, int x) {
vectorvec;
for(auto i: arr)
vec.push_back({abs(i-x),i});
sort(vec.begin(),vec.end());
vectorans;
for(int i=0;i
its nlogn so bad code