Codility Number of Disc intersections

Sdílet
Vložit
  • čas přidán 6. 09. 2024
  • My solution to Codility Number of Disc intersections. This is the third task in the sorting lesson.
    NumberOfDiscIntersections is a respectable exercise in the sorting lesson on Codility. The goal is to find the number of intersections between discs that are specified in an array. The index of the array represents the position of the disc on the x-axis and the value in the array represents the disc's radius.
    This video shows my Java solution that scores 100% on Codility.

Komentáře • 20

  • @kabeltm68
    @kabeltm68 Před 3 lety +3

    The best explanation on the internet.

  • @phamhung142
    @phamhung142 Před 8 měsíci +1

    Your solution is great. thanks Dave.

    • @javadave
      @javadave  Před 8 měsíci

      Thanks for saying so and for watching.

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

    Hi Dave, your videos are great! Keep it on

  • @bipeshav8730
    @bipeshav8730 Před 2 lety

    Great video, thanks for this comprehensive explanation :)

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

    I can't understand clear how we create the array in minute 6 .. can you signefecent the strategy more please ?
    and thank a lot ☺️

    • @javadave
      @javadave  Před 3 lety +3

      Hi. This array is how many discs have started at this point on the X-axis (eg. the left edge of the disc covers this point or is further to the left of this point). So, at position 0, 4 discs have started (the discs with centre positions 0, 1, 2 and 4). At the next position, no new disc starts, so the value is still 4. Then at the next position, disc with 3 at the centre starts, so now we add this to the 4 to get 5. It carries on like this. I hope this makes sense.

  • @Mahmoud-xe8hz
    @Mahmoud-xe8hz Před 3 lety

    Hey Dave here is my Solution kind regards!!
    public int discIntersections(int[] a) {
    int[] start = new int[a.length]; int[] end = new int[a.length];
    for (int i = 0; i < a.length; i++) {
    // get start and end of circle
    start[i] = i - a[i];end[i] = i + a[i];
    }
    int count = 0;
    for (int i = 0; i < a.length; i++) {
    int s = start[i];
    int e = end[i];
    for (int j = i + 1; j< a.length ; j++) {
    //First condition if a circle contain a circle e.q circle 2(-4,6) contain circle 0 (-1,+1)
    //Second condition e.g c0 (-1,1) c2 (0,2) 1>0>-1
    //Third condition r.g c0(-1,1) c2(0,2) but if we check from left
    if ((start[j] = e) |(start[j]>=s && start[j] =s && end[j]

    • @javadave
      @javadave  Před 3 lety

      Great, a bit shorter than my solution. Did you try it in Codility?

    • @Mahmoud-xe8hz
      @Mahmoud-xe8hz Před 3 lety

      @@javadave Hi, no i do not try in Codility

    • @shakilahmed4647
      @shakilahmed4647 Před 3 měsíci

      @@Mahmoud-xe8hz this is good but time complexity is NxN

  • @terrencechao232
    @terrencechao232 Před 3 lety

    Hi, Dave.
    I don't get it about max(4,4) -1, max(4, 6) - 1.... since 6:47. I cannot catch your mind. Did I miss anything?

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

      Hi Terrence... this one was one of my favourite problems and very trickey. By max(4,4) I mean just the maximum number between 4 and 4 (4 in this case).... Max(4,6)=6, etc. I hope that makes sense.

  • @romawar1869
    @romawar1869 Před 3 lety

    Hello Dave

  • @jetlavi
    @jetlavi Před 3 lety

    I will be honest with you, I've gotten lost to understand the task :(

    • @javadave
      @javadave  Před 3 lety

      Yes, this one confused me quite a bit. That's why there's a break in the video when I work it out.