Codility MaxNonoverlappingSegments Java solution

  Рет қаралды 1,604

Dave Kirkwood

Dave Kirkwood

2 жыл бұрын

MaxNonoverlappingSegments in the first exercise in the Greedy algorithms lesson on Codility. The aim is to find the maximum number of segments that don't overlap in a set of segments defined in two given arrays.
The segments are pre-sorted by end position. I keep track of the current position, which is the end of each segment that I consider, I then scan over the arrays to find lines that start after the current position and count the segments when I find a line that doesn't overlap.
This solution scores 100% on Codility.

Пікірлер: 9
@bootagain
@bootagain 2 жыл бұрын
Thanks for your valuable video!
@javadave
@javadave 2 жыл бұрын
Glad it was helpful!
@WhiteSiroi
@WhiteSiroi Жыл бұрын
Great vid and explanation, helped me a lot, thank you DK
@javadave
@javadave Жыл бұрын
Thanks WS!
@huynhviet5154
@huynhviet5154 2 жыл бұрын
How long does it take you to think about the silution?
@javadave
@javadave 2 жыл бұрын
Some I can do straight away and others take a bit of time. I think for me, about 2 hours is the maximum. I've been coding for years.
@NivagSwerdna
@NivagSwerdna 2 жыл бұрын
I think you got lucky there... doesn't this only work when the first segment is in the largest set? Inevitably the loop with pick up A[0] since that is going to have A[0]>-1
@NivagSwerdna
@NivagSwerdna 2 жыл бұрын
I'm thinking that B[K] ≤ B[K + 1] guarantees your solution but having a hard time wrapping my head around it!
@javadave
@javadave 2 жыл бұрын
@@NivagSwerdna Hi. Its a while since I did this, but having a quick look at it now, I think it works because the lines are sorted by end position. The first one is definitely going to end before any subsequent ones, so when we're looking for another one that starts after it, the first one we find will always do. I think the problem would be a lot harder if they weren't sorted like this.
Codility TieRopes Java solution
7:59
Dave Kirkwood
Рет қаралды 998
Codility MinAbsSum Java solution
30:54
Dave Kirkwood
Рет қаралды 2,2 М.
THEY WANTED TO TAKE ALL HIS GOODIES 🍫🥤🍟😂
00:17
OKUNJATA
Рет қаралды 11 МЛН
Codility MinMaxDivision solution
19:30
Dave Kirkwood
Рет қаралды 3,2 М.
Non overlapping intervals | Leetcode #435
15:29
Techdose
Рет қаралды 35 М.
Codility Count Distinct Slices solution
16:20
Dave Kirkwood
Рет қаралды 2,4 М.
Coding Interview | Software Engineer @ Bloomberg (Part 1)
30:05
Keep On Coding
Рет қаралды 4,6 МЛН
Codility Number of Disc intersections
24:43
Dave Kirkwood
Рет қаралды 3,7 М.
Why Good Programmers FAIL Coding Interviews
8:15
Sahil & Sarra
Рет қаралды 337 М.
git rebase - Why, When & How to fix conflicts
9:45
Philomatics
Рет қаралды 30 М.
Codility Ladder
15:23
Dave Kirkwood
Рет қаралды 1 М.
Async JavaScript & Callback Functions -- Tutorial for Beginners
24:21
Object Oriented Programming - The Four Pillars of OOP
11:23
Keep On Coding
Рет қаралды 317 М.
THEY WANTED TO TAKE ALL HIS GOODIES 🍫🥤🍟😂
00:17
OKUNJATA
Рет қаралды 11 МЛН