Skip to content

Instantly share code, notes, and snippets.

@wxgrzr
Last active November 29, 2021 05:42
Show Gist options
  • Save wxgrzr/6deb39b4bde1c6078321a0ffba0e6da5 to your computer and use it in GitHub Desktop.
Save wxgrzr/6deb39b4bde1c6078321a0ffba0e6da5 to your computer and use it in GitHub Desktop.

Revisions

  1. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 8 additions and 10 deletions.
    18 changes: 8 additions & 10 deletions 1-SlidingWindow.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    # Find Maximum in Sliding Window

    [Link](https://www.educative.io/module/lesson/data-structures-in-javascript/gxnlB9N5MR9)
    @@ -7,20 +6,20 @@
    ## Description

    Given a large array of integers and a window size of `w` find the current maximum value in the window as the window slides through the entire array.

    Let's try to find all maximums for `w = 3`:

    [-4, 2, -5, 3, 6]

    **Step 1:** for the first *3* elements, max is 2.

    [-4, 2, -5], 3, 6
    *
    **Step 2:** Slide `window` one position to right and the max is now *3*
    *

    **Step 2:** Slide `window` one position to right and the max is now *3*

    -4, [2, -5, 3], 6
    *
    *

    **Step 3:** In the last `window` the max is 6.

    @@ -71,7 +70,7 @@ console.log("Array = " + array);
    console.log("Max = " + ffindMaxSlidingWindow(array, 3));
    // Max = 3,4,5,6,7,8,9,10
    let array1 = [10, 6, 9, -3, 23, -1, 34, 56, 67, -1, -4, -8, -2, 9, 10, 34, 67]
    let array1 = [10, 6, 9, -3, 23, -1, 34, 56, 67, -1, -4, -8, -2, 9, 10, 34, 67]
    console.log("Array = " + array1);
    console.log("Max = " + ffindMaxSlidingWindow(array1, 3))
    // Max = 10,9,23,23,34,56,67,67,67,-1,-2,9,10,34,67
    @@ -88,4 +87,3 @@ The runtime complexity in *`linear, O(n)`*.
    Memory complexity is *`linear, O*(w)`*, where `w` is the window size



  2. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 0-Index.md
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,5 @@
    Educative Modules

    [[1-SlidingWindow]]
    [[2-RotatedArray]]
    [[DSAX-Modules/educative.io/2-RotatedArray]]

  3. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Rotated Array Search

    [[0-Index]]
    [[0-Index]]

    ## Description

  4. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Rotated Array Search

    [[0-Index]]
    [[0-Index]]

    ## Description

  5. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -204,4 +204,4 @@ This solution works iteratively so te algo comes down to **`O(1)`** even though

    ---

    Next - [[3-]]
    Next - [[3-SmallestCommonNumber.md]]
  6. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -204,4 +204,4 @@ This solution works iteratively so te algo comes down to **`O(1)`** even though

    ---

    Next - [[]]
    Next - [[3-]]
  7. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -204,4 +204,4 @@ This solution works iteratively so te algo comes down to **`O(1)`** even though

    ---

    Next - []
    Next - [[]]
  8. wxgrzr revised this gist Nov 29, 2021. No changes.
  9. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -202,6 +202,6 @@ console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te algo comes down to **`O(1)`** even though the logic remains the same

    --
    ---

    Next -
    Next - []
  10. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -202,4 +202,6 @@ console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te algo comes down to **`O(1)`** even though the logic remains the same

    Next -
    --

    Next -
  11. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -202,3 +202,4 @@ console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te algo comes down to **`O(1)`** even though the logic remains the same

    Next -
  12. wxgrzr revised this gist Nov 29, 2021. No changes.
  13. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -202,4 +202,3 @@ console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te algo comes down to **`O(1)`** even though the logic remains the same


  14. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -200,4 +200,6 @@ let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te algo comes down to **`O(1)`**
    This solution works iteratively so te algo comes down to **`O(1)`** even though the logic remains the same


  15. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -200,4 +200,4 @@ let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te algo comes down to **``**
    This solution works iteratively so te algo comes down to **`O(1)`**
  16. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -200,4 +200,4 @@ let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te algo comes down to **``
    This solution works iteratively so te algo comes down to **``**
  17. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -200,4 +200,4 @@ let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te algo comes down to ``
    This solution works iteratively so te algo comes down to **``
  18. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -200,4 +200,4 @@ let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te algo comes down to
    This solution works iteratively so te algo comes down to ``
  19. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -200,4 +200,4 @@ let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te al
    This solution works iteratively so te algo comes down to
  20. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -200,4 +200,4 @@ let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively so te
    This solution works iteratively so te al
  21. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -200,4 +200,4 @@ let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```
    This solution works iteratively
    This solution works iteratively so te
  22. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -200,4 +200,4 @@ let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```

    This solution works iteratively
  23. wxgrzr revised this gist Nov 29, 2021. No changes.
  24. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -150,7 +150,7 @@ The memory complexity of this solution is constant
    `O(1)`.


    ```
    ```javascript
    let binarySearchRotated = function(arr, key) {

    start = 0;
    @@ -198,6 +198,6 @@ console.log("Key(6) found at: " + binarySearchRotated(v1, 6));
    let v2 = [4, 5, 6, 1, 2, 3];

    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```

  25. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -150,5 +150,54 @@ The memory complexity of this solution is constant
    `O(1)`.


    ```
    let binarySearchRotated = function(arr, key) {
    start = 0;
    end = arr.length - 1;
    if (start > end){
    return -1;
    }
    while (start <= end){
    mid = start + Math.floor((end - start) / 2);
    if (arr[mid] == key){
    return mid;
    }
    if (arr[start] <= arr[mid] && key <= arr[mid] && key >= arr[start]){
    end = mid - 1;
    }
    else if (arr[mid] <= arr[end] && key >= arr[mid] && key <= arr[end]){
    start = mid + 1;
    }
    else if (arr[end] <= arr[mid]){
    start = mid + 1;
    }
    else if (arr[start] >= arr[mid]){
    end = mid - 1;
    }
    else{
    return -1;
    }
    }
    return -1;
    };
    let v1 = [6, 7, 1, 2, 3, 4, 5];
    console.log("Key(3) found at: " + binarySearchRotated(v1, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v1, 6));
    let v2 = [4, 5, 6, 1, 2, 3];
    console.log("Key(3) found at: " + binarySearchRotated(v2, 3));
    console.log("Key(6) found at: " + binarySearchRotated(v2, 6));
    ```

  26. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -150,3 +150,5 @@ The memory complexity of this solution is constant
    `O(1)`.




  27. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -146,6 +146,7 @@ The runtime complexity of this solution is logarithmic
    `O(log n)`.

    ### Memory complexity
    The memory complexity of this solution is constant, `O(1)`.
    The memory complexity of this solution is constant
    `O(1)`.


  28. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -146,6 +146,6 @@ The runtime complexity of this solution is logarithmic
    `O(log n)`.

    ### Memory complexity
    The memory complexity of this solution is constant, O(1)O(1)`.
    The memory complexity of this solution is constant, `O(1)`.


  29. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -146,6 +146,6 @@ The runtime complexity of this solution is logarithmic
    `O(log n)`.

    ### Memory complexity
    The memory complexity of this solution is constant, O(1)O(1).
    The memory complexity of this solution is constant, O(1)O(1)`.


  30. wxgrzr revised this gist Nov 29, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2-RotatedArray.md
    Original file line number Diff line number Diff line change
    @@ -143,7 +143,7 @@ Solution 2: Iterative

    ### Runtime complexity
    The runtime complexity of this solution is logarithmic
    , `O(log n)`.
    `O(log n)`.

    ### Memory complexity
    The memory complexity of this solution is constant, O(1)O(1).