-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |