Question: I have come across the following problem statement:

> 
> You have a sentence written entirely in a single row. You would like to split it into several rows by replacing some of the spaces
>  with "new row" indicators. Your goal is to minimize the width of the
>  longest row in the resulting text ("new row" indicators do not count
>  towards the width of a row). You may replace at most K spaces.
> 
> 
> You will be given a sentence and a K. Split the sentence using the
>  procedure described above and return the width of the longest row.
> 
> 
> 

I am a little lost with where to start. To me, it seems I need to try to figure out every possible sentence length that satisfies the criteria of splitting the single sentence up into K lines.

I can see a couple of edge cases:

1. There are <= K words in the sentence, therefore return the longest word.
2. The sentence length is 0, return 0

If neither of those criteria are true, then we have to determine all possible combinations of splitting the sentence and the return the minimum of all those options. This is the part I don't know how to do (and is obviously the heart of the problem).

Answer: 
This got me 100%

```
function solution(A) {
if (A.length === 0 || !A) {
    return 1;
}

A.sort((a, b) => a - b);
let count = A[0];
if (count !== 1) { return 1 }
for (let i = 0; i <= A.length; i++) {
    if (A[i + 1] === count + 1) {
        count ++;
        continue
    }
    return count + 1;
}

```

}
I had the same problem with my code:

```
function solution(A) {
    var i, next;

    A.sort();

    next = 1;
    for (i=0; i<A.length; i++){
        if (A[i] != next) return next;
        next++;
    }

    return next;
}

```

Altough it scored 100% in correctness, it returned wrong answer in all the performance tests.

The same code in C receives 100% on both:

```
int cmpfunc (const void * a, const void * b){
   return ( *(int*)a - *(int*)b );
}

int solution(int a[], int n) {
    int i, next;

    qsort(a, n, sizeof(int), cmpfunc);

    next = 1;
    for (i=0; i<n; i++){
        if (a[i] != next) return next;
        next++;
    }

    return next;
}

```