Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2f43d1a

Browse files
2952. Minimum Number of Coins to be Added
1 parent c09c278 commit 2f43d1a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
3+
// Solution by Sergey Leschev
4+
// 2952. Minimum Number of Coins to be Added
5+
6+
// Time complexity : O(N log N)
7+
// Space complexity: O(1)
8+
9+
func minimumAddedCoins(_ coins: [Int], _ target: Int) -> Int {
10+
var coins = coins.sorted()
11+
var currentMax = 0
12+
var additions = 0
13+
var index = 0
14+
15+
while currentMax < target {
16+
if index < coins.count && coins[index] <= currentMax + 1 {
17+
currentMax += coins[index]
18+
index += 1
19+
} else {
20+
currentMax += currentMax + 1
21+
additions += 1
22+
}
23+
}
24+
25+
return additions
26+
}
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /