Skip to content

Commit 6fbc8f5

Browse files
committed
479_Largest_Palindrome_Product
1 parent 30853cf commit 6fbc8f5

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ Also, there are open source implementations for basic data structs and algorithm
148148
| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/461_Hamming_Distance.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/461_Hamming_Distance.java) | Hamming Distance is related to XOR for numbers. So, XOR then count 1. O(n) |
149149
| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/463_Island_Perimeter.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/463_Island_Perimeter.java) | math, find the area, actual number, then find the digit |
150150
| 475 | [Heaters](https://leetcode.com/problems/heaters/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/475_Heaters.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/475_Heaters.java) | 1. Binary search hourse in heater array, O(nlogn) and O(1)<br> 2. Two points, O(nlogn) and O(1) |
151+
| 479 | [Largest Palindrome Product](https://leetcode.com/problems/largest-palindrome-product/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/479_Largest_Palindrome_Product.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/479_Largest_Palindrome_Product.java) | Product max palindrome than check, O(n^2) and O(1) |
151152
| 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/482_License_Key_Formatting.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/482_License_Key_Formatting.java) | String processing, lower and len % K, O(n) and O(n) |
152153
| 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/538_Convert_BST_to_Greater_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/538_Convert_BST_to_Greater_Tree.java) | Right first DFS with a variable recording sum of node.val and right.val. 1. Recursive.<br>2. Stack 3. Reverse Morris In-order Traversal |
153154
| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/543_Diameter_of_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/543_Diameter_of_Binary_Tree.java) | DFS with O(1) for max answer |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public int largestPalindrome(int n) {
3+
// https://leetcode.com/problems/largest-palindrome-product/discuss/96297/Java-Solution-using-assumed-max-palindrom
4+
// if input is 1 then max is 9
5+
if(n == 1){
6+
return 9;
7+
}
8+
9+
// if n = 3 then upperBound = 999 and lowerBound = 99
10+
int upperBound = (int) Math.pow(10, n) - 1, lowerBound = upperBound / 10;
11+
long maxNumber = (long) upperBound * (long) upperBound;
12+
13+
// represents the first half of the maximum assumed palindrom.
14+
// e.g. if n = 3 then maxNumber = 999 x 999 = 998001 so firstHalf = 998
15+
int firstHalf = (int)(maxNumber / (long) Math.pow(10, n));
16+
17+
boolean palindromFound = false;
18+
long palindrom = 0;
19+
20+
while (!palindromFound) {
21+
// creates maximum assumed palindrom
22+
// e.g. if n = 3 first time the maximum assumed palindrom will be 998 899
23+
palindrom = createPalindrom(firstHalf);
24+
25+
// here i and palindrom/i forms the two factor of assumed palindrom
26+
for (long i = upperBound; upperBound > lowerBound; i--) {
27+
// if n= 3 none of the factor of palindrom can be more than 999 or less than square root of assumed palindrom
28+
if (palindrom / i > maxNumber || i * i < palindrom) {
29+
break;
30+
}
31+
32+
// if two factors found, where both of them are n-digits,
33+
if (palindrom % i == 0) {
34+
palindromFound = true;
35+
break;
36+
}
37+
}
38+
39+
firstHalf--;
40+
}
41+
42+
return (int) (palindrom % 1337);
43+
}
44+
45+
private long createPalindrom(long num) {
46+
String str = num + new StringBuilder().append(num).reverse().toString();
47+
return Long.parseLong(str);
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution(object):
2+
# def largestPalindrome(self, n):
3+
# """
4+
# :type n: int
5+
# :rtype: int
6+
# """
7+
# if n == 1:
8+
# return 9
9+
# # https://leetcode.com/problems/largest-palindrome-product/discuss/96297/Java-Solution-using-assumed-max-palindrom
10+
# upperBound = 10 ** n - 1
11+
# lowerBound = upperBound / 10
12+
# maxNum = upperBound * upperBound
13+
# firstHalf = maxNum / (10 ** n)
14+
# palindromFound = False
15+
# palindrom = 0
16+
# while not palindromFound:
17+
# palindrom = int(str(firstHalf) + str(firstHalf)[::-1])
18+
# for i in xrange(upperBound, lowerBound, -1):
19+
# if i * i < palindrom:
20+
# break
21+
# if palindrom % i == 0:
22+
# palindromFound = True
23+
# break
24+
# firstHalf -= 1
25+
# return palindrom % 1337
26+
27+
def largestPalindrome(self, n):
28+
if n == 1:
29+
return 9
30+
if n == 2:
31+
return 987
32+
for a in xrange(2, 9 * 10 ** (n - 1)):
33+
hi = (10 ** n) - a
34+
lo = int(str(hi)[::-1])
35+
if a ** 2 - 4 * lo < 0:
36+
continue
37+
if (a ** 2 - 4 * lo) ** .5 == int((a ** 2 - 4 * lo) ** .5):
38+
return (lo + 10 ** n * (10 ** n - a)) % 1337

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