Skip to content

Commit

Permalink
bow
Browse files Browse the repository at this point in the history
  • Loading branch information
WenDesi committed Jul 18, 2016
1 parent 214bfdb commit ea4a376
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 299.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution(object):
def getHint(self, secret, guess):
"""
:type secret: str
:type guess: str
:rtype: str
"""

secret_dict = {}
guess_dict = {}

for s in secret:
secret_dict[s] = 0
guess_dict[s] = 0

for s in guess:
secret_dict[s] = 0
guess_dict[s] = 0


A,B = 0,0
for i in range(len(guess)):
if secret[i] == guess[i]:
A += 1
else:
secret_dict[secret[i]] += 1
guess_dict[guess[i]] += 1


for ele in guess_dict:
B += min(guess_dict[ele],secret_dict[ele])

result = '%dA%dB' % (A,B)
return result

if __name__ == '__main__':
wds = Solution()
print wds.getHint("1","2")


91 changes: 91 additions & 0 deletions 373.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
class mHeap(object):
heap = []

def __init__(self,MIN_HEAP = True):
self.heap = []
self.MIN_HEAP = MIN_HEAP

def cal(self,i,j):
if self.MIN_HEAP:
return i[0] < j[0]
return i[0] > j[0]

def cal_equal(self,i,j):
if self.MIN_HEAP:
return i[0] <= j[0]
return i[0] >= j[0]

def top(self):
return self.heap[0]

def push(self,element):
i = len(self.heap)
self.heap.append(element)

j = (i-1)/2
tmp = element
while( j >= 0 and i != 0 ):
if self.cal(self.heap[j],tmp):
break
self.heap[i] = self.heap[j]
i =j
j = ( i - 1 ) / 2
self.heap[i] = tmp

def pop(self):
if len(self.heap) == 0:
return

self.heap[0] = self.heap[-1]
self.heap.pop()
if len(self.heap) == 0:
return
self.fix_down()

def fix_down(self):
i = 0
j = 2 * i + 1
n = len(self.heap)

tmp = self.heap[i]

while j < n:
if j + 1 < n and self.cal(self.heap[j+1],self.heap[j]):
j += 1
if self.cal_equal(tmp,self.heap[j]):
break

self.heap[i] = self.heap[j]
i = j
j = 2 * i + 1

self.heap[i] = tmp

class Solution(object):
def kSmallestPairs(self, nums1, nums2, k):
"""
:type nums1: List[int]
:type nums2: List[int]
:type k: int
:rtype: List[List[int]]
"""
heap = mHeap(False)

for i in nums1:
for j in nums2:
if len(heap.heap) < k:
heap.push([i+j,[i,j]])
else:
top = heap.top()
if i+j < top[0]:
heap.pop()
heap.push([i+j,[i,j]])

result = []
for ele in heap.heap:
result.append(ele[1])
return result

if __name__ == '__main__':
wds = Solution()
print wds.kSmallestPairs([1,1,2],[1,2,3],2)

0 comments on commit ea4a376

Please sign in to comment.