Skip to content

Commit e2e4843

Browse files
committed
Fix the questions numbers issue and solution of new problems
1 parent 25bbcbe commit e2e4843

File tree

12 files changed

+351
-202
lines changed

12 files changed

+351
-202
lines changed

1000-1100q/1010.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
In a list of songs, the i-th song has a duration of time[i] seconds.
3+
4+
Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.
5+
6+
7+
8+
Example 1:
9+
10+
Input: [30,20,150,100,40]
11+
Output: 3
12+
Explanation: Three pairs have a total duration divisible by 60:
13+
(time[0] = 30, time[2] = 150): total duration 180
14+
(time[1] = 20, time[3] = 100): total duration 120
15+
(time[1] = 20, time[4] = 40): total duration 60
16+
Example 2:
17+
18+
Input: [60,60,60]
19+
Output: 3
20+
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
21+
22+
Note:
23+
24+
1 <= time.length <= 60000
25+
1 <= time[i] <= 500
26+
'''
27+
28+
class Solution(object):
29+
def numPairsDivisibleBy60(self, time):
30+
"""
31+
:type time: List[int]
32+
:rtype: int
33+
"""
34+
count_arr = [0]*60
35+
result = 0
36+
for t in time:
37+
remainder = t%60
38+
complement = (60-remainder)%60
39+
result += count_arr[complement]
40+
count_arr[remainder] += 1
41+
return result
42+
43+
'''
44+
Explanation:
45+
Q1: why create array of size 60?
46+
it is similar to the map which store the count. Why only 60 because 60 modulo of number cannot be more than 60
47+
Q2: why we need complement?
48+
to check the pair if it exisit with given value or not example: if remainder is 20 then we need to check if we have any number with remainder 40 or not.
49+
Q3: why 60 modulo complement?
50+
for handle cause when remainder is zero
51+
'''

1000-1100q/1011.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'''
2+
A conveyor belt has packages that must be shipped from one port to another within D days.
3+
4+
The i-th package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
5+
6+
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days.
7+
8+
9+
10+
Example 1:
11+
12+
Input: weights = [1,2,3,4,5,6,7,8,9,10], D = 5
13+
Output: 15
14+
Explanation:
15+
A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
16+
1st day: 1, 2, 3, 4, 5
17+
2nd day: 6, 7
18+
3rd day: 8
19+
4th day: 9
20+
5th day: 10
21+
22+
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
23+
Example 2:
24+
25+
Input: weights = [3,2,2,4,1,4], D = 3
26+
Output: 6
27+
Explanation:
28+
A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
29+
1st day: 3, 2
30+
2nd day: 2, 4
31+
3rd day: 1, 4
32+
33+
Note:
34+
35+
1 <= D <= weights.length <= 50000
36+
1 <= weights[i] <= 500
37+
38+
'''
39+
40+
41+
class Solution(object):
42+
def shipWithinDays(self, weights, D):
43+
"""
44+
:type weights: List[int]
45+
:type D: int
46+
:rtype: int
47+
"""
48+
high, low = sum(weights)+1, max(weights)
49+
50+
while(low < high):
51+
mid = (high+low)/2
52+
temp_left = mid
53+
packet_at_left = D-1
54+
for weight in weights:
55+
if weight <= mid:
56+
if temp_left < weight:
57+
if packet_at_left == 0:
58+
low = mid+1
59+
break
60+
packet_at_left -= 1
61+
temp_left = mid-weight
62+
else:
63+
temp_left -= weight
64+
else:
65+
high = mid
66+
67+
return low
68+
69+
70+
class Solution(object):
71+
def shipWithinDays(self, weights, D):
72+
"""
73+
:type weights: List[int]
74+
:type D: int
75+
:rtype: int
76+
"""
77+
left, right = max(weights), sum(weights)
78+
79+
while left < right:
80+
curr_sum, groups, invalid = 0, 0, True
81+
mid = left + ((right-left) >> 1)
82+
for weight in weights:
83+
if weight > mid:
84+
invalid = False
85+
break
86+
if curr_sum + weight > mid:
87+
groups += 1
88+
curr_sum = 0
89+
curr_sum += weight
90+
if invalid and groups < D:
91+
right = mid
92+
else:
93+
left = mid + 1
94+
return left

1000-1100q/1013.py

+34-34
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
'''
2-
In a list of songs, the i-th song has a duration of time[i] seconds.
2+
Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.
33
4-
Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.
4+
Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])
55
66
77
88
Example 1:
99
10-
Input: [30,20,150,100,40]
11-
Output: 3
12-
Explanation: Three pairs have a total duration divisible by 60:
13-
(time[0] = 30, time[2] = 150): total duration 180
14-
(time[1] = 20, time[3] = 100): total duration 120
15-
(time[1] = 20, time[4] = 40): total duration 60
10+
Input: [0,2,1,-6,6,-7,9,1,2,0,1]
11+
Output: true
12+
Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
1613
Example 2:
1714
18-
Input: [60,60,60]
19-
Output: 3
20-
Explanation: All three pairs have a total duration of 120, which is divisible by 60.
15+
Input: [0,2,1,-6,6,7,9,-1,2,0,1]
16+
Output: false
17+
Example 3:
18+
19+
Input: [3,3,6,5,-2,2,5,1,-9,4]
20+
Output: true
21+
Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
22+
2123
2224
Note:
2325
24-
1 <= time.length <= 60000
25-
1 <= time[i] <= 500
26+
3 <= A.length <= 50000
27+
-10000 <= A[i] <= 10000
2628
'''
2729

2830
class Solution(object):
29-
def numPairsDivisibleBy60(self, time):
31+
def canThreePartsEqualSum(self, A):
3032
"""
31-
:type time: List[int]
32-
:rtype: int
33+
:type A: List[int]
34+
:rtype: bool
3335
"""
34-
count_arr = [0]*60
35-
result = 0
36-
for t in time:
37-
remainder = t%60
38-
complement = (60-remainder)%60
39-
result += count_arr[complement]
40-
count_arr[remainder] += 1
41-
return result
42-
43-
'''
44-
Explanation:
45-
Q1: why create array of size 60?
46-
it is similar to the map which store the count. Why only 60 because 60 modulo of number cannot be more than 60
47-
Q2: why we need complement?
48-
to check the pair if it exisit with given value or not example: if remainder is 20 then we need to check if we have any number with remainder 40 or not.
49-
Q3: why 60 modulo complement?
50-
for handle cause when remainder is zero
51-
'''
36+
total_sum = 0
37+
for val in A:
38+
total_sum += val
39+
40+
if(total_sum%3 != 0):
41+
return False
42+
43+
curr_sum, groups = 0, 0
44+
for val in A:
45+
curr_sum += val
46+
if curr_sum == total_sum/3:
47+
curr_sum = 0
48+
groups +=1
49+
print groups
50+
return groups == 3
51+

1000-1100q/1014.py

+17-78
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,33 @@
11
'''
2-
A conveyor belt has packages that must be shipped from one port to another within D days.
2+
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.
33
4-
The i-th package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
4+
The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.
55
6-
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days.
6+
Return the maximum score of a pair of sightseeing spots.
77
88
99
1010
Example 1:
1111
12-
Input: weights = [1,2,3,4,5,6,7,8,9,10], D = 5
13-
Output: 15
14-
Explanation:
15-
A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
16-
1st day: 1, 2, 3, 4, 5
17-
2nd day: 6, 7
18-
3rd day: 8
19-
4th day: 9
20-
5th day: 10
21-
22-
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
23-
Example 2:
24-
25-
Input: weights = [3,2,2,4,1,4], D = 3
26-
Output: 6
27-
Explanation:
28-
A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
29-
1st day: 3, 2
30-
2nd day: 2, 4
31-
3rd day: 1, 4
12+
Input: [8,1,5,2,6]
13+
Output: 11
14+
Explanation: i = 0, j = 2, A[i] + A[j] + i - j = 8 + 5 + 0 - 2 = 11
15+
3216
3317
Note:
3418
35-
1 <= D <= weights.length <= 50000
36-
1 <= weights[i] <= 500
37-
19+
2 <= A.length <= 50000
20+
1 <= A[i] <= 1000
3821
'''
39-
40-
41-
class Solution(object):
42-
def shipWithinDays(self, weights, D):
43-
"""
44-
:type weights: List[int]
45-
:type D: int
46-
:rtype: int
47-
"""
48-
high, low = sum(weights)+1, max(weights)
49-
50-
while(low < high):
51-
mid = (high+low)/2
52-
temp_left = mid
53-
packet_at_left = D-1
54-
for weight in weights:
55-
if weight <= mid:
56-
if temp_left < weight:
57-
if packet_at_left == 0:
58-
low = mid+1
59-
break
60-
packet_at_left -= 1
61-
temp_left = mid-weight
62-
else:
63-
temp_left -= weight
64-
else:
65-
high = mid
66-
67-
return low
68-
69-
7022
class Solution(object):
71-
def shipWithinDays(self, weights, D):
23+
def maxScoreSightseeingPair(self, A):
7224
"""
73-
:type weights: List[int]
74-
:type D: int
25+
:type A: List[int]
7526
:rtype: int
7627
"""
77-
left, right = max(weights), sum(weights)
78-
79-
while left < right:
80-
curr_sum, groups, invalid = 0, 0, True
81-
mid = left + ((right-left) >> 1)
82-
for weight in weights:
83-
if weight > mid:
84-
invalid = False
85-
break
86-
if curr_sum + weight > mid:
87-
groups += 1
88-
curr_sum = 0
89-
curr_sum += weight
90-
if invalid and groups < D:
91-
right = mid
92-
else:
93-
left = mid + 1
94-
return left
28+
prev_best, result = 0, 0
29+
for index in range(0, len(A)):
30+
result = max(result, A[index]-index+prev_best)
31+
prev_best = max(prev_best, A[index]+index)
32+
return result
33+
File renamed without changes.
File renamed without changes.

1000-1100q/1017.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'''
2+
Given a number N, return a string consisting of "0"s and "1"s that represents its value in base -2 (negative two).
3+
4+
The returned string must have no leading zeroes, unless the string is "0".
5+
6+
7+
Example 1:
8+
9+
Input: 2
10+
Output: "110"
11+
Explantion: (-2) ^ 2 + (-2) ^ 1 = 2
12+
Example 2:
13+
14+
Input: 3
15+
Output: "111"
16+
Explantion: (-2) ^ 2 + (-2) ^ 1 + (-2) ^ 0 = 3
17+
18+
Example 3:
19+
20+
Input: 4
21+
Output: "100"
22+
Explantion: (-2) ^ 2 = 4
23+
24+
25+
Note:
26+
27+
0 <= N <= 10^9
28+
'''
29+
30+
class Solution(object):
31+
def baseNeg2(self, N):
32+
"""
33+
:type N: int
34+
:rtype: str
35+
"""
36+
if N == 0:
37+
digits = ['0']
38+
else:
39+
digits = []
40+
while N != 0:
41+
N, remainder = divmod(N, -2)
42+
if remainder < 0:
43+
N, remainder = N+1, remainder + 2
44+
digits.append(str(remainder))
45+
return ''.join(digits[::-1])

0 commit comments

Comments
 (0)