Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[Solutions to problems] Arrays & Dynamic Arrays

[Solutions to problems] Arrays & Dynamic Arrays

3fafd51aa8dad766208e11eaaa1d534c?s=128

Pablo Ángel Trinidad

May 28, 2020
Tweet

Transcript

  1. Pablo Trinidad | @_pablotrinidad_ Arrays & Dynamic arrays [Homework solution]

  2. Multi-dimensional arrays

  3. Arrays Definition Collection of values of the same type, stored

    in contiguous memory, accessible by an index.
  4. 2-dimensional array

  5. 2-dimensional array ❓ Values of the same type

  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”
  7. 2-dimensional array ❓ Stored in contiguous memory

  8. 2-dimensional array ✅ Stored in contiguous memory

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

  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
  11. 2-dimensional array ❓ Accessible by and index A[i][j] = ?

    Solution: For a 2-dimensional array , an A A[i] = S + it1
  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
  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
  14. 2-dimensional array ✅ Accessible by and index A[i][j] = S

    + it1 + jt2 *Getting and setting time complexity remains O(1)
  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
  16. Multi-dimensional array Size sizeof(A): bytes t1 ⋅ 2 = 20

    ⋅ 2 = 40
  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
  18. Multi-dimensional array Size d ∏ s=1 ns =

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

    ni ⋅ ni+1 ⋅ ni+2 ⋅ . . . ⋅ nd d ∏ s=i ns
  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
  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
  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
  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
  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]
  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
  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)
  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]
  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)
  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)
  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)
  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
  32. Optimum solution O(n) O(n)

  33. Submissions github.com/platzimaster/challenge-python-09 31% Acceptance 56% Submissions

  34. Arrays & Dynamic arrays bit.ly/dynamic-arrays