forked from mwasa/algorithm-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.swift
More file actions
35 lines (32 loc) · 1.05 KB
/
solution.swift
File metadata and controls
35 lines (32 loc) · 1.05 KB
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
import Foundation
let file: FileHandle? = FileHandle(forReadingAtPath: "test.txt")
if let file = file {
// Read all the data
let data = file.readDataToEndOfFile()
// Close the file
file.closeFile()
// Convert our data to string
let str = String(data: data, encoding: .utf8)
let lines:[String] = str!.characters.split{$0 == "\n"}
.map(String.init)
var lineIterator = lines.makeIterator()
let count = Int(lineIterator.next()!)!
for index in 1...count {
var max_ = 0
let n = lineIterator.next()!
var arr:[Int] = lineIterator.next()!.characters
.split{$0 == " "}.map(String.init).map{ Int($0)! }
var max_value = arr.max()!
var max_index = arr.index(of:max_value)!
var max_sum = 0
while arr.count > 0 {
for value in arr[0..<max_index] {
max_sum += max_value - value
}
arr = Array(arr[(max_index+1)..<arr.endIndex])
}
print(max_sum)
}
} else {
print("File was not loaded")
}