Skip to content

Commit 4e45c5b

Browse files
committed
added array and hashing questions
1 parent b76d8c5 commit 4e45c5b

File tree

17 files changed

+325
-21
lines changed

17 files changed

+325
-21
lines changed
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [LC 1. Two Sum - Easy](https://leetcode.com/problems/maximum-product-subarray/)
2+
3+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
4+
5+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
6+
7+
You can return the answer in any order.
8+
9+
10+
### Example 1
11+
```
12+
Input: nums = [2,7,11,15], target = 9
13+
Output: [0,1]
14+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
15+
```
16+
17+
### Example 2
18+
```
19+
Input: nums = [3,2,4], target = 6
20+
Output: [1,2]
21+
```
22+
23+
### Example 3
24+
```
25+
Input: nums = [3,3], target = 6
26+
Output: [0,1]
27+
```
28+
29+
### Constraints:
30+
31+
- 2 <= nums.length <= 104
32+
- -10^9 <= nums[i] <= 10^9
33+
- -10^9 <= target <= 10^9
34+
- Only one valid answer exists.
35+
36+
37+
<br>
38+
39+
- Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def two_sum(nums, target):
2+
num_idx = {}
3+
for i in range(len(nums)):
4+
diff = target - nums[i]
5+
if diff in num_idx:
6+
return [num_idx[diff], i]
7+
8+
num_idx[nums[i]] = i
9+
10+
return []
11+
12+
13+
print(two_sum([2,7,11,15], 9))
14+
print(two_sum([3,2,4], 6))
15+
print(two_sum([3,3], 6))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# [LC 217. Contains Duplicate - Easy](https://leetcode.com/problems/contains-duplicate/description/)
2+
3+
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
4+
5+
6+
### Example 1
7+
```
8+
Input: nums = [1,2,3,1]
9+
Output: true
10+
```
11+
12+
### Example 2
13+
```
14+
Input: nums = [1,2,3,4]
15+
Output: false
16+
```
17+
18+
### Example 3
19+
```
20+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
21+
Output: true
22+
```
23+
24+
### Constraints:
25+
26+
- 1 <= nums.length <= 10^5
27+
- -10^9 <= nums[i] <= 10^9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# Time: O(n)
3+
# Space: O(n)
4+
def containsDuplicate(nums) -> bool:
5+
hash_set = set()
6+
for val in nums:
7+
if val in hash_set:
8+
return True
9+
hash_set.add(val)
10+
11+
return False
12+
13+
14+
# Time: O(n)
15+
# Space: O(n)
16+
def containsDuplicate2(nums) -> bool:
17+
num_freq = {}
18+
for val in nums:
19+
if val in num_freq:
20+
return True
21+
num_freq[val] = 1
22+
23+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [LC 238. Product of Array Except Self- Medium](https://leetcode.com/problems/product-of-array-except-self/description/)
2+
3+
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
4+
5+
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
6+
7+
You must write an algorithm that runs in O(n) time and without using the division operation.
8+
9+
### Example 1
10+
```
11+
Input: nums = [1,2,3,4]
12+
Output: [24,12,8,6]
13+
```
14+
15+
### Example 2
16+
```
17+
Input: nums = [-1,1,0,-3,3]
18+
Output: [0,0,9,0,0]
19+
```
20+
21+
22+
### Constraints:
23+
24+
- 2 <= nums.length <= 10^5
25+
- -30 <= nums[i] <= 30
26+
- The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
27+
28+
29+
30+
31+
<br>
32+
33+
Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def productExceptSelf(nums):
2+
result = [1] * len(nums)
3+
prefix = 1
4+
for i in range(len(nums)):
5+
result[i] = prefix
6+
prefix *= nums[i]
7+
8+
postfix = 1
9+
for i in range(len(nums) - 1, -1, -1):
10+
result[i] *= postfix
11+
postfix *= nums[i]
12+
13+
return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [LC 242. Valid Anagram - Easy](https://leetcode.com/problems/valid-anagram/description/)
2+
3+
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
4+
5+
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
6+
7+
### Example 1
8+
```
9+
Input: s = "anagram", t = "nagaram"
10+
Output: true
11+
```
12+
13+
### Example 2
14+
```
15+
Input: s = "rat", t = "car"
16+
Output: false
17+
```
18+
19+
### Example 3
20+
```
21+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
22+
Output: true
23+
```
24+
25+
### Constraints:
26+
27+
- 1 <= s.length, t.length <= 5 * 10^4
28+
- s and t consist of lowercase English letters.
29+
30+
<br>
31+
32+
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def isAnagram(s, t) -> bool:
2+
if len(s) != len(t): return False
3+
char_freq = {}
4+
5+
for i in range(len(s)):
6+
st_1, st_2 = s[i], t[i]
7+
char_freq[st_1] = char_freq.get(st_1, 0) + 1
8+
char_freq[st_2] = char_freq.get(st_2, 0) - 1
9+
10+
if st_2 in char_freq and char_freq.get(st_1, 0) == 0:
11+
del char_freq[st_1]
12+
if st_2 in char_freq and char_freq[st_2] == 0:
13+
del char_freq[st_2]
14+
15+
return True if len(char_freq) == 0 else False
16+
17+
18+
def isAnagram2(s, t) -> bool:
19+
if len(s) != len(t): return False
20+
21+
char_count = {}
22+
for i in range(len(s)):
23+
char_count[s[i]] = char_count.get(s[i], 0) + 1
24+
char_count[t[i]] = char_count.get(t[i], 0) - 1
25+
26+
for count in char_count.values():
27+
if count != 0: return False
28+
29+
return True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [LC 347. Top K Frequent Elements - Medium](https://leetcode.com/problems/top-k-frequent-elements/description/)
2+
3+
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
4+
5+
### Example 1
6+
```
7+
Input: nums = [1,1,1,2,2,3], k = 2
8+
Output: [1,2]
9+
```
10+
11+
### Example 2
12+
```
13+
Input: nums = [1], k = 1
14+
Output: [1]
15+
```
16+
17+
### Constraints:
18+
19+
- 1 <= nums.length <= 10^5
20+
- -10^4 <= nums[i] <= 10^4
21+
- k is in the range [1, the number of unique elements in the array].
22+
- It is guaranteed that the answer is unique.
23+
24+
<br>
25+
26+
Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from heapq import *
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
def topKFrequent(nums, k):
6+
num_count = {}
7+
freq_bucket = [[] for _ in range(len(nums) + 1)]
8+
9+
for num in nums:
10+
num_count[num] = num_count.get(num, 0) + 1
11+
12+
for num, count in num_count.items():
13+
freq_bucket[count].append(num)
14+
15+
result = []
16+
for i in range(len(freq_bucket)-1, 0, -1):
17+
for num in freq_bucket[i]:
18+
result.append(num)
19+
if len(result) == k:
20+
return result
21+
22+
23+
24+
# Time: O(n + n + nlog(n) + n) ----> O(n + nlog(n))
25+
# Space: O(n)
26+
def topKFrequent2(nums, k):
27+
num_freq, max_heap = {}, []
28+
for num in nums:
29+
num_freq[num] = num_freq.get(num, 0) + 1
30+
31+
for num, count in num_freq.items():
32+
max_heap.append((-count, num))
33+
34+
heapify(max_heap)
35+
result = []
36+
while max_heap and k > 0:
37+
_, num = heappop(max_heap)
38+
result.append(num)
39+
k -= 1
40+
41+
return result

leetcode/src/linkedlist/lc_143_reorder_list/reorder_list.py

+32-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,38 @@ def __init__(self, val=0, next=None):
55
self.next = next
66

77
def reorderList(head) -> None:
8+
if not head or not head.next: return
9+
10+
last_node_of_first_half, slow, fast = None, head, head
11+
while fast and fast.next:
12+
last_node_of_first_half = slow
13+
slow, fast = slow.next, fast.next.next
14+
15+
last_node_of_first_half.next = None
16+
first_half, second_half = head, reverse(slow)
17+
18+
while first_half and second_half:
19+
first_half_next_node = first_half.next
20+
second_half_next_node = second_half.next
21+
first_half.next, second_half.next = second_half, first_half.next
22+
23+
if not first_half_next_node:
24+
second_half.next = second_half_next_node
25+
26+
first_half, second_half = first_half_next_node, second_half_next_node
27+
28+
29+
def reverse(node):
30+
prev, curr = None, node
31+
while curr:
32+
next_node = curr.next
33+
curr.next = prev
34+
prev, curr = curr, next_node
35+
36+
return prev
37+
38+
39+
def reorderList2(head) -> None:
840
if not head and not head.next: return head
941
slow = fast = head
1042
while fast and fast.next:
@@ -23,11 +55,3 @@ def reorderList(head) -> None:
2355
if head_of_first_half:
2456
head_of_first_half.next = None
2557

26-
def reverse(node):
27-
prev, curr = None, node
28-
while curr:
29-
next_node = curr.next
30-
curr.next = prev
31-
prev, curr = curr, next_node
32-
33-
return prev

leetcode/src/linkedlist/lc_2_add_two_numbers/add_two_numbers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ def __init__(self, val=0, next=None):
66

77

88
def addTwoNumbers(l1, l2):
9-
sentinel = result = ListNode(0)
9+
result_iter = result = ListNode(0)
1010
remainder = 0
1111
while l1 or l2 or remainder:
1212
l1_val = l1.val if l1 else 0
1313
l2_val = l2.val if l2 else 0
1414

1515
curr_sum = l1_val + l2_val + remainder
16-
sentinel.next = ListNode(curr_sum % 10)
16+
result_iter.next = ListNode(curr_sum % 10)
1717
remainder = curr_sum // 10
1818

1919
l1 = l1.next if l1 else None
2020
l2 = l2.next if l2 else None
21-
sentinel = sentinel.next
21+
result_iter = result_iter.next
2222

2323
return result.next

0 commit comments

Comments
 (0)