Skip to content

Commit

Permalink
2020-02-28
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Feb 29, 2020
1 parent 86e9e7f commit 202badb
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def countNegatives(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if not grid or not grid[0]:
return 0
m, n = len(grid), len(grid[0])

res = 0
for i in range(m):
for j in range(n):
if grid[i][j] < 0:
res += 1
return res
23 changes: 23 additions & 0 deletions 1352.最后K个数的乘积/1352-最后K个数的乘积.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class ProductOfNumbers(object):

def __init__(self):
self.prefix = [1]

def add(self, num):
"""
:type num: int
:rtype: None
"""
if num:
self.prefix.append(self.prefix[-1] * num)
else:
self.prefix = [1]

def getProduct(self, k):
"""
:type k: int
:rtype: int
"""
if k >= len(self.prefix):
return 0
return self.prefix[-1] / self.prefix[-k - 1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution(object):
def maxEvents(self, events):
"""
:type events: List[List[int]]
:rtype: int
"""
from heapq import *
if not events:
return 0
# 排序并反转,反转是为了可以快速pop
events = sorted(events, key = lambda x:(x[0], x[1]))[::-1]

queue = []
res = 0
for day in range(1, 10 ** 5 + 1):
# 把所有结束日期在当前日期之前的event都pop掉
while queue and queue[0] < day:
heappop(queue)

# 把所有开始日期大于等于当前日期的event都push进队列
while events and events[-1][0] <= day:
last = events.pop()
heappush(queue, last[1])

if queue:
# 如果当前日期有可以去的event,就去这一个
heappop(queue)
res += 1
if not queue and not events:
# 如果所有event都参加完了
break

return res
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution(object):
def isPossible(self, target):
"""
:type target: List[int]
:rtype: bool
"""
from heapq import *
if len(target) == 1:
return target[0] == 1

s = sum(target)
target = [-item for item in target]
heapify(target)

while s > len(target):
# 找当前最大的数和第二大的数
m = -heappop(target)
s_m = -target[0]

# 更新 m 并更新 s
diff = s - m
if not diff:
break
new_m = m - (max(1, (m - s_m) / diff) * diff)
s = s - m + new_m

heappush(target, -new_m)

return not any([num != -1 for num in target])

0 comments on commit 202badb

Please sign in to comment.