Skip to content

Commit 9c8bbdc

Browse files
committedMay 26, 2019
2019-05-26
1 parent c94668f commit 9c8bbdc

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
 
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def minDistance(self, word1, word2):
3+
"""
4+
:type word1: str
5+
:type word2: str
6+
:rtype: int
7+
"""
8+
#用dp[i][j]表示word1[:i + 1], word2[:j + 1]这个问题的解
9+
m, n = len(word1), len(word2)
10+
dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
11+
12+
for i in range(m + 1):
13+
dp[i][0] = i
14+
15+
for i in range(n + 1):
16+
dp[0][i] = i
17+
18+
for i in range(1, m + 1):
19+
for j in range(1, n + 1):
20+
if word1[i - 1] == word2[j - 1]:
21+
dp[i][j] = dp[i - 1][j - 1]
22+
else:
23+
dp[i][j] = 1 + min(dp[i - 1][j], dp[i - 1][j - 1], dp[i][j - 1]) #分别对应插入,替换,删除
24+
25+
return dp[m][n]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def heightChecker(self, heights):
3+
"""
4+
:type heights: List[int]
5+
:rtype: int
6+
"""
7+
h = sorted(heights)
8+
# print h
9+
res = 0
10+
for i in range(len(heights)):
11+
if h[i] != heights[i]:
12+
res += 1
13+
return res
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution(object):
2+
def maxSatisfied(self, customers, grumpy, X):
3+
"""
4+
:type customers: List[int]
5+
:type grumpy: List[int]
6+
:type X: int
7+
:rtype: int
8+
"""
9+
record = [0 for _ in range(len(grumpy))] #代表在i满意的顾客总数
10+
s = 0
11+
for i in range(len(grumpy)):
12+
if grumpy[i] == 0:
13+
record[i] += record[i - 1] + customers[i]
14+
else:
15+
record[i] += record[i - 1]
16+
17+
print record
18+
tmp = record[-1]#不生气的时候已经可以满足的
19+
20+
prefix = [0 for _ in range(len(grumpy))]
21+
prefix[0] = customers[0]
22+
23+
for i in range(1, len(grumpy)):
24+
prefix[i] += prefix[i - 1] + customers[i]
25+
26+
lo, hi = 0, X - 1
27+
newcus = 0
28+
print prefix
29+
while(hi < len(grumpy)):
30+
if lo == 0:
31+
presum = prefix[hi] - 0 #上了BUFF之后的
32+
angsum = record[hi] - 0 #没上BUFF
33+
else:
34+
presum = prefix[hi] - prefix[lo - 1]
35+
angsum = record[hi] - record[lo - 1]
36+
37+
earn = presum - angsum
38+
print presum, angsum, earn, hi
39+
newcus = max(presum - angsum, newcus)
40+
hi += 1
41+
lo += 1
42+
return tmp + newcus
43+
44+
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def prevPermOpt1(self, A):
3+
"""
4+
:type A: List[int]
5+
:rtype: List[int]
6+
"""
7+
l = len(A)
8+
if sorted(A) == A:
9+
return A
10+
11+
for i in range(l - 1, -1, -1): #保证在这一段递减
12+
if A[i - 1] > A[i]: #找一个最大的数
13+
break
14+
#移动i - 1
15+
print A[i - 1]
16+
for j in range(l - 1, i - 1, -1): #找比A[i-1]小的最大的
17+
if A[j] < A[i - 1]:
18+
# j -= 1
19+
break
20+
print i, j
21+
A[i - 1],A[j] = A[j],A[i - 1]
22+
print A
23+
return A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution(object):
2+
def rearrangeBarcodes(self, barcodes):
3+
"""
4+
:type barcodes: List[int]
5+
:rtype: List[int]
6+
"""
7+
from collections import Counter
8+
import heapq
9+
10+
record = Counter(barcodes) #统计每个数字出现的频率
11+
12+
queue = []
13+
for key, val in record.items():
14+
queue.append([-val, key])
15+
16+
heapq.heapify(queue) #建立优先级队列
17+
18+
res = []
19+
pre = None
20+
while queue or pre:
21+
if queue:
22+
cur = heapq.heappop(queue) #取出当前出现次数最多的元素,如果次数相同则先进先出
23+
#frequency, value = cur[0], cur[1]
24+
res.append(cur[1]) #把它放到答案里
25+
cur[0] += 1 #给它的频率 - 1,因为Python支持最小堆,为了达到最大堆的效果,所以取了相反数操作
26+
if cur[0] == 0: #这个元素已经放好了
27+
cur = None
28+
else:
29+
cur = None
30+
if pre: #把前一个插入的数再进行入堆操作
31+
heapq.heappush(queue, pre)
32+
pre = cur #把这一轮的数用pre保存起来,此时这个数不在堆里,可以保障相邻元素不会重复
33+
34+
return res

0 commit comments

Comments
 (0)
Please sign in to comment.