Find K Closest Elements | Leetcode 658 Solution | Searching and Sorting

Sdílet
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

Komentáře • 38

  • @ayushiagarwal6996
    @ayushiagarwal6996 Před 3 lety +5

    wonderful explanation taking all cases into considereation. Truly loved it. Thank you!!

  • @DurgaShiva7574
    @DurgaShiva7574 Před 2 lety +1

    salute u mam.. the way u teach, hats off mam..

    • @Pepcoding
      @Pepcoding  Před 2 lety

      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/

  • @surajdangi6492
    @surajdangi6492 Před rokem

    madam god level smjati hai

  • @prashantagrawal1140
    @prashantagrawal1140 Před 2 lety

    Gr8 explanation

  • @shobhitkumar9452
    @shobhitkumar9452 Před 3 lety

    well explained!

    • @Pepcoding
      @Pepcoding  Před 3 lety +1

      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

    • @shobhitkumar9452
      @shobhitkumar9452 Před 3 lety

      @@Pepcoding yes of course. I have reviewed already.

  • @RishabhJain-hr6sz
    @RishabhJain-hr6sz Před 3 lety

    Acha explanation tha mam!

    • @Pepcoding
      @Pepcoding  Před 3 lety +1

      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

  • @muditjain9117
    @muditjain9117 Před 3 lety +1

    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?

  • @himanshuchhikara4918
    @himanshuchhikara4918 Před 3 lety +2

    mam iska binary search k through soln bhi aayega bcz interview mai to O(log(n) + k*logk) hi mangte hai

  • @nitishprasadkushwaha
    @nitishprasadkushwaha Před rokem

    Op in the chat

  • @SHASHANKRUSTAGII
    @SHASHANKRUSTAGII Před 2 lety

    This was asked in microsoft

  • @aakashsharma5901
    @aakashsharma5901 Před 3 lety +5

    Well explained mam specially analysis of time complexity at last was damn good

    • @Pepcoding
      @Pepcoding  Před 3 lety

      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

  • @rosonerri-faithful
    @rosonerri-faithful Před 3 lety +1

    Manisha Mam ka explanation super heii😊 ..kaash hamari coolege ki mam ese padha pati 😪

    • @Pepcoding
      @Pepcoding  Před 3 lety

      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😊

  • @himanshujoshi1124
    @himanshujoshi1124 Před rokem

    i don't get it if we are using pq then why compare for largest just pop the topmost for k+1 ele

  • @coding8453
    @coding8453 Před 2 lety

    Where is this pepcoding question page?

  • @kapilsingh2816
    @kapilsingh2816 Před rokem

    have we done operator overloading here?

  • @user-il8yt6gm3y
    @user-il8yt6gm3y Před rokem

    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;
    }

  • @ialgorithms
    @ialgorithms Před rokem

    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])]

  • @arnabmukherjee4672
    @arnabmukherjee4672 Před 3 lety

    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 ?

  • @jasmeenkaur6001
    @jasmeenkaur6001 Před 3 lety

    mam plz explain the code of cpp as well................

  • @indranilchakraborty5949

    mam ap ne jab gap same hoga usko manage krna bhul gye hai...

  • @DurgaShiva7574
    @DurgaShiva7574 Před 2 lety

    one small question the priority queue could also had been created by using comparator also right?..please guide if yes, then how?

    • @Pepcoding
      @Pepcoding  Před 2 lety

      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/

  • @shreyjain2357
    @shreyjain2357 Před 2 lety +1

    GFG par TLE aa raha is solution se

  • @lovleshbhatt7797
    @lovleshbhatt7797 Před 3 lety

    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