-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreversedSumOfDigits.go
62 lines (45 loc) · 963 Bytes
/
reversedSumOfDigits.go
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
package main
/*
Given integers p and n, find the smallest non-negative n-digit integer (represented as a string) whose digits add up to p. If there is no such number, return "-1" instead.
Example
For p = 15 and n = 3, the output should be
reversedSumOfDigits(p, n) = "159";
For p = 30 and n = 2, the output should be
reversedSumOfDigits(p, n) = "-1";
For p = 2 and n = 5, the output should be
reversedSumOfDigits(p, n) = "10001".
Input/Output
[execution time limit] 4 seconds (go)
[input] integer p
Guaranteed constraints:
0 ≤ p ≤ 105.
[input] integer n
Guaranteed constraints:
1 ≤ n ≤ 104.
[output] string
*/
import "strings"
func reversedSumOfDigits(p, n int) string {
if p == 0 {
if n == 1 {
return "0"
}
return "-1"
}
d := []byte("1" + strings.Repeat("0", n-1))
p -= 1
n -= 1
for {
if n < 0 {
return "-1"
}
if p >= 9 {
p -= 9
d[n] += 9
} else {
d[n] += byte(p)
return string(d)
}
n -= 1
}
}