Skip to content

Commit 116ba7a

Browse files
committed
Adding solution of latest contest
1 parent 37c146b commit 116ba7a

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

1000-1100q/1020.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
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.
3+
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])
5+
6+
7+
8+
Example 1:
9+
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
13+
Example 2:
14+
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+
23+
24+
Note:
25+
26+
3 <= A.length <= 50000
27+
-10000 <= A[i] <= 10000
28+
'''
29+
30+
class Solution(object):
31+
def canThreePartsEqualSum(self, A):
32+
"""
33+
:type A: List[int]
34+
:rtype: bool
35+
"""
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/1021.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
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.
3+
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.
5+
6+
Return the maximum score of a pair of sightseeing spots.
7+
8+
9+
10+
Example 1:
11+
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+
16+
17+
Note:
18+
19+
2 <= A.length <= 50000
20+
1 <= A[i] <= 1000
21+
'''
22+
class Solution(object):
23+
def maxScoreSightseeingPair(self, A):
24+
"""
25+
:type A: List[int]
26+
:rtype: int
27+
"""
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+

1000-1100q/1022.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
Given a positive integer K, you need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1.
3+
4+
Return the length of N. If there is no such N, return -1.
5+
6+
7+
8+
Example 1:
9+
10+
Input: 1
11+
Output: 1
12+
Explanation: The smallest answer is N = 1, which has length 1.
13+
Example 2:
14+
15+
Input: 2
16+
Output: -1
17+
Explanation: There is no such positive integer N divisible by 2.
18+
Example 3:
19+
20+
Input: 3
21+
Output: 3
22+
Explanation: The smallest answer is N = 111, which has length 3.
23+
'''
24+
25+
class Solution(object):
26+
def smallestRepunitDivByK(self, K):
27+
"""
28+
:type K: int
29+
:rtype: int
30+
"""
31+
length, value = 0, 0
32+
for no_one in range(100000):
33+
value = (10*value + 1)%K
34+
length += 1
35+
if value == 0:
36+
return length
37+
return -1

1000-1100q/1023.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.
3+
4+
5+
6+
Example 1:
7+
8+
Input: S = "0110", N = 3
9+
Output: true
10+
Example 2:
11+
12+
Input: S = "0110", N = 4
13+
Output: false
14+
15+
16+
Note:
17+
18+
1 <= S.length <= 1000
19+
1 <= N <= 10^9
20+
'''
21+
22+
class Solution(object):
23+
def queryString(self, S, N):
24+
"""
25+
:type S: str
26+
:type N: int
27+
:rtype: bool
28+
"""
29+
for num in range(1, N+1):
30+
binary_str = ''
31+
while (num != 0):
32+
binary_str += str(num%2)
33+
num /= 2
34+
reversed_str = binary_str[::-1]
35+
36+
if reversed_str not in S:
37+
return False
38+
return True
39+

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ 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|
1014
|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|
1115
|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|
1216
|1012|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/) | [Python](./1000-1100q/1012.py)|Easy|

0 commit comments

Comments
 (0)