File tree 3 files changed +62
-0
lines changed
1012.Complement of Base 10 Integer
1013.Pairs of Songs With Total Durations Divisible by 60
1014.Capacity To Ship Packages Within D Days
3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+
3
+ def bitwiseComplement (self , N : int ) -> int :
4
+ if N == 0 :
5
+ return 1
6
+ ans = 0
7
+ index = 0
8
+ while N > 1 :
9
+ if N % 2 == 0 :
10
+ ans += 2 ** index
11
+ index += 1
12
+ N = N // 2
13
+ return ans
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+
3
+ def numPairsDivisibleBy60 (self , time : List [int ]) -> int :
4
+ ans = 0
5
+ time = [i % 60 for i in time ]
6
+ # 整60s的歌曲,任取两个都满足
7
+ count = 0
8
+ while 0 in time :
9
+ count += 1
10
+ time .remove (0 )
11
+ ans += count * (count - 1 ) // 2
12
+
13
+ d = {}
14
+ for item in time :
15
+ d [item ] = d .get (item , 0 ) + 1
16
+ # 整30s的歌曲,任取两个也是满足的
17
+ ans += d .get (30 , 0 ) * (d .get (30 , 0 ) - 1 ) // 2
18
+
19
+ # 既不是60s也不是30s的歌曲,只能找时间上互补的歌曲
20
+ for i in range (1 , 30 ):
21
+ ans += d .get (i , 0 ) * d .get (60 - i , 0 )
22
+
23
+ return ans
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+
3
+ def shipWithinDays (self , weights : List [int ], D : int ) -> int :
4
+ ans = max (max (weights ), sum (weights ) // D )
5
+
6
+ # 判断某一个运载量能否支撑运载
7
+ def can_trans (ans , weights , D ):
8
+ # print('input : ',ans)
9
+ tmp = 0
10
+ while D >= 0 and weights :
11
+ w = weights .pop ()
12
+ if tmp + w <= ans :
13
+ tmp += w
14
+ else :
15
+ # print(D,tmp,weights+[w])
16
+ D -= 1
17
+ tmp = w
18
+ if D > 1 :
19
+ return True
20
+ elif D > 0 and tmp <= ans :
21
+ return True
22
+ return False
23
+
24
+ while not can_trans (ans , weights [:], D ):
25
+ ans += 1
26
+ return ans
You can’t perform that action at this time.
0 commit comments