Skip to content

Commit 698cbc6

Browse files
committed
339
1 parent 10e4f9e commit 698cbc6

File tree

13 files changed

+367
-3
lines changed

13 files changed

+367
-3
lines changed

269_alien_dictionary/dictionary.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,37 @@ def alienOrder(self, words):
1818
if word1[j]!=word2[j]:
1919
self.add_to_mat(word1[j], word2[j])
2020
break
21-
21+
22+
# the alphabet only includes the chars that appear in words
2223
alphabet = set()
2324
for word in words:
24-
alphabet.add([c for c in word])
25+
for c in word:
26+
alphabet.add(c)
2527

2628
# toplogical sort
29+
degree_in = {}
30+
for char in alphabet:
31+
j = ord(char)-ord('a')
32+
degree_in[char]=sum([self.matrix[i][j] for i in range(26)])
2733

28-
34+
result = ""
35+
while True:
36+
hasZeroDegree=False
37+
for c in degree_in:
38+
if degree_in[c]==0:
39+
hasZeroDegree = True
40+
degree_in[c]=-1 # skip in the following checks
41+
result += c
42+
for k in range(26):
43+
if self.matrix[ord(c)-ord('a')][k]>0:
44+
degree_in[chr(ord('a')+k)]-=1
45+
# print c
46+
# print degree_in
47+
if not hasZeroDegree:
48+
if len(result)==len(alphabet):
49+
return result
50+
return ""
51+
2952
def add_to_mat(self, c1, c2):
3053
i1 = ord(c1)-ord('a')
3154
i2 = ord(c2)-ord('a')

269_alien_dictionary/problem.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
2+
3+
For example,
4+
Given the following words in dictionary,
5+
6+
[
7+
"wrt",
8+
"wrf",
9+
"er",
10+
"ett",
11+
"rftt"
12+
]
13+
The correct order is: "wertf".
14+
15+
Note:
16+
You may assume all letters are in lowercase.
17+
If the order is invalid, return an empty string.
18+
There may be multiple valid order of letters, return any one of them is fine.

276_paint_fence/paint.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def numWays(self, n, k):
3+
"""
4+
:type n: int
5+
:type k: int
6+
:rtype: int
7+
"""
8+
self.memo = {}
9+
return self.recurse(n, k)
10+
11+
def recurse(self, n, k):
12+
if n==0: return 0
13+
if n==1: return k
14+
if n==2: return k*k
15+
if n not in self.memo:
16+
self.memo[n] = self.recurse(n-1,k)*(k-1)+self.recurse(n-2,k)*(k-1)
17+
return self.memo[n]
18+
19+
20+
21+
22+

276_paint_fence/problem.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
There is a fence with n posts, each post can be painted with one of the k colors.
2+
3+
You have to paint all the posts such that no more than two adjacent fence posts have the same color.
4+
5+
Return the total number of ways you can paint the fence.
6+
7+
Note:
8+
n and k are non-negative integers.
9+
10+
Hide Company Tags Google
11+
Show Tags
12+
Show Similar Problems
13+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution(object):
2+
def minArea(self, image, x, y):
3+
"""
4+
:type image: List[List[str]]
5+
:type x: int
6+
:type y: int
7+
:rtype: int
8+
"""
9+
if not image:
10+
return 0
11+
# BFS from x, y to find the boundaries
12+
xl = len(image)
13+
yl = len(image[0])
14+
minx, maxx = x, x
15+
miny, maxy = y, y
16+
17+
visited = set((x, y))
18+
q = [(x,y)]
19+
20+
# Loop for BFS
21+
while q:
22+
xx, yy = q.pop(0)
23+
directions = [(1,0), (-1,0), (0, 1), (0, -1)]
24+
for dx, dy in directions:
25+
# print xx+dx, yy+dy
26+
if (0<=xx+dx<xl and 0<=yy+dy<yl and image[xx+dx][yy+dy]=='1'
27+
and (xx+dx, yy+dy) not in visited):
28+
q.append((xx+dx, yy+dy))
29+
visited.add((xx+dx, yy+dy))
30+
minx = min(minx, xx+dx)
31+
maxx = max(maxx, xx+dx)
32+
miny = min(miny, yy+dy)
33+
maxy = max(maxy, yy+dy)
34+
# print q
35+
# print minx, maxx, miny, maxy
36+
return (maxx-minx+1)*(maxy-miny+1)
37+
38+
39+
40+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution(object):
2+
def minArea(self, image, x, y):
3+
"""
4+
:type image: List[List[str]]
5+
:type x: int
6+
:type y: int
7+
:rtype: int
8+
"""
9+
if not image:
10+
return 0
11+
# BFS from x, y to find the boundaries
12+
xl = len(image)
13+
yl = len(image[0])
14+
minx, maxx = x, x
15+
miny, maxy = y, y
16+
17+
visited = set((x, y))
18+
q = [(x,y)]
19+
20+
# Loop for BFS
21+
while q:
22+
xx, yy = q.pop(0)
23+
directions = [(1,0), (-1,0), (0, 1), (0, -1)]
24+
for dx, dy in directions:
25+
# print xx+dx, yy+dy
26+
if (0<=xx+dx<xl and 0<=yy+dy<yl and image[xx+dx][yy+dy]=='1'
27+
and (xx+dx, yy+dy) not in visited):
28+
q.append((xx+dx, yy+dy))
29+
visited.add((xx+dx, yy+dy))
30+
minx = min(minx, xx+dx)
31+
maxx = max(maxx, xx+dx)
32+
miny = min(miny, yy+dy)
33+
maxy = max(maxy, yy+dy)
34+
# print q
35+
# print minx, maxx, miny, maxy
36+
return (maxx-minx+1)*(maxy-miny+1)
37+
38+
39+
40+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import bisect
2+
class Solution(object):
3+
def minArea(self, image, x, y):
4+
"""
5+
:type image: List[List[str]]
6+
:type x: int
7+
:type y: int
8+
:rtype: int
9+
"""
10+
if not image:
11+
return 0
12+
# BFS from x, y to find the boundaries
13+
xl = len(image)
14+
yl = len(image[0])
15+
minx, maxx = x, x+1
16+
miny, maxy = y, y+1
17+
18+
lo, hi = 0, x
19+
while lo<hi:
20+
mid = (lo+hi)/2
21+
if any([c=='1' for c in image[mid]]):
22+
hi = mid
23+
else:
24+
lo = mid+1
25+
minx = hi
26+
27+
lo, hi = x, xl
28+
while lo<hi:
29+
mid = (lo+hi)/2
30+
if any([c=='1' for c in image[mid]]):
31+
lo = mid+1
32+
else:
33+
hi = mid
34+
maxx = hi
35+
print minx, maxx
36+
lo, hi = 0, y
37+
while lo<hi:
38+
mid = (lo+hi)/2
39+
if any([image[i][mid]=='1' for i in range(minx, maxx)]):
40+
hi = mid
41+
else:
42+
lo = mid+1
43+
miny = hi
44+
45+
lo, hi = y, yl
46+
while lo<hi:
47+
mid = (lo+hi)/2
48+
if any([image[i][mid]=='1' for i in range(minx, maxx)]):
49+
lo = mid+1
50+
else:
51+
hi = mid
52+
maxy = hi
53+
print miny, maxy
54+
return (maxx-minx)*(maxy-miny)
55+
56+
57+
58+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def multiply(self, A, B):
3+
"""
4+
:type A: List[List[int]]
5+
:type B: List[List[int]]
6+
:rtype: List[List[int]]
7+
"""
8+
if not A or not B: return []
9+
ra = len(A)
10+
la = len(A[0])
11+
rb = len(B)
12+
lb = len(B[0])
13+
14+
b_status = [any([B[i][j] for i in range(rb)]) for j in range(lb)]
15+
result = []
16+
for i in range(ra):
17+
if not any(A[i]):
18+
result.append([0]*lb)
19+
else:
20+
temp = []
21+
for j in range(lb):
22+
if not b_status:
23+
temp.append(0)
24+
else:
25+
temp.append(sum([A[i][x]*B[x][j] for x in range(la)]))
26+
result.append(temp)
27+
return result
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Given two sparse matrices A and B, return the result of AB.
2+
3+
You may assume that A's column number is equal to B's row number.
4+
5+
Example:
6+
7+
A = [
8+
[ 1, 0, 0],
9+
[-1, 0, 3]
10+
]
11+
12+
B = [
13+
[ 7, 0, 0 ],
14+
[ 0, 0, 0 ],
15+
[ 0, 0, 1 ]
16+
]
17+
18+
19+
| 1 0 0 | | 7 0 0 | | 7 0 0 |
20+
AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |
21+
| 0 0 1 |
22+
Hide Company Tags LinkedIn
23+
Show Tags
24+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from collections import defaultdict
2+
3+
class Solution(object):
4+
def countComponents(self, n, edges):
5+
"""
6+
:type n: int
7+
:type edges: List[List[int]]
8+
:rtype: int
9+
"""
10+
if n==0:
11+
return 0
12+
if not edges:
13+
return n
14+
# First convert the edge list to a map
15+
edge_map = defaultdict(list)
16+
for x, y in edges:
17+
edge_map[x].append(y)
18+
edge_map[y].append(x)
19+
20+
components = 0
21+
left = set(range(n))
22+
while left:
23+
components+=1
24+
q = [left.pop()]
25+
while q:
26+
v = q.pop(0)
27+
for vv in edge_map[v]:
28+
if vv in left:
29+
q.append(vv)
30+
left.remove(vv)
31+
32+
return components
33+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.
2+
3+
Example 1:
4+
0 3
5+
| |
6+
1 --- 2 4
7+
Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.
8+
9+
Example 2:
10+
0 4
11+
| |
12+
1 --- 2 --- 3
13+
Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 1.
14+
15+
Note:
16+
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Given a nested list of integers, return the sum of all integers in the list weighted by their depth.
2+
3+
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
4+
5+
Example 1:
6+
Given the list [[1,1],2,[1,1]], return 10. (four 1's at depth 2, one 2 at depth 1)
7+
8+
Example 2:
9+
Given the list [1,[4,[6]]], return 27. (one 1 at depth 1, one 4 at depth 2, and one 6 at depth 3; 1 + 4*2 + 6*3 = 27)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# """
2+
# This is the interface that allows for creating nested lists.
3+
# You should not implement it, or speculate about its implementation
4+
# """
5+
#class NestedInteger(object):
6+
# def isInteger(self):
7+
# """
8+
# @return True if this NestedInteger holds a single integer, rather than a nested list.
9+
# :rtype bool
10+
# """
11+
#
12+
# def getInteger(self):
13+
# """
14+
# @return the single integer that this NestedInteger holds, if it holds a single integer
15+
# Return None if this NestedInteger holds a nested list
16+
# :rtype int
17+
# """
18+
#
19+
# def getList(self):
20+
# """
21+
# @return the nested list that this NestedInteger holds, if it holds a nested list
22+
# Return None if this NestedInteger holds a single integer
23+
# :rtype List[NestedInteger]
24+
# """
25+
26+
class Solution(object):
27+
def depthSum(self, nestedList):
28+
"""
29+
:type nestedList: List[NestedInteger]
30+
:rtype: int
31+
"""
32+
return self.recurse(nestedList, 1)
33+
34+
def recurse(self, ll, weight):
35+
sum = 0
36+
for item in ll:
37+
if item.isInteger():
38+
sum += item.getInteger()*weight
39+
else:
40+
sum += self.recurse(item.getList(), weight+1)
41+
return sum

0 commit comments

Comments
 (0)