Guest User

Untitled

a guest
Nov 15th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. import Foundation
  2.  
  3. // Complete the arrayManipulation function below.
  4. func arrayManipulation(n: Int, queries: [[Int]]) -> Int {
  5. var items = Array(repeating: 0, count: n)
  6. print("\(items)")
  7. for query in queries {
  8. let s = query[0] - 1
  9. let e = query[1]
  10. let k = query[2]
  11. items[s] = items[s] + k
  12. if (e<items.count) { items[e] = items[e] - k }
  13. }
  14. var current = 0
  15. var max = -1
  16. for i in 0..<items.count {
  17. current = current + items[i]
  18. if current > max {
  19. max = current
  20. }
  21. }
  22. return max
  23. }
  24.  
  25. let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
  26. FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
  27. let fileHandle = FileHandle(forWritingAtPath: stdout)!
  28.  
  29. guard let nmTemp = readLine() else { fatalError("Bad input") }
  30. let nm = nmTemp.split(separator: " ").map{ String($0) }
  31.  
  32. guard let n = Int(nm[0].trimmingCharacters(in: .whitespacesAndNewlines))
  33. else { fatalError("Bad input") }
  34.  
  35. guard let m = Int(nm[1].trimmingCharacters(in: .whitespacesAndNewlines))
  36. else { fatalError("Bad input") }
  37.  
  38. let queries: [[Int]] = AnyIterator{ readLine() }.prefix(m).map {
  39. let queriesRow: [Int] = $0.split(separator: " ").map {
  40. if let queriesItem = Int($0.trimmingCharacters(in: .whitespacesAndNewlines)) {
  41. return queriesItem
  42. } else { fatalError("Bad input") }
  43. }
  44.  
  45. guard queriesRow.count == 3 else { fatalError("Bad input") }
  46.  
  47. return queriesRow
  48. }
  49.  
  50. guard queries.count == m else { fatalError("Bad input") }
  51.  
  52. let result = arrayManipulation(n: n, queries: queries)
  53.  
  54. fileHandle.write(String(result).data(using: .utf8)!)
  55. fileHandle.write("\n".data(using: .utf8)!)
Add Comment
Please, Sign In to add comment