K Closest Points to Origin | Leetcode

Поделиться
HTML-код
  • Опубликовано: 8 окт 2024

Комментарии • 68

  • @BakijanRahman
    @BakijanRahman 4 года назад +4

    Python3:
    class Solution:
    def sorting_func(self, lst):
    return lst[0]**2 + lst[1]**2
    def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
    sorted_lst = sorted(points, key= self.sorting_func)
    return sorted_lst[0:K]

  • @subham-raj
    @subham-raj 4 года назад +15

    I waited for *quick select* algorithm :(

    • @techdose4u
      @techdose4u  4 года назад +1

      Dint think of applying it 😅 could have been much better since order was not important. Thanks for the suggestion.

    • @somaanilsreeram9555
      @somaanilsreeram9555 3 года назад

      I was expecting Quick Select too. The interviewer would not be impressed if Multi-Map was used to solve this.

  • @mansiagrawal4039
    @mansiagrawal4039 3 года назад +4

    After seeing your videos The logic becomes so easier that we don't need to see the code and we remember it.
    Keep going :)

    • @techdose4u
      @techdose4u  3 года назад

      Nice :) then I guess I won't have to upload code 😂

    • @mansiagrawal4039
      @mansiagrawal4039 3 года назад +1

      @@techdose4u 😂 Of course but you have to keep others in mind too 😅

    • @techdose4u
      @techdose4u  3 года назад

      @@mansiagrawal4039 Sure :)

  • @rimajain2374
    @rimajain2374 4 года назад +5

    Simple easy one:
    public int[][] kClosest(int[][] points, int K) {
    Arrays.sort(points, Comparator.comparing(p -> p[0] * p[0] + p[1] * p[1]));
    return Arrays.copyOfRange(points, 0, K);
    }

    • @Star_Bawa9
      @Star_Bawa9 2 года назад

      Hey can you please explain this Arrays.sort Function And yeah could you explain this code as well PriorityQueue maxheap=new PriorityQueue((a,b)->(b[0]*b[0]+b[1]*b[1])-(a[0]*a[0]+a[1]*a[1]));

  • @nagajaswanth831
    @nagajaswanth831 4 года назад +2

    did it in python : )
    class Solution(object):
    def kClosest(self, points, K):
    d={}
    if len(points)==0:
    return
    for t,i in enumerate(points):
    d[t]=i[0]**2+i[1]**2
    x=d.items()
    x.sort(key=lambda x:x[1])
    l=[]
    for i in x:
    l.append(points[i[0]])
    return l[:K]

  • @Tensor08
    @Tensor08 3 года назад

    Please upload with divide and conquer approach. Your explaination is awesome

  • @twrskshm
    @twrskshm 3 года назад +2

    We don't need sorting for this problem, we can solve it in O(n) using kth order statistics.

    • @techdose4u
      @techdose4u  3 года назад

      👍🏼

    • @Star_Bawa9
      @Star_Bawa9 2 года назад

      Hi I am a beginner please can you give some idea about the concept which you are telling

  • @vlajkojankov3149
    @vlajkojankov3149 2 месяца назад

    thx, love the explanation

  • @santhoskumar1369
    @santhoskumar1369 4 года назад +1

    Pair of pair vector is redundant right? We could have simply used a custom comparator in sort function

    • @techdose4u
      @techdose4u  4 года назад +1

      Could have taken just one pair with index value of points like I showed in multimap.

  • @abhireddy8164
    @abhireddy8164 4 года назад +2

    If u have time please do a question on avl insertion and deletion sir.Atleast add to ur Queue.

    • @techdose4u
      @techdose4u  4 года назад +4

      AVL will be added but one video per day is my limit. Since I will be continuing the June challenge. It won't be possible in JUNE either. Please remind when challenges are over.

    • @abhireddy8164
      @abhireddy8164 4 года назад

      Ok sir fine

    • @spetsnaz_2
      @spetsnaz_2 4 года назад

      Yes... We want red black and avl tree videos

  • @ridimamittal4064
    @ridimamittal4064 3 года назад +1

    in method 1 , 2 is misplaced in distance

  • @shresthmishra9329
    @shresthmishra9329 4 года назад +1

    multimap is new for me.
    Confused in this line below. When iterator is pointing to map.begin() then how are we pushing points[it->second] ??
    ans.push_back(points[it->second]);

    • @techdose4u
      @techdose4u  4 года назад +1

      it->second is the index of point contained in points vector as I explained in code walkthrough. I am just pushing the coordinate X,Y using the index.

    • @shresthmishra9329
      @shresthmishra9329 4 года назад +1

      @@techdose4u ok makes sense. Thank you 🙏

    • @techdose4u
      @techdose4u  4 года назад

      👍

  • @kunalchhatwani2309
    @kunalchhatwani2309 4 года назад +1

    Hey , wouldn't it be better for u as well as people who want to learn that u preferred making videos topicwise for competitive programming and for challenges like that of leetcode make a one-in-all video like errichto . Just a suggestion
    Awesome amount of work anyways you put in.

    • @techdose4u
      @techdose4u  4 года назад

      Then nobody will understand. I start doing what Errichto does then people who don't understand there will not understand here as well. Moreover, I am tired of making daily videos. I am just continuing coz I have committed. I prefer making 2 videos per week chosen by me. That gives me ample time to make videos on good topics.

    • @kunalchhatwani2309
      @kunalchhatwani2309 4 года назад

      @@techdose4u yup i know learning new concepts would help a lot like u said even if it came 2 videos per week but anyways great work , much appreciated.

  • @AnkitSingh-zj2uc
    @AnkitSingh-zj2uc 4 года назад +2

    I want to know one thing
    I am preparing for product base companies and i am not doing competitive programming instead of this i am solving problems on leetcode and i have done so many problems on GFG
    And yes i do belong to a tier 3 college 🙂 is this approach good? or should i start competitive programming

    • @techdose4u
      @techdose4u  4 года назад +4

      Very good approach. No need of CP. Those who do it is either out of passion or they are following someone. Leetcode is enough to get placed.

    • @AnkitSingh-zj2uc
      @AnkitSingh-zj2uc 4 года назад

      @@techdose4u thank you so much
      I am on the right path🤩

    • @ridimamittal4064
      @ridimamittal4064 3 года назад

      @@techdose4u thank you so much sir , you are actually helping many students with a very clear and good approach :)

    • @sougatoghosh8467
      @sougatoghosh8467 3 года назад

      DO CP!!!!

  • @biboswanroy6699
    @biboswanroy6699 4 года назад +1

    Its O(klogk + (n-k)*2logk) right? Becoz for k+1 onwards we both insert and then remove max value, n-k times

  • @abhishekgautam1063
    @abhishekgautam1063 4 года назад +1

    Submitted this solution using a custom comparator function.
    bool compare(vector&a, vector&b )
    {
    int distance1 = a[0]*a[0] + a[1]*a[1];
    int distance2 = b[0]*b[0] + b[1]*b[1];

    return distance1

  • @deepakkumar-fp8uj
    @deepakkumar-fp8uj 3 года назад

    Thank you sir!

  • @agileprogramming7463
    @agileprogramming7463 4 года назад

    Sir I used min-heap to solve the problem.Is it more efficient than the multimap approach?
    class Point{

    int x;
    int y;
    public:
    Point(int _x, int _y){
    x=_x;
    y=_y;
    }

    int getX() const { return x; }
    int getY() const { return y; }
    int getDist() const { return x*x+y*y; }
    };
    class myComparator
    {
    public:
    int operator() (const Point& p1, const Point& p2)
    {
    return p1.getDist() > p2.getDist();
    }
    };
    class Solution {
    public:
    vector kClosest(vector& points, int K) {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // Creates a Min heap of points (order by distance from origin)
    priority_queue min_heap;

    vector result;
    for(int i = 0;i

  • @sarthakagarwal8499
    @sarthakagarwal8499 4 года назад +1

    Divide and Conquer will do it in O(n)

    • @techdose4u
      @techdose4u  4 года назад

      Please share the CODE.

    • @sarthakagarwal8499
      @sarthakagarwal8499 4 года назад

      @@techdose4u leetcode.com/problems/k-closest-points-to-origin/solution/

    • @sakthim7160
      @sakthim7160 4 года назад

      In worst case it will take O(N^2).
      Only average case will take O(n).

    • @sarthakagarwal8499
      @sarthakagarwal8499 4 года назад

      @@sakthim7160 Exactly..

  • @evgeni-nabokov
    @evgeni-nabokov 4 года назад +1

    Rust:
    points.sort_unstable_by_key(|p| p[0].pow(2) + p[1].pow(2));
    points[..k as usize].to_vec()

  • @khushbukella3580
    @khushbukella3580 4 года назад

    what is time complexcity for multimap solution

    • @yatinarora1252
      @yatinarora1252 3 года назад

      O(n log N) for inserting logn in map and n is size of vector

  • @princeanthony8525
    @princeanthony8525 Год назад

    Quick select dude.

  • @BoobalanMunusamy
    @BoobalanMunusamy 4 года назад +1

    I'm really sorry to give -1 for the first time on your channel. I was expecting a divide & Conquer algorithm because you solution is nowhere in the performance graph submitted by other people.

    • @kushgupta1187
      @kushgupta1187 4 года назад +1

      Heap solution is the fastest one

    • @techdose4u
      @techdose4u  4 года назад

      Divide and conquer is not fast. Did you try both the methods? How much fast solution do you expect?

    • @techdose4u
      @techdose4u  4 года назад +1

      Yes. I already explained how it is faster than other methods.I cannot cover each and every algo for a question, people should try them out.

    • @kushgupta1187
      @kushgupta1187 4 года назад

      Hi tech dose yes u r right but your role is now a mentor.. And with great power comes great responsibility. People will always look after you for best solution. You have to take time to do best solution, otherwise there are lots of mentors in RUclips providing solutions. Your knowledge and way of explanation will only makes you stand out among others.

    • @BoobalanMunusamy
      @BoobalanMunusamy 4 года назад

      @@techdose4u If you look at leetcode performance chart, all 90%+ best performance solutions are in divide & conquer. what is your heat chart performance % in leet code chart ?

  • @deep8843
    @deep8843 4 года назад +1

    bhai thanku yaar😂

  • @rajeshbammidy180
    @rajeshbammidy180 4 года назад +1

    But still Heap one is better one tho