Skip to content

Commit bb3ece6

Browse files
committed
Merge repository 'geemaple/leetcode'
2 parents bcf75af + 9dc2a38 commit bb3ece6

35 files changed

+1548
-1
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 渴望越狱的猫
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,110 @@
11
# Algorithms
2+
23
Algorithms written in python
34

4-
# [Sortings](https://github.com/geemaple/algorithm/tree/master/Sorting)
5+
# [Sortings](https://github.com/geemaple/algorithm/tree/master/Sorting)
6+
7+
# Leetcode
8+
My personal leetcode answer in python<br/>
9+
This is a **continually updated, open source** project.
10+
11+
12+
🎉🎉🎉 Finished 100 questions on 2018-05-28
13+
14+
# Algorithms
15+
16+
- [Leetcode](#leetcode)
17+
- [Algorithms](#algorithms)
18+
- [Array](#array)
19+
- [Two Pointers](#two-pointers)
20+
- [Linked List](#linked-list)
21+
- [Binary Search](#binary-search)
22+
- [Tree Traversal](#tree-traversal)
23+
- [Graph Traversal](#graph-traversal)
24+
- [Divide and Conquer](#divide-and-conquer)
25+
- [Backtracking](#backtracking)
26+
- [Topological Sort](#topological-sort)
27+
- [Dynamic Programming](#dynamic-programming)
28+
29+
## Array
30+
31+
| Problem | Solution | Time | Difficulty | Tag | Note|
32+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
33+
| 88.merge-sorted-array | [python](./algorithm/88.merge-sorted-array.py) | O(N + M) | Easy |
34+
| 349.intersection-of-two-arrays | [python](./algorithm/349.intersection-of-two-arrays.py) O(N + M) |Easy
35+
| 350.intersection-of-two-arrays-ii | [python](./algorithm/350.intersection-of-two-arrays-ii.py) O(N * logN) |Easy
36+
37+
## Two Pointers
38+
39+
| Problem | Solution | Time | Difficulty | Tag | Note|
40+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
41+
| 19.remove-nth-node-from-end-of-list | [python](./algorithm/19.remove-nth-node-from-end-of-list.py) | O(N) | Medium
42+
| 141.linked-list-cycle | [python](./algorithm/141.linked-list-cycle.py) | O(N) | Easy | linked-list
43+
| 142.linked-list-cycle-ii | [python](./algorithm/142.linked-list-cycle-ii.py) | O(N) | Medium | linked-list
44+
| 160.intersection-of-two-linked-lists | [python](./algorithm/160.intersection-of-two-linked-lists.py) | O(N + M) | Easy | linked-list
45+
46+
47+
## Linked List
48+
49+
| Problem | Solution | Time | Difficulty | Tag | Note|
50+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
51+
| 21.merge-two-sorted-lists | [python](./algorithm/21.merge-two-sorted-lists.py) | O(N) | Easy |
52+
| 25.reverse-nodes-in-k-group | [python](./algorithm/25.reverse-nodes-in-k-group.py) | O(N) | Hard |
53+
| 61.rotate-list | [python](./algorithm/61.rotate-list.py) | O(N) | Medium |
54+
| 86.partition-list | [python](./algorithm/86.partition-list.py) | O(N) | Medium |
55+
| 92.reverse-linked-list-ii | [python](./algorithm/92.reverse-linked-list-ii.py) | O(N) | Medium |
56+
| 138.copy-list-with-random-pointer | [python](./algorithm/138.copy-list-with-random-pointer.py) | O(N) | Medium
57+
| 143.reorder-list | [python](./algorithm/143.reorder-list.py) | O(N) | Medium |
58+
| 148.sort-list | [python](./algorithm/148.sort-list.py) | O(N * logN) | Medium |
59+
| 203.remove-linked-list-elements | [python](./algorithm/203.remove-linked-list-elements.py) | O(N) | Easy
60+
| 206.reverse-linked-list | [python](./algorithm/206.reverse-linked-list.py) | O(N) | Easy |
61+
| 237.delete-node-in-a-linked-list | [python](./algorithm/237.delete-node-in-a-linked-list.py) | O(1) | Easy
62+
63+
## Binary Search
64+
65+
| Problem | Solution | Time | Difficulty | Tag | Note|
66+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
67+
| 278.first-bad-version | [python](./algorithm/278.first-bad-version.py) | O(LogN) | Easy
68+
69+
## Divide and Conquer
70+
71+
| Problem | Solution | Time | Difficulty | Tag | Note|
72+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
73+
74+
75+
## Tree Traversal
76+
77+
| Problem | Solution | Time | Difficulty | Tag | Note|
78+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
79+
80+
81+
## Graph Traversal
82+
83+
| Problem | Solution | Time | Difficulty | Tag | Note|
84+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
85+
| 127.word-ladder | [python](./algorithm/127.word-ladder.py) | O(N * L^2) | Medium | BFS |
86+
| 200.number-of-islands | [python](./algorithm/200.number-of-islands.py) | O(ROW x COL) | Medium | BFS/DFS | union-find
87+
88+
## Backtracking
89+
| Problem | Solution | Time | Difficulty | Tag | Note|
90+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
91+
| 39.combination-sum | [python](./algorithm/39.combination-sum.py) | ??? | Medium | DFS |
92+
| 40.combination-sum-ii | [python](./algorithm/40.combination-sum-ii.py) | ??? | Medium | DFS |
93+
| 46.permutations | [python](./algorithm/46.permutations.py) | ??? | Medium | DFS |
94+
| 47.permutations-ii | [python](./algorithm/47.permutations-ii.py) | ??? | Medium | DFS |
95+
| 51.n-queens | [python](./algorithm/51.n-queens.py) | ??? | Hard | DFS |
96+
| 52.n-queens-ii | [python](./algorithm/52.n-queens-ii.py) | ??? | Hard | DFS |
97+
| 78.subsets | [python](./algorithm/78.subsets.py) | ??? | Medium | DFS | bit-manipulation
98+
| 90.subsets-ii | [python](./algorithm/90.subsets-ii.py) | ??? | Medium | DFS | bit-manipulation
99+
| 126.word-ladder-ii | [python](./algorithm/126.word-ladder-ii.py) | O((V+E) * L^2) | Hard | BFS+DFS |
100+
| 131.palindrome-partitioning | [python](./algorithm/131.palindrome-partitioning.py) | ??? | Medium | DFS | dynamic-programming
101+
102+
## Topological Sort
103+
104+
| Problem | Solution | Time | Difficulty | Tag | Note|
105+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|
106+
| 444.sequence-reconstruction | [python](./algorithm/444.sequence-reconstruction.py) | O(V+E) | Medium | BFS/DFS |
107+
108+
## Dynamic Programming
109+
| Problem | Solution | Time | Difficulty | Tag | Note|
110+
| ----------------- | --------------- | --------------- | ------------- |--------------|-----|

algorithm/126.word-ladder-ii.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
class Solution(object):
2+
def findLadders(self, beginWord, endWord, wordList):
3+
"""
4+
:type beginWord: str
5+
:type endWord: str
6+
:type wordList: List[str]
7+
:rtype: List[List[str]]
8+
"""
9+
10+
distant_map = {}
11+
for word in wordList:
12+
distant_map[word] = 0
13+
14+
if endWord not in distant_map:
15+
return []
16+
17+
# using BFS to get each word distancd to endWord
18+
self.bfs_traversal(endWord, distant_map)
19+
20+
results = []
21+
tmp = [beginWord]
22+
visted = set()
23+
self.dfs_traversal(beginWord, endWord, tmp, results, visted, distant_map)
24+
return results
25+
26+
def dfs_traversal(self, word, endWord, tmp, results, visted, distant_map):
27+
if word == endWord:
28+
results.append(list(tmp))
29+
return
30+
31+
distance = distant_map.get(word, float('inf'))
32+
neighbors = self.get_neighbors(word, visted, distant_map)
33+
34+
print word, neighbors
35+
for neighbor in neighbors:
36+
if distant_map[neighbor] >= distance:
37+
continue
38+
39+
visted.add(neighbor)
40+
41+
tmp.append(neighbor)
42+
self.dfs_traversal(neighbor, endWord, tmp, results, visted, distant_map)
43+
tmp.pop()
44+
45+
visted.remove(neighbor)
46+
47+
48+
def bfs_traversal(self, endWord, distant_map):
49+
50+
visted = set()
51+
queue = [endWord]
52+
visted.add(endWord)
53+
count = 0
54+
55+
while(len(queue) > 0):
56+
57+
size = len(queue)
58+
59+
for _ in range(size):
60+
word = queue.pop(0)
61+
62+
distant_map[word] = count
63+
64+
neighbors = self.get_neighbors(word, visted, distant_map)
65+
for neighbor in neighbors:
66+
queue.append(neighbor)
67+
visted.add(neighbor)
68+
69+
count += 1
70+
71+
72+
def get_neighbors(self, word, visted, distant_map):
73+
74+
results = []
75+
76+
for i in range(len(word)):
77+
for letter in 'abcdefghijklmnopqrstuvwxyz':
78+
if letter == word[i]:
79+
continue
80+
81+
neighbor = word[:i] + letter + word[(i + 1):]
82+
83+
if neighbor not in distant_map:
84+
continue
85+
86+
if neighbor in visted:
87+
continue
88+
89+
results.append(neighbor)
90+
91+
return results

algorithm/127.word-ladder.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Solution(object):
2+
def ladderLength(self, beginWord, endWord, wordList):
3+
"""
4+
:type beginWord: str
5+
:type endWord: str
6+
:type wordList: List[str]
7+
:rtype: int
8+
"""
9+
count = 0
10+
word_set = set(wordList)
11+
12+
if endWord not in word_set:
13+
return 0
14+
15+
queue = [beginWord]
16+
17+
while(len(queue) > 0):
18+
19+
size = len(queue)
20+
count += 1
21+
22+
for _i in range(size):
23+
node = queue.pop(0)
24+
25+
if node == endWord:
26+
return count
27+
28+
neighbors = self.get_neighbors(node, word_set)
29+
for neighbor in neighbors:
30+
queue.append(neighbor)
31+
32+
return 0
33+
34+
# using 26 letters is better than check every nodes in wordList
35+
# l = length of word
36+
# 26 letters = O(l * 25 * l)
37+
# each word has l * 25 variation, and to check or change each attempt using l
38+
#
39+
# check every nodes in wordlist = O(l * N * N)
40+
# to check neighbors of every word in the list needs l * N, and there are N such words
41+
def get_neighbors(self, word, word_set):
42+
results = []
43+
for i in range(len(word)):
44+
for letter in 'abcdefghijklmnopqrstuvwxyz':
45+
if letter == word[i]:
46+
continue
47+
48+
neighbor = word[:i] + letter + word[(i + 1):]
49+
if neighbor not in word_set:
50+
continue
51+
52+
results.append(neighbor)
53+
word_set.remove(neighbor) #this is important
54+
55+
return results
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class Solution(object):
2+
def partition(self, s):
3+
"""
4+
:type s: str
5+
:rtype: List[List[str]]
6+
"""
7+
8+
if s is None:
9+
return []
10+
11+
results = []
12+
partitions = []
13+
self.helper(s, partitions, results, 0)
14+
return results
15+
16+
def helper(self, s, partitions, results, start):
17+
18+
if start == len(s):
19+
results.append(list(partitions))
20+
21+
for i in range(start, len(s)):
22+
sub = s[start: i + 1]
23+
if not self.is_palindrome(sub):
24+
continue
25+
26+
partitions.append(sub)
27+
self.helper(s, partitions, results, i + 1)
28+
partitions.pop()
29+
30+
31+
def is_palindrome(self, part):
32+
length = len(part)
33+
if length <= 1:
34+
return True
35+
36+
for i in range(length):
37+
if part[i] != part[length - 1 - i]:
38+
return False
39+
40+
return True
41+
42+
# since is_palindrome func is called most,
43+
# we can optimize this with a prebuit matrix
44+
def build_matrix(self, s):
45+
46+
def expand(matrix, begin, end):
47+
# A palindrome added the SAME char to both sides is aslo a valid palindrome
48+
while (begin - 1 >= 0 and end + 1 <= size):
49+
matrix[begin - 1][end + 1] = matrix[begin][end] & int(s[begin - 1] == s[end])
50+
begin = begin - 1
51+
end = end + 1
52+
53+
size = len(s)
54+
# (n + 1) * n
55+
matrix = [[0 for _ in range(size + 1)] for _ in range(size)]
56+
57+
for i in range(size):
58+
# s[i:i] = ''
59+
matrix[i][i] = 1
60+
expand(matrix, i, i)
61+
if i + 1 <= size:
62+
# s[i: i + 1] = 'a single char'
63+
matrix[i][i + 1] = 1
64+
expand(matrix, i, i + 1)

0 commit comments

Comments
 (0)