-
Notifications
You must be signed in to change notification settings - Fork 29
/
Contents.swift
45 lines (35 loc) · 965 Bytes
/
Contents.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
import UIKit
/*
This problem was asked by Google.
Given a sorted list of integers, square the elements and give the output in sorted order.
For example, given [-9, -2, 0, 2, 3], return [0, 4, 4, 9, 81].
*/
//O(n) - time complexity
//O(n) - space complexity
func squaredSort(elements: [Int]) -> [Int] {
var negative = [Int]()
var positive = [Int]()
var result = [Int]()
for i in elements {
if i < 0 {
negative.append(i * i)
}
else if i > 0 {
positive.append(i * i)
}
else {
result.append(i)
}
}
negative.reverse()
while !negative.isEmpty && !positive.isEmpty {
if negative[0] < positive[0] {
result.append(negative.removeFirst())
}
else {
result.append(positive.removeFirst())
}
}
return result + positive + negative
}
squaredSort(elements: [-10,-2,-2,0,1,5,100])