Skip to content

Commit 6ae2059

Browse files
add 625
1 parent 5d69170 commit 6ae2059

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|625|[Minimum Factorization](https://leetcode.com/problems/minimum-factorization/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_625.java) | O(?) |O(?) | Medium |
2324
|624|[Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_624.java) | O(nlogn) |O(1) | Easy | Sort, Array
2425
|623|[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_623.java) | O(n) |O(h) | Medium | Tree
2526
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_617.java) | O(n) |O(h) | Easy | Tree, Recursion
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 625. Minimum Factorization
8+
*
9+
* Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a.
10+
11+
If there is no answer or the answer is not fit in 32-bit signed integer, then return 0.
12+
13+
Example 1
14+
Input:
15+
48
16+
Output:
17+
68
18+
19+
Example 2
20+
Input:
21+
15
22+
Output:
23+
35
24+
*/
25+
public class _625 {
26+
27+
/**reference: https://discuss.leetcode.com/topic/92854/java-solution-result-array
28+
* and https://leetcode.com/articles/minimum-factorization/#approach-3-using-factorizationaccepted*/
29+
public int smallestFactorization(int a) {
30+
//case 1: a < 10
31+
if (a < 10) return a;
32+
33+
//case 2: start with 9 and try every possible digit
34+
List<Integer> resultArray = new ArrayList<>();
35+
for (int i = 9; i > 1; i--) {
36+
//if current digit divides a, then store all occurences of current digit in res
37+
while (a % i == 0) {
38+
a = a/i;
39+
resultArray.add(i);
40+
}
41+
}
42+
43+
//if a could not be broken in form of digits, return 0
44+
if (a != 0) return 0;
45+
46+
//get the result from the result array in reverse order
47+
long result = 0;
48+
for (int i = resultArray.size()-1; i >=0; i--) {
49+
result = result*10 + resultArray.get(i);
50+
if (result > Integer.MAX_VALUE) return 0;
51+
}
52+
return (int) result;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy