-
Notifications
You must be signed in to change notification settings - Fork 5
/
leetcode-1167-minimum-cost-to-connect-sticks.swift
185 lines (127 loc) · 4.85 KB
/
leetcode-1167-minimum-cost-to-connect-sticks.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
Copyright (c) 2020 David Seek, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
https://leetcode.com/problems/minimum-cost-to-connect-sticks/
You have some sticks with positive integer lengths.
You can connect any two sticks of lengths X and Y into one stick by paying a cost of X + Y. You perform this action until there is one stick remaining.
Return the minimum cost of connecting all the given sticks into one stick in this way.
Example 1:
Input: sticks = [2,4,3]
Output: 14
Example 2:
Input: sticks = [1,8,3,5]
Output: 30
Constraints:
1 <= sticks.length <= 10^4
1 <= sticks[i] <= 10^4
*/
/**
Big O Annotation
Time complexity O(n) where n is the amount of elements in nums.
Space complexity O(n) where n is the amount of elements in nums.
*/
func connectSticks(_ sticks: [Int]) -> Int {
// Init a min heap from the sticks
var heap = Heap(sticks, areSorted: <)
// The total costs
var cost: Int = 0
// While the heap is not empty
while !heap.isEmpty {
// Get the root (aka the most min element)
let stick1 = heap.getRoot()!
// See if there is a second elements
guard let stick2 = heap.getRoot() else {
// ... if not, stop
break
}
// ... if so, smash them
let sum = stick1 + stick2
// And append the costs
cost += sum
// And add the smashed stick to the heap
heap.insert(sum)
}
return cost
}
// Basic Heap implementation
struct Heap {
private var storage: [Int] = []
private var areSorted: (Int, Int) -> Bool
init(_ storage: [Int], areSorted: @escaping (Int, Int) -> Bool) {
self.storage = storage
self.areSorted = areSorted
guard !storage.isEmpty else {
return
}
for index in stride(from: storage.count / 2 - 1, through: 0 , by: -1) {
siftDown(from: index)
}
}
public var isEmpty: Bool {
return storage.isEmpty
}
public var count: Int {
return storage.count
}
public func getChildIndices(forParentAt index: Int) -> (left: Int, right: Int) {
let left = (index * 2) + 1
return (left, left + 1)
}
public func getParentIndex(forChildAt index: Int) -> Int {
return (index - 1) / 2
}
public mutating func insert(_ element: Int) {
storage.append(element)
siftUp(from: count - 1)
}
public mutating func getRoot() -> Int? {
guard !isEmpty else {
return nil
}
storage.swapAt(0, count - 1)
let root = storage.removeLast()
siftDown(from: 0)
return root
}
public mutating func siftUp(from index: Int) {
var childIndex = index
var parentIndex = getParentIndex(forChildAt: childIndex)
while childIndex > 0 && areSorted(storage[childIndex], storage[parentIndex]) {
storage.swapAt(childIndex, parentIndex)
childIndex = parentIndex
parentIndex = getParentIndex(forChildAt: childIndex)
}
}
public mutating func siftDown(from index: Int) {
var parentIndex = index
while true {
let (left, right) = getChildIndices(forParentAt: parentIndex)
var swapIndex: Int?
if left < count && areSorted(storage[left], storage[parentIndex]) {
swapIndex = left
}
if right < count && areSorted(storage[right], storage[swapIndex ?? parentIndex]) {
swapIndex = right
}
guard swapIndex != nil else {
return
}
storage.swapAt(parentIndex, swapIndex!)
parentIndex = swapIndex!
}
}
}