[Solutions to problems] Arrays & Dynamic Arrays

[Solutions to problems] Arrays & Dynamic Arrays

3fafd51aa8dad766208e11eaaa1d534c?s=128

Pablo Ángel Trinidad

May 28, 2020
Tweet

Transcript

  1. 3.

    Arrays Definition Collection of values of the same type, stored

    in contiguous memory, accessible by an index.
  2. 6.

    2-dimensional array ✅ Values of the same type • First

    dimension stores values of type: “arrays of 5 integers”. • Second dimension stores values of type: “integer”
  3. 10.

    2-dimensional array ❓ Accessible by and index A[i][j] = ?

    Hint: For a 1-dimensional array , the i-th element can be located at address A A[i] = S + ti
  4. 11.

    2-dimensional array ❓ Accessible by and index A[i][j] = ?

    Solution: For a 2-dimensional array , an A A[i] = S + it1
  5. 12.

    2-dimensional array ❓ Accessible by and index A[i][j] = ?

    Solution: For a 2-dimensional array , an element on the 2nd dimension, of type , can be found at: A t2 A[i][j] = S + it1 + jt2
  6. 13.

    2-dimensional array ❓ Accessible by and index A[i][j] = ?

    Solution: For a 2-dimensional array , an element in the 2nd dimension, of type , can be found at: A t2 A[i][j] = S + it1 + jt2 If our were to start at memory address 800 (in decimal), then: A
  7. 14.

    2-dimensional array ✅ Accessible by and index A[i][j] = S

    + it1 + jt2 *Getting and setting time complexity remains O(1)
  8. 15.

    Multi-dimensional array Indexes • 2 dimensions: • 3 dimensions: •

    4 dimensions: A[i][j] = S + it1 + jt2 A[i][j][k] = S + it1 + jt2 + kt3 A[i][j][k][l] = S + it1 + jt2 + kt3 + lt4
  9. 17.

    Multi-dimensional array Size Let be the number of dimensions of

    an array, an specific dimension such that , and the size of the dimension , therefore the size of an array is: Or simplified: d s 1 ≤ s ≤ d ns s n1 ⋅ n2 ⋅ n3 ⋅ . . . ⋅ nd d ∏ s=1 ns
  10. 19.

    Multi-dimensional array Size The i-th dimension have size: Or simplified:

    ni ⋅ ni+1 ⋅ ni+2 ⋅ . . . ⋅ nd d ∏ s=i ns
  11. 20.

    Multi-dimensional array Size A[i1 ] . . . [id ]

    = S + i1 d ∏ s=2 ns + i2 d ∏ s=3 ns + . . . + id−2 d ∏ s=d−1 ns + id−1 nd + id
  12. 21.

    Multi-dimensional array Size A[i1 ] . . . [id ]

    = S + i1 d ∏ s=2 ns + i2 d ∏ s=3 ns + . . . + id−2 d ∏ s=d−1 ns + id−1 nd + id If we say that then: d ∏ s=d+1 ns = 1 d ∑ t=1 d ∏ s=t+1 ns
  13. 22.

    Multi-dimensional array Size A[i1 ] . . . [id ]

    = S + i1 d ∏ s=2 ns + i2 d ∏ s=3 ns + . . . + id−2 d ∏ s=d−1 ns + id−1 nd + id If we say that then: d ∏ s=d+1 ns = 1 d ∑ t=1 d ∏ s=t+1 ns
  14. 23.

    Python Challenge github.com/platzimaster/challenge-python-09 Given an array of numbers between of

    length , modify the given array (in place) such that every appears twice while maintaining the size of the array. A [0, 9] 1 ≤ n ≤ 109 0 Example 1 • Input: [1, 0, 1, 2, 9, 0, 1] • Output: [1, 0, 0, 1, 2, 9, 0] Example 1 • Input: [1, 2, 3] • Output: [1, 2, 3] Note: Found a solution with O(n) in time and O(1) in space complexity
  15. 24.

    Given an array of numbers between of length , modify

    the given array (in place) such that every appears twice while maintaining the size of the array. A [0, 9] 1 ≤ n ≤ 109 0 Test case • Input: [1] • Output: [1] Test case • Input: [0] • Output: [0] Test case • Input: [0, 0, 0, …, 0] • Output: [0, 0, 0, …, 0] Test case • Input: [1, 2, 3, 4, 0] • Output: [1, 2, 3, 4, 0]
  16. 25.

    Given an array of numbers between of length , modify

    the given array (in place) such that every appears twice while maintaining the size of the array. A [0, 9] 1 ≤ n ≤ 109 0
  17. 26.

    Meaning of criteria • : Asume a computer can perform

    primitive operations in 1 second, a solution will perform primitive operations, which will take seconds or years. Which implies a (or less) solution. • In place: Means no additional/auxiliary space is required. Which implies a space solution. • Maintaining the size: Array size must not change, and every number pushed outside the array will not be part of the array anymore. 1 ≤ n ≤ 109 109 O(n2) 1018 109 ≈ 31.7 O(n) O(1)
  18. 27.

    Brute force approach (Following problem description) def duplicate_zeros( ): ⃪

    len( ) for ⃪ : if == 0: .insert( , 0) return A n A i {0,...,n − 1} A[i] A i A[: n]
  19. 28.

    Brute force approach (Following problem description) def duplicate_zeros( ): ⃪

    len( ) for ⃪ : if == 0: .insert( , 0) return A n A i {0,...,n − 1} A[i] A i A[: n] O(n)
  20. 29.

    Brute force approach (Following problem description) def duplicate_zeros( ): ⃪

    len( ) for ⃪ : if == 0: .insert( , 0) return A n A i {0,...,n − 1} A[i] A i A[: n] O(n) O(n2)
  21. 30.

    Observations • Every modification from left to right costs •

    For every 0 we found, we loose one element from the end • If the last element is 0, there’s nothing to do O(n)
  22. 31.

    Optimum solution • Count the number of zeros that will

    be duplicated, as • Read from using index • Set • If : • Decrease by • Set to d n − d − 1 i A[i + d] = A[i] A[i + d] = 0 d 1 A[i + d] 0