Codility: TieRopes Solution

Lesson 16 Greedy Algorithms

Approach

Accumulated sum, reset when exceed

K

Implementation

1function solution(K, A) {
2 let sum = 0
3 let res = 0
4 for (const a of A) {
5 sum += a
6 if (sum >= K) {
7 res++
8 sum = 0
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

Codility: MaxNonoverlappingSegments

Jan 30, 2021

Lesson 16 Greedy Algorithms

Previous Post

HoningJS

Search Posts