-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayManipulation.swift
66 lines (48 loc) · 1.91 KB
/
arrayManipulation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Foundation
/*
* Complete the 'arrayManipulation' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. 2D_INTEGER_ARRAY queries
*/
func arrayManipulation(n: Int, queries: [[Int]]) -> Int {
// Write your code here
var tmp = [Int](repeating: 0, count: n+1)
for i in 0..<queries.count {
tmp[queries[i][0] - 1] += queries[i][2]
tmp[queries[i][1]] -= queries[i][2]
}
var ans = Int.min
var sum = 0
for t in tmp {
sum += t
ans = max(ans, sum)
}
return ans
}
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let firstMultipleInputTemp = readLine()?.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression) else { fatalError("Bad input") }
let firstMultipleInput = firstMultipleInputTemp.split(separator: " ").map{ String($0) }
guard let n = Int(firstMultipleInput[0])
else { fatalError("Bad input") }
guard let m = Int(firstMultipleInput[1])
else { fatalError("Bad input") }
var queries = [[Int]]()
for _ in 1...m {
guard let queriesRowTemp = readLine()?.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression) else { fatalError("Bad input") }
let queriesRow: [Int] = queriesRowTemp.split(separator: " ").map {
if let queriesItem = Int($0) {
return queriesItem
} else { fatalError("Bad input") }
}
guard queriesRow.count == 3 else { fatalError("Bad input") }
queries.append(queriesRow)
}
guard queries.count == m else { fatalError("Bad input") }
let result = arrayManipulation(n: n, queries: queries)
fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)