Skip to content

Commit 30853cf

Browse files
committed
953_Verifying_an_Alien_Dictionary and 954_Array_of_Doubled_Pairs
1 parent 4edafe4 commit 30853cf

7 files changed

+116
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ Also, there are open source implementations for basic data structs and algorithm
184184
| 929 | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/929_Unique_Email_Addresses.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/929_Unique_Email_Addresses.java) | String handle and hash (or set) |
185185
| 945 | [Minimum Increment to Make Array Unique](https://leetcode.com/problems/minimum-increment-to-make-array-unique/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/945_Minimum_Increment_to_Make_Array_Unique.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/945_Minimum_Increment_to_Make_Array_Unique.java) | Sort, then list duplicate and missing value in sorted list. O(nlgn) and O(n) |
186186
| 946 | [Validate Stack Sequences](https://leetcode.com/problems/validate-stack-sequences/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/946_Validate_Stack_Sequences.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/946_Validate_Stack_Sequences.java) | Add a stack named inStack to help going through pushed and popped. O(n) and O(n) |
187+
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/contest/weekly-contest-114/problems/verifying-an-alien-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/953_Verifying_an_Alien_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/953_Verifying_an_Alien_Dictionary.java) | Use hashmap to store index of each value, then create a comparator based on this index, O(n) and O(n) |
188+
| 954 | [Array of Doubled Pairs](https://leetcode.com/contest/weekly-contest-114/problems/array-of-doubled-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/954_Array_of_Doubled_Pairs.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/954_Array_of_Doubled_Pairs.java) | Sort, then use hashmap to store the frequency of each value. Then, check n, 2 * n in hashmap, O(nlogn) and O(n) |
187189

188190
| # | To Understand |
189191
|---| ----- |

java/179_Largest_Number.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public String largestNumber(int[] nums) {
2929
for (String numAsStr : asStrs) {
3030
largestNumberStr += numAsStr;
3131
}
32-
3332
return largestNumberStr;
3433
}
3534
}

java/905_Sort_Array_By_Parity.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class Solution {
2-
/*public int[] sortArrayByParity(int[] A) {
3-
Array.sort(A, (a, b)-> Integer.compare(a%2, b%2));
2+
/* public int[] sortArrayByParity(int[] A) {
3+
A = Arrays.stream(A).
4+
boxed().
5+
sorted((a, b) -> Integer.compare(a% 2, b % 2)).
6+
mapToInt(i -> i).
7+
toArray();
48
return A;
59
}*/
610

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
HashMap<Character, Integer> orderMap = new HashMap<>();
3+
public boolean isAlienSorted(String[] words, String order) {
4+
// Put value index map into hashmap
5+
for (int i = 0; i < order.length(); i++) {
6+
orderMap.put(order.charAt(i), i);
7+
}
8+
for (int i = 0; i < words.length - 1; i++) {
9+
if (cmp_alien(words[i], words[i + 1]) > 0) return false;
10+
}
11+
return true;
12+
13+
}
14+
private int cmp_alien(String a, String b) {
15+
int ls = a.length() < b.length() ? a.length(): b.length();
16+
int pos = 0;
17+
// Compare based on hashmap
18+
while (pos < ls) {
19+
if (orderMap.get(a.charAt(pos)) != orderMap.get(b.charAt(pos)))
20+
return orderMap.get(a.charAt(pos)) - orderMap.get(b.charAt(pos));
21+
pos += 1;
22+
}
23+
return a.length() <= b.length() ? -1: 1;
24+
}
25+
}

java/954_Array_of_Doubled_Pairs.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public boolean canReorderDoubled(int[] A) {
3+
HashMap<Integer, Integer> valueMap = new HashMap<>();
4+
// Sort in[] with comparator
5+
A = Arrays.stream(A).
6+
boxed().
7+
sorted((a, b) -> Integer.compare(Math.abs(a), Math.abs(b))).
8+
mapToInt(i -> i).
9+
toArray();
10+
for (int n: A) valueMap.put(n, valueMap.getOrDefault(n, 0) + 1);
11+
for (int n: A) {
12+
if (valueMap.get(n) <= 0) continue;
13+
if (valueMap.containsKey(2 * n) && valueMap.get(2 * n) > 0) {
14+
valueMap.put(n, valueMap.get(n) - 1);
15+
valueMap.put(2 * n, valueMap.get(2 * n) - 1);
16+
} else {
17+
return false;
18+
}
19+
}
20+
return true;
21+
}
22+
23+
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution(object):
2+
def isAlienSorted(self, words, order):
3+
"""
4+
:type words: List[str]
5+
:type order: str
6+
:rtype: bool
7+
"""
8+
order_map = {}
9+
for i, v in enumerate(order):
10+
order_map[v] = i
11+
12+
def cmp_alien(x, y):
13+
ls = min(len(x), len(y))
14+
index = 0
15+
while index < ls:
16+
if x[index] != y[index]:
17+
return order_map[x[index]] - order_map[y[index]]
18+
index += 1
19+
return len(x) - len(y)
20+
pos = 0
21+
while pos + 1 < len(words):
22+
if cmp_alien(words[pos], words[pos + 1]) > 0:
23+
return False
24+
pos += 1
25+
return True
26+
27+
28+
if __name__ == '__main__':
29+
s = Solution()
30+
print s.isAlienSorted(["hello","leetcode"], "hlabcdefgijkmnopqrstuvwxyz")
31+
print s.isAlienSorted(["word","world","row"], "worldabcefghijkmnpqstuvxyz")
32+
print s.isAlienSorted(["apple","app"], "abcdefghijklmnopqrstuvwxyz")

python/954_Array_of_Doubled_Pairs.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def canReorderDoubled(self, A):
3+
"""
4+
:type A: List[int]
5+
:rtype: bool
6+
"""
7+
v_map = {}
8+
A.sort(key=lambda x: abs(x))
9+
for n in A:
10+
v_map[n] = v_map.get(n, 0) + 1
11+
for n in A:
12+
if v_map[n] <= 0:
13+
continue
14+
if 2 * n in v_map and v_map[2 * n] > 0:
15+
v_map[n] -= 1
16+
v_map[2 * n] -= 1
17+
else:
18+
return False
19+
return True
20+
21+
22+
if __name__ == '__main__':
23+
s = Solution()
24+
print s.canReorderDoubled([3, 1, 3, 6])
25+
print s.canReorderDoubled([2, 1, 2, 6])
26+
print s.canReorderDoubled([4, -2, 2, -4])
27+
print s.canReorderDoubled([1, 2, 4, 16, 8, 4])

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