Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. let matrix1 = [[1,2,3],
  2. [2,3,4],
  3. [3,4,5]]
  4.  
  5. // min col max row = 3
  6. // max col min row = 3
  7.  
  8. let matrix2 = [[1,5,2],
  9. [5,7,5],
  10. [4,5,8],
  11. [1,1,1],
  12. [3,3,3]]
  13.  
  14. // min col max row = 1
  15. // max col min row = 5
  16.  
  17. let matrix10 = [[5,4,2,8],
  18. [8,1,7,9],
  19. [5,9,4,8]]
  20.  
  21.  
  22. let matrix11 = [[1,2,3],
  23. [2,3,4],
  24. [3,4,5]]
  25.  
  26. func quicksort(_ array: [Int]) -> [Int] {
  27. guard array.count > 1 else { return array }
  28.  
  29. let pivot = array[array.count/2]
  30. let less = array.filter { $0 < pivot }
  31. let equal = array.filter { $0 == pivot }
  32. let greater = array.filter { $0 > pivot }
  33.  
  34. return quicksort(less) + equal + quicksort(greater)
  35. }
  36.  
  37. func getSaddlePoints(from input: [[Int]]) {
  38.  
  39. for (index, array) in input.enumerated() {
  40. let maxInRow = maxPointInRow(from: array)
  41. let indexOfMaxInRow = input[index].firstIndex(of: maxInRow)
  42. let minInCol = minPointInColumn(from: formColumn(from: input, index: indexOfMaxInRow!))
  43. if maxInRow == minInCol {
  44. print(maxInRow)
  45. }
  46.  
  47. let minInRow = minPointInRow(from: array)
  48. let indexOfMixInRow = input[index].firstIndex(of: minInRow)
  49. let maxInCol = maxPointInColumn(from: formColumn(from: input, index: indexOfMixInRow!))
  50. if minInRow == maxInCol {
  51. print(minInRow)
  52. }
  53. }
  54. }
  55.  
  56. func formColumn(from input: [[Int]], index:Int) -> [Int]{
  57. var column = [Int]()
  58. for array in input {
  59. column.append(array[index])
  60. }
  61. return column
  62. }
  63.  
  64. func minPointInColumn(from input: [Int]) -> Int {
  65. return quicksort(input).first!
  66. }
  67.  
  68. func maxPointInColumn(from input: [Int]) -> Int {
  69. return quicksort(input).last!
  70. }
  71.  
  72. func minPointInRow(from input: [Int]) -> Int {
  73. let sorted = quicksort(input)
  74. return sorted.first!
  75. }
  76.  
  77. func maxPointInRow(from input: [Int]) -> Int {
  78. let sorted = quicksort(input)
  79. return sorted.last!
  80. }
  81.  
  82. getSaddlePoints(from: matrix1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement