Skip to content

Commit e8c42e2

Browse files
committed
2020-06-27
1 parent ced41f0 commit e8c42e2

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution(object):
2+
def average(self, salary):
3+
"""
4+
:type salary: List[int]
5+
:rtype: float
6+
"""
7+
return (sum(salary) - max(salary) - min(salary)) * 1.0 / (len(salary) - 2)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def kthFactor(self, n, k):
3+
"""
4+
:type n: int
5+
:type k: int
6+
:rtype: int
7+
"""
8+
l = []
9+
for i in range(1, int(n ** 0.5) + 1):
10+
if n % i == 0:
11+
l.append(i)
12+
total = len(l) * 2 if l[-1] ** 2 != n else len(l) * 2 - 1
13+
if total < k:
14+
return -1
15+
16+
if k - 1 < len(l):
17+
return l[k - 1]
18+
else:
19+
idx = (len(l) - 1) * 2 - k
20+
return n // l[idx] if total % 2 else n // l[idx + 2]
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def longestSubarray(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if sum(nums) == len(nums):
8+
return len(nums) - 1
9+
zeroCnt = 0
10+
left = 0
11+
res = 0
12+
for right in range(len(nums)):
13+
if nums[right] == 0:
14+
zeroCnt += 1
15+
while zeroCnt > 1:
16+
if nums[left] == 0:
17+
zeroCnt -= 1
18+
left += 1
19+
res = max(res, right - left + 1 - zeroCnt)
20+
return res
21+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution(object):
2+
def minNumberOfSemesters(self, n, dependencies, k):
3+
"""
4+
:type n: int
5+
:type dependencies: List[List[int]]
6+
:type k: int
7+
:rtype: int
8+
"""
9+
from collections import defaultdict
10+
inDegree = defaultdict(int)
11+
outDegree = defaultdict(int)
12+
children = defaultdict(set)
13+
14+
for src, dec in dependencies: # 建图
15+
inDegree[dec] += 1
16+
outDegree[src] += 1
17+
children[src].add(dec)
18+
19+
queue = []
20+
for i in range(1, n + 1):
21+
if inDegree[i] == 0: # 入度为0(没有先修课了)的课入队
22+
heappush(queue, (-outDegree[i], i, -1)) # 出度越大(以这门课作为先修课的课越多),优先级越高
23+
24+
semesterCnt = 0
25+
while queue:
26+
semesterCnt += 1
27+
nextSemesterCourses = [] # 存放这个学期不能上的课
28+
courseCnt = 0
29+
while courseCnt < k and queue: # 每个学期最多上 k 门课
30+
priority, node, preFinishedSemester = heappop(queue)
31+
32+
if preFinishedSemester >= semesterCnt: # 当前学期不能上这门课
33+
nextSemesterCourses.append((priority, node, preFinishedSemester))
34+
continue
35+
36+
for child in children[node]: # 这门课可以学,学它,然后处理孩子课的入度
37+
inDegree[child] -= 1
38+
39+
if inDegree[child] == 0: # 孩子课的先修课全上完了
40+
heappush(queue, (-outDegree[child], child, semesterCnt))
41+
courseCnt += 1
42+
43+
for item in nextSemesterCourses: # 把之前存起来的本学期不能上的课再重新入队
44+
heappush(queue, item)
45+
46+
return semesterCnt
47+
48+
49+

0 commit comments

Comments
 (0)