Codility: MaxNonoverlappingSegments Solution

Lesson 16 Greedy Algorithms

Approach

Compare current's start (A) with previous' end (B)

Implementation

1function solution(A, B) {
2 const N = A.length
3 let res = N ? 1 : 0
4 let prevB = B[0]
5 for (let i = 1; i < N; i++) {
6 if (A[i] > prevB) {
7 res++
8 prevB = B[i]
9 }
10 }
11 return res
12}

Comments

Loading comments...

Tags

codility

greedy

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Squares of a Sorted Array

Map and sort or vice versa

Previous Post

Codility: TieRopes

Lesson 16 Greedy Algorithms

HoningJS

Search Posts