@@ -104,6 +104,145 @@ tags:
104
104
#### Go
105
105
106
106
``` go
107
+ smallestNumber (num string , t int64 ) string {
108
+ primeCount , isDivisible := getPrimeCount (t)
109
+ if !isDivisible {
110
+ return " -1"
111
+ }
112
+
113
+ factorCount := getFactorCount (primeCount)
114
+ if sumValues (factorCount) > len (num) {
115
+ return construct (factorCount)
116
+ }
117
+
118
+ primeCountPrefix := getPrimeCountFromString (num)
119
+ firstZeroIndex := strings.Index (num, " 0" )
120
+ if firstZeroIndex == -1 {
121
+ firstZeroIndex = len (num)
122
+ if isSubset (primeCount, primeCountPrefix) {
123
+ return num
124
+ }
125
+ }
126
+
127
+ for i := len (num) - 1 ; i >= 0 ; i-- {
128
+ d := int (num[i] - ' 0' )
129
+ primeCountPrefix = subtract (primeCountPrefix, kFactorCounts[d])
130
+ spaceAfterThisDigit := len (num) - 1 - i
131
+ if i > firstZeroIndex {
132
+ continue
133
+ }
134
+ for biggerDigit := d + 1 ; biggerDigit < 10 ; biggerDigit++ {
135
+ factorsAfterReplacement := getFactorCount (
136
+ subtract (subtract (primeCount, primeCountPrefix), kFactorCounts[biggerDigit]),
137
+ )
138
+ if sumValues (factorsAfterReplacement) <= spaceAfterThisDigit {
139
+ fillOnes := spaceAfterThisDigit - sumValues (factorsAfterReplacement)
140
+ return num[:i] + strconv.Itoa (biggerDigit) + strings.Repeat (" 1" , fillOnes) + construct (factorsAfterReplacement)
141
+ }
142
+ }
143
+ }
144
+
145
+ factorsAfterExtension := getFactorCount (primeCount)
146
+ return strings.Repeat (" 1" , len (num)+1 -sumValues (factorsAfterExtension)) + construct (factorsAfterExtension)
147
+ }
148
+
149
+ var kFactorCounts = map [int ]map [int ]int {
150
+ 0 : {}, 1 : {}, 2 : {2 : 1 }, 3 : {3 : 1 }, 4 : {2 : 2 },
151
+ 5 : {5 : 1 }, 6 : {2 : 1 , 3 : 1 }, 7 : {7 : 1 }, 8 : {2 : 3 }, 9 : {3 : 2 },
152
+ }
153
+
154
+ func getPrimeCount (t int64 ) (map [int ]int , bool ) {
155
+ count := map [int ]int {2 : 0 , 3 : 0 , 5 : 0 , 7 : 0 }
156
+ for _ , prime := range []int {2 , 3 , 5 , 7 } {
157
+ for t%int64 (prime) == 0 {
158
+ t /= int64 (prime)
159
+ count[prime]++
160
+ }
161
+ }
162
+ return count, t == 1
163
+ }
164
+
165
+ func getPrimeCountFromString (num string ) map [int ]int {
166
+ count := map [int ]int {2 : 0 , 3 : 0 , 5 : 0 , 7 : 0 }
167
+ for _ , d := range num {
168
+ for prime , freq := range kFactorCounts[int (d-' 0' )] {
169
+ count[prime] += freq
170
+ }
171
+ }
172
+ return count
173
+ }
174
+
175
+ func getFactorCount (count map [int ]int ) map [int ]int {
176
+ res := map [int ]int {}
177
+ count8 := count[2 ] / 3
178
+ remaining2 := count[2 ] % 3
179
+ count9 := count[3 ] / 2
180
+ count3 := count[3 ] % 2
181
+ count4 := remaining2 / 2
182
+ count2 := remaining2 % 2
183
+ count6 := 0
184
+ if count2 == 1 && count3 == 1 {
185
+ count2, count3 = 0 , 0
186
+ count6 = 1
187
+ }
188
+ if count3 == 1 && count4 == 1 {
189
+ count2 = 1
190
+ count6 = 1
191
+ count3, count4 = 0 , 0
192
+ }
193
+ res[2 ] = count2
194
+ res[3 ] = count3
195
+ res[4 ] = count4
196
+ res[5 ] = count[5 ]
197
+ res[6 ] = count6
198
+ res[7 ] = count[7 ]
199
+ res[8 ] = count8
200
+ res[9 ] = count9
201
+ return res
202
+ }
203
+
204
+ func construct (factors map [int ]int ) string {
205
+ var res strings.Builder
206
+ for digit := 2 ; digit < 10 ; digit++ {
207
+ res.WriteString (strings.Repeat (strconv.Itoa (digit), factors[digit]))
208
+ }
209
+ return res.String ()
210
+ }
211
+
212
+ func isSubset (a , b map [int ]int ) bool {
213
+ for key , value := range a {
214
+ if b[key] < value {
215
+ return false
216
+ }
217
+ }
218
+ return true
219
+ }
220
+
221
+ func subtract (a , b map [int ]int ) map [int ]int {
222
+ res := make (map [int ]int , len (a))
223
+ for k , v := range a {
224
+ res[k] = v
225
+ }
226
+ for k , v := range b {
227
+ res[k] = max (0 , res[k]-v)
228
+ }
229
+ return res
230
+ }
231
+
232
+ func sumValues (count map [int ]int ) int {
233
+ sum := 0
234
+ for _ , v := range count {
235
+ sum += v
236
+ }
237
+ return sum
238
+ }
239
+
240
+ func max (a , b int ) int {
241
+ if a > b {
242
+ return a
243
+ }
244
+ return b
245
+ }
107
246
108
247
```
109
248
0 commit comments