parent
b6e8283908
commit
9d74d2b836
@ -0,0 +1,37 @@
|
|||||||
|
---
|
||||||
|
id: 1
|
||||||
|
title: Histogram Stars
|
||||||
|
topics: [array]
|
||||||
|
difficulty: easy
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Given an array of integers, print out a histogram using the array. Include a base layer (all stars)
|
||||||
|
|
||||||
|
[5, 4, 0, 3, 4, 1] will result in the following:
|
||||||
|
|
||||||
|
******
|
||||||
|
*****
|
||||||
|
*
|
||||||
|
****
|
||||||
|
*****
|
||||||
|
**
|
||||||
|
'''
|
||||||
|
```
|
||||||
|
|
||||||
|
## Follow Up
|
||||||
|
|
||||||
|
```
|
||||||
|
Print out a vertical version of the histogram.
|
||||||
|
|
||||||
|
*
|
||||||
|
** *
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** ***
|
||||||
|
******
|
||||||
|
'''
|
||||||
|
```
|
@ -0,0 +1,56 @@
|
|||||||
|
---
|
||||||
|
id: 2
|
||||||
|
title: Matrix Islands
|
||||||
|
topics: [array, depth-first-search, matrix]
|
||||||
|
difficulty: easy
|
||||||
|
source:
|
||||||
|
- https://leetcode.com/problems/number-of-islands/
|
||||||
|
- https://leetcode.com/problems/max-area-of-island/
|
||||||
|
- https://leetcode.com/problems/number-of-distinct-islands/
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Given a matrix of 0s and 1s, count the number of islands present.
|
||||||
|
|
||||||
|
[[0,0,1],
|
||||||
|
[0,0,0],
|
||||||
|
[0,1,1]]
|
||||||
|
|
||||||
|
Answer: 2
|
||||||
|
'''
|
||||||
|
```
|
||||||
|
|
||||||
|
## Follow Up
|
||||||
|
|
||||||
|
```
|
||||||
|
Given a matrix of 0s and 1s, find the size of the largest island present.
|
||||||
|
|
||||||
|
'''
|
||||||
|
[[0,0,1],
|
||||||
|
[0,0,0],
|
||||||
|
[0,1,1]]
|
||||||
|
|
||||||
|
Answer: 2
|
||||||
|
```
|
||||||
|
|
||||||
|
## Follow Up II
|
||||||
|
|
||||||
|
```
|
||||||
|
Given a matrix of 0s and 1s, find the number of unique islands present present.
|
||||||
|
|
||||||
|
'''
|
||||||
|
[[1,0,1,0],
|
||||||
|
[1,0,0,0],
|
||||||
|
[0,1,1,0]]
|
||||||
|
|
||||||
|
Answer: 3
|
||||||
|
|
||||||
|
[[1,1,0,1],
|
||||||
|
[0,0,0,0],
|
||||||
|
[0,1,1,0]]
|
||||||
|
|
||||||
|
Answer: 2
|
||||||
|
```
|
@ -0,0 +1,40 @@
|
|||||||
|
---
|
||||||
|
id: 3
|
||||||
|
title: K Smallest Elements from Two Sorted Arrays
|
||||||
|
topics: [array]
|
||||||
|
difficulty: easy
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Find the k smallest elements from two sorted arrays of integers.
|
||||||
|
|
||||||
|
Input: [1, 2, 3, 4, 5], [2, 3, 4], k = 3
|
||||||
|
Output: [1, 2, 2]
|
||||||
|
'''
|
||||||
|
```
|
||||||
|
|
||||||
|
## Time Complexities
|
||||||
|
|
||||||
|
- Bad:
|
||||||
|
- Time: O((n + m)log(n + m))
|
||||||
|
- Good:
|
||||||
|
- Time: O(k)
|
||||||
|
|
||||||
|
## Sample Answers
|
||||||
|
|
||||||
|
```py
|
||||||
|
def k_smallest(A, B, k):
|
||||||
|
res = []
|
||||||
|
a = b = 0
|
||||||
|
while a < len(A) and b < len(B) and (a + b) < k:
|
||||||
|
if A[a] < B[b]:
|
||||||
|
res.append(A[a])
|
||||||
|
a += 1
|
||||||
|
else:
|
||||||
|
res.append(B[b])
|
||||||
|
b += 1
|
||||||
|
return res + A[a:k - b] + B[b:k - a]
|
||||||
|
```
|
@ -0,0 +1,36 @@
|
|||||||
|
---
|
||||||
|
id: 4
|
||||||
|
title: Synonyms
|
||||||
|
topics: [graph]
|
||||||
|
difficulty: medium
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Given a list of synonym pairs, determine if two words are synonymous.
|
||||||
|
Synonyms have a symmetric and transitive relation. i.e. if a <-> b and b <-> c, a <-> c.
|
||||||
|
|
||||||
|
Input:
|
||||||
|
[["computer", "laptop"]]
|
||||||
|
"computer"
|
||||||
|
"laptop"
|
||||||
|
|
||||||
|
Output: true
|
||||||
|
|
||||||
|
Input:
|
||||||
|
[["computer", "laptop"], ["laptop", "pc"]]
|
||||||
|
"computer"
|
||||||
|
"pc"
|
||||||
|
|
||||||
|
Output: true
|
||||||
|
|
||||||
|
Input:
|
||||||
|
[["computer", "laptop"], ["laptop", "pc"], ["tablet", "iPad"]]
|
||||||
|
"computer"
|
||||||
|
"iPad"
|
||||||
|
|
||||||
|
Output: false
|
||||||
|
'''
|
||||||
|
```
|
@ -0,0 +1,33 @@
|
|||||||
|
---
|
||||||
|
id: 5
|
||||||
|
title: Longest Consecutive Set of Numbers
|
||||||
|
topics: [graph]
|
||||||
|
difficulty: hard
|
||||||
|
source: https://leetcode.com/problems/longest-consecutive-sequence/
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Given an array of integers, find the size of the largest set of consecutive numbers present in the array.
|
||||||
|
|
||||||
|
Input: [100, 4, 200, 1, 3, 2]
|
||||||
|
Output: 4 because {1, 2, 3, 4}
|
||||||
|
'''
|
||||||
|
```
|
||||||
|
|
||||||
|
## Sample Answers
|
||||||
|
|
||||||
|
```py
|
||||||
|
def longest_consecutive(nums):
|
||||||
|
nums = set(nums)
|
||||||
|
best = 0
|
||||||
|
for x in nums:
|
||||||
|
if x - 1 not in nums:
|
||||||
|
y = x + 1
|
||||||
|
while y in nums:
|
||||||
|
y += 1
|
||||||
|
best = max(best, y - x)
|
||||||
|
return best
|
||||||
|
```
|
@ -0,0 +1,25 @@
|
|||||||
|
---
|
||||||
|
id: 6
|
||||||
|
title: String Compression and Decompression
|
||||||
|
topics: [string]
|
||||||
|
difficulty: easy
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Given a string, compress it by grouping repeated characters. The length after
|
||||||
|
compression must always be smaller than or equal to the original string.
|
||||||
|
|
||||||
|
'aaabbccc' => 'a3b2c3'
|
||||||
|
'''
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Given the above compressed string, decompress to obtain the original string.
|
||||||
|
|
||||||
|
'a3b2c3' => 'aaabbccc'
|
||||||
|
'''
|
||||||
|
```
|
@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
id: 7
|
||||||
|
title: Biggest Number After Swap
|
||||||
|
topics: [math]
|
||||||
|
difficulty: medium
|
||||||
|
source: https://leetcode.com/problems/maximum-swap/
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Given a non-negative integer, find the maximum possible number if you can swap two digits at most once.
|
||||||
|
|
||||||
|
2736 => 7236
|
||||||
|
'''
|
||||||
|
```
|
||||||
|
|
||||||
|
## Follow Up
|
||||||
|
|
||||||
|
- What if the given integer can be negative?
|
@ -0,0 +1,19 @@
|
|||||||
|
---
|
||||||
|
id: 8
|
||||||
|
title: Pivot Index
|
||||||
|
topics: [array]
|
||||||
|
difficulty: easy
|
||||||
|
source: https://leetcode.com/problems/find-pivot-index/
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Given an array of integers, return the pivot index of this array.
|
||||||
|
|
||||||
|
A pivot index is the index where the sum of the numbers to its left is equal to the sum of the numbers to its right:
|
||||||
|
|
||||||
|
[7, 2, 3, 4] => 1
|
||||||
|
'''
|
||||||
|
```
|
@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
id: 9
|
||||||
|
title: Move Zeroes
|
||||||
|
topics: [array]
|
||||||
|
difficulty: easy
|
||||||
|
source: https://leetcode.com/problems/move-zeroes/
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question
|
||||||
|
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
Given an array of integers, write a function to move all zeroes to the end of it while
|
||||||
|
maintaining the relative order of the non-zero elements.
|
||||||
|
|
||||||
|
[0, 1, 0, 3, 12] => [1, 3, 12, 0, 0].
|
||||||
|
'''
|
||||||
|
```
|
Loading…
Reference in new issue