Skip to content

Commit 2f12988

Browse files
committed
2019-08-24
1 parent 798a401 commit 2f12988

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def calculateTime(self, keyboard, word):
3+
"""
4+
:type keyboard: str
5+
:type word: str
6+
:rtype: int
7+
"""
8+
dic = dict()
9+
for i, char in enumerate(keyboard):
10+
dic[char] = i
11+
12+
res, last_pos = 0, 0
13+
for char in word:
14+
res += abs(dic[char] - last_pos)
15+
last_pos = dic[char]
16+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class FileSystem(object):
2+
3+
def __init__(self):
4+
self.dic = dict()
5+
6+
def create(self, path, value):
7+
"""
8+
:type path: str
9+
:type value: int
10+
:rtype: bool
11+
"""
12+
if path in self.dic:
13+
return False
14+
parent = path.split("/")
15+
parent_path = ""
16+
for i in range(1, len(parent) - 1):
17+
# print parent, i
18+
parent_path += "/" + parent[i]
19+
# print parent_path
20+
if parent_path not in self.dic:
21+
return False
22+
self.dic[path] = value
23+
return True
24+
25+
26+
def get(self, path):
27+
"""
28+
:type path: str
29+
:rtype: int
30+
"""
31+
return self.dic[path] if path in self.dic else -1
32+
33+
34+
35+
# Your FileSystem object will be instantiated and called as such:
36+
# obj = FileSystem()
37+
# param_1 = obj.create(path,value)
38+
# param_2 = obj.get(path)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def connectSticks(self, sticks):
3+
"""
4+
:type sticks: List[int]
5+
:rtype: int
6+
"""
7+
from heapq import *
8+
heapify(sticks)
9+
res = 0
10+
while len(sticks) > 1:
11+
s1 = heappop(sticks)
12+
s2 = heappop(sticks)
13+
res += s1 + s2
14+
heappush(sticks, s1 + s2)
15+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
class UnionFindSet(object):
3+
def __init__(self, n):
4+
5+
self.roots = [i for i in range(n + 1)]
6+
self.rank = [0 for i in range(n + 1)]
7+
self.count = n
8+
9+
def find(self, member):
10+
tmp = []
11+
while member != self.roots[member]:
12+
tmp.append(member)
13+
member = self.roots[member]
14+
for root in tmp:
15+
self.roots[root] = member
16+
return member
17+
18+
def union(self, p, q):
19+
parentP = self.find(p)
20+
parentQ = self.find(q)
21+
if parentP != parentQ:
22+
if self.rank[parentP] > self.rank[parentQ]:
23+
self.roots[parentQ] = parentP
24+
elif self.rank[parentP] < self.rank[parentQ]:
25+
self.roots[parentP] = parentQ
26+
else:
27+
self.roots[parentQ] = parentP
28+
self.rank[parentP] -= 1
29+
self.count -= 1
30+
31+
class Solution(object):
32+
def minCostToSupplyWater(self, n, wells, pipes):
33+
"""
34+
:type n: int
35+
:type wells: List[int]
36+
:type pipes: List[List[int]]
37+
:rtype: int
38+
"""
39+
from heapq import *
40+
for i, well in enumerate(wells):
41+
pipes.append([0, i + 1, well])
42+
43+
queue = []
44+
ufs = UnionFindSet(n)
45+
for start, end, cost in pipes:
46+
queue.append([cost, start, end])
47+
48+
heapify(queue)
49+
res = 0
50+
while ufs.count > 0:
51+
52+
cost, start, end = heappop(queue)
53+
# print ufs.roots, cost, start, end
54+
if ufs.find(start) == ufs.find(end):
55+
continue
56+
res += cost
57+
ufs.union(end, start)
58+
return res
59+
60+

0 commit comments

Comments
 (0)