Skip to content

Commit 681542d

Browse files
committed
1118
1 parent f131452 commit 681542d

File tree

5 files changed

+137
-3
lines changed

5 files changed

+137
-3
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Feel free to submit pull requests, add issues and be a contributer.
9494
| Leetcode | [389. Find the Difference](https://leetcode.com/problems/find-the-difference/description/) | [Java](./java/findTheDifference.java) | _O(n)_ | _O(1)_ | Easy | |
9595
| Leetcode | [408. Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/description/) | [Java](./java/validWordAbbreviation.java) | _O(n)_ | _O(1)_ | Easy | |
9696
| Leetcode | [422. Valid Word Square](https://leetcode.com/problems/valid-word-square/description/) | [Java](./java/validWordSquare.java) | _O(n)_ | _O(1)_ | Easy | |
97+
| Leetcode | [479. Largest Palindrome Product](https://leetcode.com/problems/largest-palindrome-product/description/) | [Java](./java/largestPalindrome.java) | _O(n)_ | _O(1)_ | Medium | |
9798
| Leetcode | [482. License Key Formatting](https://leetcode.com/problems/license-key-formatting/description/) | [Java](./java/licenseKeyFormatting.java) | _O(n)_ | _O(1)_ | Medium | |
9899
| Leetcode | [500. Keyboard Row](https://leetcode.com/problems/keyboard-row/description/) | [Java](./java/findWords.java) | _O(n)_ | _O(1)_ | Easy | |
99100
| Leetcode | [520. Detect Capital](https://leetcode.com/problems/detect-capital/description/) | [Java](./java/detectCapitalUse.java) | _O(n)_ | _O(1)_ | Easy | |
@@ -151,6 +152,7 @@ Feel free to submit pull requests, add issues and be a contributer.
151152
| Leetcode | [653. Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/) | [Java](./java/Twosum.java) | _O(n)_ | _O(n)_ | Easy | |
152153
| Leetcode | [662. Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/description/) | [Java](./java/widthOfBinaryTree.java) | O(n) | O(d) | Medium | |
153154
| Leetcode | [663. Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/description/) | [Java](./java/CheckEqualTrees.java) | | | Medium | |
155+
| Leetcode | [677. Map Sum Pairs](https://leetcode.com/problems/map-sum-pairs/description/) | [Java](./java/MapSumii.java) | O(n) | O(1) | Medium | HashMap |
154156
| Leetcode | [687. Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/description/) | [Java](./java/longestUnivaluePath.java) | O(n) | O(1) | Easy | |
155157

156158

@@ -252,6 +254,7 @@ Feel free to submit pull requests, add issues and be a contributer.
252254
| Leetcode | [147. Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/description/) | [Java](./java/insertionSortList.java) | _O(nlogn)_ | _O(1)_ | Medium | |
253255
| Leetcode | [179. Largest Number](https://leetcode.com/problems/largest-number/description/) | [Java](./java/largestNumber.java) | _O(nlogn)_ | _O(n)_ | Medium | |
254256
| Leetcode | [280. Wiggle Sort](https://leetcode.com/problems/wiggle-sort/description/) | [Java](./java/wiggleSort.java) | _O(n)_ | _O(1)_ | Medium | |
257+
| Leetcode | [581. Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/description/) | [Java](./java/findUnsortedSubarray.java) | _O(nlogn)_ | _O(n)_ | Easy | |
255258

256259
## Math
257260
| Website | Title | Solution | Time | Space | Difficulty | Note|

java/MapSumii.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class MapSum {
2+
3+
class TrieNode
4+
{
5+
Map<Character, TrieNode> children;
6+
int value;
7+
boolean isWord;
8+
9+
public TrieNode()
10+
{
11+
children = new HashMap<>();
12+
value = 0;
13+
isWord = false;
14+
}
15+
}
16+
17+
TrieNode root;
18+
19+
/** Initialize your data structure here. */
20+
public MapSum() {
21+
root = new TrieNode();
22+
}
23+
24+
public void insert(String key, int val) {
25+
TrieNode cur = root;
26+
for(char c:key.toCharArray())
27+
{
28+
TrieNode next = cur.children.get(c);
29+
if(next == null)
30+
{
31+
next = new TrieNode();
32+
cur.children.put(c, next);
33+
}
34+
cur = next;
35+
}
36+
cur.isWord = true;
37+
cur.value = val;
38+
}
39+
40+
public int sum(String prefix) {
41+
TrieNode cur = root;
42+
for(char c:prefix.toCharArray())
43+
{
44+
TrieNode next = cur.children.get(c);
45+
if(next == null)
46+
return 0;
47+
cur = next;
48+
}
49+
return dfs(cur);
50+
}
51+
52+
public int dfs(TrieNode cur)
53+
{
54+
int sum = 0;
55+
for(char c:cur.children.keySet())
56+
sum += dfs(cur.children.get(c));
57+
return sum+cur.value;
58+
}
59+
}
60+
61+
/**
62+
* Your MapSum object will be instantiated and called as such:
63+
* MapSum obj = new MapSum();
64+
* obj.insert(key,val);
65+
* int param_2 = obj.sum(prefix);
66+
*/

java/detectCapitalUse.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class Solution {
22
public boolean detectCapitalUse(String word) {
3-
if(word.length() < 2) return true;
4-
if(word.toUpperCase().equals(word)) return true;
5-
if(word.substring(1).equals(word.substring(1))) return true;
3+
if(word.equals(word.toUpperCase())) return true;
4+
if(word.equals(word.toLowerCase())) return true;
5+
if(Character.isUpperCase(word.charAt(0)) &&
6+
word.substring(1).equals(word.substring(1).toLowerCase())) return true;
67

78
return false;
89
}

java/findUnsortedSubarray.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int findUnsortedSubarray(int[] nums) {
3+
4+
int [] numsCopy = nums.clone();
5+
Arrays.sort(numsCopy);
6+
7+
int start = 0, end = nums.length - 1;
8+
9+
while(start < nums.length && nums[start] == numsCopy[start]) start++;
10+
11+
while(end > start && nums[end] == numsCopy[end]) end--;
12+
13+
return end-start+1;
14+
}
15+
}

java/largestPalindrome.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
ass Solution {
2+
public int largestPalindrome(int n) {
3+
// if input is 1 then max is 9
4+
if(n == 1){
5+
return 9;
6+
}
7+
8+
// if n = 3 then upperBound = 999 and lowerBound = 99
9+
int upperBound = (int) Math.pow(10, n) - 1, lowerBound = upperBound / 10;
10+
long maxNumber = (long) upperBound * (long) upperBound;
11+
12+
// represents the first half of the maximum assumed palindrom.
13+
// e.g. if n = 3 then maxNumber = 999 x 999 = 998001 so firstHalf = 998
14+
int firstHalf = (int)(maxNumber / (long) Math.pow(10, n));
15+
16+
boolean palindromFound = false;
17+
long palindrom = 0;
18+
19+
while (!palindromFound) {
20+
// creates maximum assumed palindrom
21+
// e.g. if n = 3 first time the maximum assumed palindrom will be 998 899
22+
palindrom = createPalindrom(firstHalf);
23+
24+
// here i and palindrom/i forms the two factor of assumed palindrom
25+
for (long i = upperBound; upperBound > lowerBound; i--) {
26+
// if n= 3 none of the factor of palindrom can be more than 999 or less than square root of assumed palindrom
27+
if (palindrom / i > maxNumber || i * i < palindrom) {
28+
break;
29+
}
30+
31+
// if two factors found, where both of them are n-digits,
32+
if (palindrom % i == 0) {
33+
palindromFound = true;
34+
break;
35+
}
36+
}
37+
38+
firstHalf--;
39+
}
40+
41+
return (int) (palindrom % 1337);
42+
}
43+
44+
private long createPalindrom(long num) {
45+
String str = num + new StringBuilder().append(num).reverse().toString();
46+
return Long.parseLong(str);
47+
}
48+
49+
}

0 commit comments

Comments
 (0)