Skip to main content
spelling
Source Link
greybeard
  • 6.6k
  • 3
  • 20
  • 52

These problems are always looking to test. It seamedseemed way to straight forward until I realized the clue.

To be fair in JS a there there is no such thing as a listlist which helps understand the problem.

This is a storage problem

This is a storage problem

Well that is the gist of it. The solution turned out a little more complicated (it was a hard one). But it maintains a storage complexity of \$O(1)\$ and a complexity of \$O(n)\$ (was hoped it could be log(n) but edge cases made it impossible for my mind)

function partition(arr, pivot) {
    var i, temp, top = arr.length - 1, mid = 0, step = true;
    for (i = 0; i <= top - mid; i++) {
        if (mid && step) { arr[i] = arr[i + mid] } 
        step = true;
        if (arr[i] > pivot) {  
            if (arr[top] === pivot) { 
                arr[top--] = arr[i];
                arr[i] = arr[i + (++mid)];
            } else {  
                temp = arr[i]; 
                arr[i] = arr[top];
                arr[top--] = temp;
                step = false;
            }
            i--;
        } else if(arr[i] === pivot) { 
            mid++;
            i--;
        }
    }
    while (mid--) { arr[i++] = pivot }
    return arr;
}

        

These problems are always looking to test. It seamed way to straight forward until I realized the clue.

To be fair in JS a there is no such thing as a list which helps understand the problem.

This is a storage problem

Well that is the gist of it. The solution turned out a little more complicated (it was a hard one) But it maintains a storage complexity of \$O(1)\$ and a complexity of \$O(n)\$ (was hoped it could be log(n) but edge cases made it impossible for my mind)

function partition(arr, pivot) {
    var i, temp, top = arr.length - 1, mid = 0, step = true;
    for (i = 0; i <= top - mid; i++) {
        if (mid && step) { arr[i] = arr[i + mid] } 
        step = true;
        if (arr[i] > pivot) {  
            if (arr[top] === pivot) { 
                arr[top--] = arr[i];
                arr[i] = arr[i + (++mid)];
            } else {  
                temp = arr[i]; 
                arr[i] = arr[top];
                arr[top--] = temp;
                step = false;
            }
            i--;
        } else if(arr[i] === pivot) { 
            mid++;
            i--;
        }
    }
    while (mid--) { arr[i++] = pivot }
    return arr;
}

        

These problems are always looking to test. It seemed way to straight forward until I realized the clue.

To be fair in JS there is no such thing as a list which helps understand the problem.

This is a storage problem

Well that is the gist of it. The solution turned out a little more complicated (it was a hard one). But it maintains a storage complexity of \$O(1)\$ and a complexity of \$O(n)\$ (was hoped it could be log(n) but edge cases made it impossible for my mind)

function partition(arr, pivot) {
    var i, temp, top = arr.length - 1, mid = 0, step = true;
    for (i = 0; i <= top - mid; i++) {
        if (mid && step) { arr[i] = arr[i + mid] } 
        step = true;
        if (arr[i] > pivot) {  
            if (arr[top] === pivot) { 
                arr[top--] = arr[i];
                arr[i] = arr[i + (++mid)];
            } else {  
                temp = arr[i]; 
                arr[i] = arr[top];
                arr[top--] = temp;
                step = false;
            }
            i--;
        } else if(arr[i] === pivot) { 
            mid++;
            i--;
        }
    }
    while (mid--) { arr[i++] = pivot }
    return arr;
}
Commonmark migration
Source Link

##This is a storage problem

This is a storage problem

##Strategy

Strategy

##Solution \$O(1)\$ storage

Solution \$O(1)\$ storage

##This is a storage problem

##Strategy

##Solution \$O(1)\$ storage

This is a storage problem

Strategy

Solution \$O(1)\$ storage

deleted 52 characters in body
Source Link
Blindman67
  • 22.3k
  • 2
  • 16
  • 40
function partition(arr, pivot) {
    var i, temp, top = arr.length - 1, mid = 0, step = true;
    for (i = 0; i <= top - mid; i++) {
        if (mid && step) { arr[i] = arr[i + mid] } 
        step = true;
        if (arr[i] > pivot) {  
            if (arr[top] === pivot) { 
                arr[top--] = arr[i];
                arr[i] = arr[i + (++mid)];
            } else {  
                temp = arr[i]; 
                arr[i] = arr[top];
                arr[top--] = temp;
                step = false;
            }
            i--;
        } else if(arr[i] === pivot) { 
            mid++;
            i--;
        }
    }
    if (mid && step) { arr[i] = arr[i + mid] }
    while (mid--) { arr[i++] = pivot }
    return arr;
}

        
function partition(arr, pivot) {
    var i, temp, top = arr.length - 1, mid = 0, step = true;
    for (i = 0; i <= top - mid; i++) {
        if (mid && step) { arr[i] = arr[i + mid] } 
        step = true;
        if (arr[i] > pivot) {  
            if (arr[top] === pivot) { 
                arr[top--] = arr[i];
                arr[i] = arr[i + (++mid)];
            } else {  
                temp = arr[i]; 
                arr[i] = arr[top];
                arr[top--] = temp;
                step = false;
            }
            i--;
        } else if(arr[i] === pivot) { 
            mid++;
            i--;
        }
    }
    if (mid && step) { arr[i] = arr[i + mid] }
    while (mid--) { arr[i++] = pivot }
    return arr;
}

        
function partition(arr, pivot) {
    var i, temp, top = arr.length - 1, mid = 0, step = true;
    for (i = 0; i <= top - mid; i++) {
        if (mid && step) { arr[i] = arr[i + mid] } 
        step = true;
        if (arr[i] > pivot) {  
            if (arr[top] === pivot) { 
                arr[top--] = arr[i];
                arr[i] = arr[i + (++mid)];
            } else {  
                temp = arr[i]; 
                arr[i] = arr[top];
                arr[top--] = temp;
                step = false;
            }
            i--;
        } else if(arr[i] === pivot) { 
            mid++;
            i--;
        }
    }
    while (mid--) { arr[i++] = pivot }
    return arr;
}

        
deleted 6 characters in body
Source Link
Blindman67
  • 22.3k
  • 2
  • 16
  • 40
Loading
Source Link
Blindman67
  • 22.3k
  • 2
  • 16
  • 40
Loading