Skip to content

Commit e2e4843

Browse files
committedApr 2, 2019
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])

‎1000-1100q/1018.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-bit to least-significant-bit.)
3+
4+
Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.
5+
6+
Example 1:
7+
8+
Input: [0,1,1]
9+
Output: [true,false,false]
10+
Explanation:
11+
The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.
12+
Example 2:
13+
14+
Input: [1,1,1]
15+
Output: [false,false,false]
16+
17+
Note:
18+
19+
1 <= A.length <= 30000
20+
A[i] is 0 or 1
21+
'''
22+
23+
class Solution(object):
24+
def prefixesDivBy5(self, A):
25+
"""
26+
:type A: List[int]
27+
:rtype: List[bool]
28+
"""
29+
result = []
30+
if not A:
31+
return []
32+
str_bin = ''
33+
for val in A:
34+
str_bin += str(val)
35+
if(int(str_bin, 2)%5 == 0):
36+
result.append(True)
37+
else:
38+
result.append(False)
39+
return result

‎1000-1100q/1019.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'''
2+
We are given a linked list with head as the first node. Let's number the nodes in the list: node_1, node_2, node_3, ... etc.
3+
4+
Each node may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, and j is the smallest possible choice. If such a j does not exist, the next larger value is 0.
5+
6+
Return an array of integers answer, where answer[i] = next_larger(node_{i+1}).
7+
8+
Note that in the example inputs (not outputs) below, arrays such as [2,1,5] represent the serialization of a linked list with a head node value of 2, second node value of 1, and third node value of 5.
9+
10+
11+
12+
Example 1:
13+
14+
Input: [2,1,5]
15+
Output: [5,5,0]
16+
Example 2:
17+
18+
Input: [2,7,4,3,5]
19+
Output: [7,0,5,5,0]
20+
Example 3:
21+
22+
Input: [1,7,5,1,9,2,5,1]
23+
Output: [7,9,9,9,0,5,0,0]
24+
25+
26+
Note:
27+
28+
1 <= node.val <= 10^9 for each node in the linked list.
29+
The given list has length in the range [0, 10000].
30+
'''
31+
32+
# Definition for singly-linked list.
33+
# class ListNode(object):
34+
# def __init__(self, x):
35+
# self.val = x
36+
# self.next = None
37+
38+
class Solution(object):
39+
def nextLargerNodes(self, head):
40+
"""
41+
:type head: ListNode
42+
:rtype: List[int]
43+
"""
44+
result = []
45+
while head:
46+
result.append(head.val)
47+
head = head.next
48+
49+
stack = [result[-1]]
50+
ans = [0]
51+
for val in range(len(result)-2, -1, -1):
52+
if result[val] < stack[-1]:
53+
ans.append(stack[-1])
54+
else:
55+
while stack and stack[-1] <= result[val]:
56+
stack.pop()
57+
if stack:
58+
ans.append(stack[-1])
59+
else:
60+
ans.append(0)
61+
stack.append(result[val])
62+
return ans[::-1]

‎1000-1100q/1020.py

-51
This file was deleted.

‎1000-1100q/1021.py

-33
This file was deleted.

‎README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
77
##### [Problems 1000-1100](./1000-1100q/)
88
| # | Title | Solution | Difficulty |
99
|---| ----- | -------- | ---------- |
10-
|1023|[Binary String With Substrings Representing 1 To N](https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n)|[Python](./1000-1100q/1023.py)|Medium|
11-
|1022|[Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k)|[Python](./1000-1100q/1022.py)|Medium|
12-
|1021|[Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair)|[Python](./1000-1100q/1021.py)|Medium|
13-
|1020|[Partition Array Into Three Parts With Equal Sum ](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum) | [Python](./1000-1100q/1020.py)|Easy|
14-
|1014|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/) | [Python](./1000-1100q/1014.py) | Medium|
15-
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Python](./1000-1100q/1013.py)|Easy|
10+
|1019|[Next Greater Node In Linked List](https://leetcode.com/problems/next-greater-node-in-linked-list)|[Python](./1000-1100q/1019.py)|Medium|
11+
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5)|[Python](./1000-1100q/1018.py)|Easy|
12+
|1017|[Convert to Base -2](https://leetcode.com/problems/convert-to-base-2)|[Python](./1000-1100q/1017.py)|Medium|
13+
|1016|[Binary String With Substrings Representing 1 To N](https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n)|[Python](./1000-1100q/1016.py)|Medium|
14+
|1015|[Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k)|[Python](./1000-1100q/1015.py)|Medium|
15+
|1014|[Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair)|[Python](./1000-1100q/1014.py)|Medium|
16+
|1013|[Partition Array Into Three Parts With Equal Sum ](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum) | [Python](./1000-1100q/1013.py)|Easy|
17+
|1011|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/) | [Python](./1000-1100q/1011.py) | Medium|
18+
|1010|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Python](./1000-1100q/1010.py)|Easy|
1619
|1012|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/) | [Python](./1000-1100q/1012.py)|Easy|
1720
|1008|[Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal)|[Python](./1000-1100q/1008.py)|Medium|
1821
|1007|[Minimum Domino Rotations For Equal Row](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row)|[Python](./1000-1100q/1007.py)|Medium|

0 commit comments

Comments
 (0)
Please sign in to comment.