Skip to content

Commit de71cdc

Browse files
committed
More solution of previous contests
1 parent f4e034a commit de71cdc

File tree

8 files changed

+288
-0
lines changed

8 files changed

+288
-0
lines changed

1200-1300q/1281.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Given an integer number n, return the difference between the product of its digits and the sum of its digits.
3+
4+
5+
Example 1:
6+
7+
Input: n = 234
8+
Output: 15
9+
Explanation:
10+
Product of digits = 2 * 3 * 4 = 24
11+
Sum of digits = 2 + 3 + 4 = 9
12+
Result = 24 - 9 = 15
13+
Example 2:
14+
15+
Input: n = 4421
16+
Output: 21
17+
Explanation:
18+
Product of digits = 4 * 4 * 2 * 1 = 32
19+
Sum of digits = 4 + 4 + 2 + 1 = 11
20+
Result = 32 - 11 = 21
21+
'''
22+
23+
class Solution(object):
24+
def subtractProductAndSum(self, n):
25+
"""
26+
:type n: int
27+
:rtype: int
28+
"""
29+
30+
from functools import reduce
31+
from operator import mul
32+
digits = [int(x) for x in str(n)]
33+
return reduce(mul, digits) - sum(digits)

1200-1300q/1282.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
There are n people whose IDs go from 0 to n - 1 and each person belongs exactly to one group. Given the array groupSizes of length n telling the group size each person belongs to, return the groups there are and the people's IDs each group includes.
3+
4+
You can return any solution in any order and the same applies for IDs. Also, it is guaranteed that there exists at least one solution.
5+
6+
7+
8+
Example 1:
9+
10+
Input: groupSizes = [3,3,3,3,3,1,3]
11+
Output: [[5],[0,1,2],[3,4,6]]
12+
Explanation:
13+
Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].
14+
Example 2:
15+
16+
Input: groupSizes = [2,1,3,3,3,2]
17+
Output: [[1],[0,5],[2,3,4]]
18+
'''
19+
class Solution(object):
20+
def groupThePeople(self, groupSizes):
21+
"""
22+
:type groupSizes: List[int]
23+
:rtype: List[List[int]]
24+
"""
25+
count = collections.defaultdict(list)
26+
for i, size in enumerate(groupSizes):
27+
count[size].append(i)
28+
result = []
29+
for s, value in count.items():
30+
for index in range(0, len(value), s):
31+
result.append(value[index:index + s])
32+
return result

1200-1300q/1283.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold.
3+
4+
Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5).
5+
6+
It is guaranteed that there will be an answer.
7+
8+
9+
10+
Example 1:
11+
12+
Input: nums = [1,2,5,9], threshold = 6
13+
Output: 5
14+
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1.
15+
If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2).
16+
Example 2:
17+
18+
Input: nums = [2,3,5,7,11], threshold = 11
19+
Output: 3
20+
Example 3:
21+
22+
Input: nums = [19], threshold = 5
23+
Output: 4
24+
'''
25+
class Solution(object):
26+
def smallestDivisor(self, nums, threshold):
27+
"""
28+
:type nums: List[int]
29+
:type threshold: int
30+
:rtype: int
31+
"""
32+
def getSum(divisor, xs):
33+
return sum([x // divisor + 1 if x % divisor else x // divisor for x in xs])
34+
35+
left, right = 1, 10 ** 6
36+
while left + 1 < right:
37+
mid = (left + right) // 2
38+
if getSum(mid, nums) > threshold:
39+
left = mid
40+
else:
41+
right = mid
42+
43+
return left if getSum(left, nums) <= threshold else right

1200-1300q/1290.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.
3+
4+
Return the decimal value of the number in the linked list.
5+
6+
7+
8+
Example 1:
9+
10+
11+
Input: head = [1,0,1]
12+
Output: 5
13+
Explanation: (101) in base 2 = (5) in base 10
14+
Example 2:
15+
16+
Input: head = [0]
17+
Output: 0
18+
Example 3:
19+
20+
Input: head = [1]
21+
Output: 1
22+
Example 4:
23+
24+
Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
25+
Output: 18880
26+
Example 5:
27+
28+
Input: head = [0,0]
29+
Output: 0
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 getDecimalValue(self, head):
40+
"""
41+
:type head: ListNode
42+
:rtype: int
43+
"""
44+
result = ''
45+
if not head:
46+
return 0
47+
while head:
48+
result+= str(head.val)
49+
head = head.next
50+
return int(result, 2)

1200-1300q/1291.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
3+
4+
Return a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.
5+
6+
7+
8+
Example 1:
9+
10+
Input: low = 100, high = 300
11+
Output: [123,234]
12+
Example 2:
13+
14+
Input: low = 1000, high = 13000
15+
Output: [1234,2345,3456,4567,5678,6789,12345]
16+
'''
17+
class Solution(object):
18+
def sequentialDigits(self, low, high):
19+
"""
20+
:type low: int
21+
:type high: int
22+
:rtype: List[int]
23+
"""
24+
result = []
25+
start = int(str(low)[0])
26+
for val in range(1, len(str(low))):
27+
new_val = start%10 + 1
28+
start = start*10 + new_val
29+
if start > high:
30+
return result
31+
32+
result.append(start)
33+
34+
while result[-1] <= high:
35+
temp = str(result[-1])
36+
next_elem = int(temp[-1]) + 1
37+
38+
if next_elem > 9:
39+
next_greater = 0
40+
for index in range(len(temp) + 1):
41+
next_greater = next_greater*10 + (index+1)
42+
else:
43+
next_greater = int(temp[1:]) * 10 + next_elem
44+
if next_greater <= high:
45+
result.append(next_greater)
46+
else:
47+
break
48+
# print next_greater
49+
final_result = []
50+
for val in result:
51+
if '0' not in str(val) and val >= low:
52+
final_result.append(val)
53+
return final_result

1200-1300q/1295.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
Given an array nums of integers, return how many of them contain an even number of digits.
3+
4+
5+
Example 1:
6+
7+
Input: nums = [12,345,2,6,7896]
8+
Output: 2
9+
Explanation:
10+
12 contains 2 digits (even number of digits).
11+
345 contains 3 digits (odd number of digits).
12+
2 contains 1 digit (odd number of digits).
13+
6 contains 1 digit (odd number of digits).
14+
7896 contains 4 digits (even number of digits).
15+
Therefore only 12 and 7896 contain an even number of digits.
16+
Example 2:
17+
18+
Input: nums = [555,901,482,1771]
19+
Output: 1
20+
Explanation:
21+
Only 1771 contains an even number of digits.
22+
'''
23+
class Solution(object):
24+
def findNumbers(self, nums):
25+
"""
26+
:type nums: List[int]
27+
:rtype: int
28+
"""
29+
return len([num for num in nums if len(str(num))%2 == 0])

1200-1300q/1296.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbers
3+
Return True if its possible otherwise return False.
4+
5+
6+
7+
Example 1:
8+
9+
Input: nums = [1,2,3,3,4,4,5,6], k = 4
10+
Output: true
11+
Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6].
12+
Example 2:
13+
14+
Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
15+
Output: true
16+
Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].
17+
Example 3:
18+
19+
Input: nums = [3,3,2,2,1,1], k = 3
20+
Output: true
21+
Example 4:
22+
23+
Input: nums = [1,2,3,4], k = 3
24+
Output: false
25+
Explanation: Each array should be divided in subarrays of size 3.
26+
'''
27+
class Solution(object):
28+
def isPossibleDivide(self, nums, k):
29+
"""
30+
:type nums: List[int]
31+
:type k: int
32+
:rtype: bool
33+
"""
34+
from collections import Counter
35+
count_map = Counter(nums)
36+
for num in sorted(count_map.keys()):
37+
if count_map[num] <= 0:
38+
continue
39+
for index in range(1, k):
40+
count_map[num+index] -= count_map[num]
41+
if count_map[num+index] < 0:
42+
return False
43+
return True

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1414
##### [Problems 1100-1200](./1200-1300q/)
1515
| # | Title | Solution | Difficulty |
1616
|---| ----- | -------- | ---------- |
17+
|1291|[Sequential Digits](https://leetcode.com/problems/sequential-digits/)|[Python](./1200-1300q/1291.py)|Medium|
18+
|1290|[Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/)|[Python](./1200-1300q/1290.py)|Easy|
19+
|1283|[Find the Smallest Divisor Given a Threshold](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/)|[Python](./1200-1300q/1283.py)|Medium|
20+
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Python](./1200-1300q/1282.py)|Medium|
21+
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Python](./1200-1300q/1281.py)|Easy|
1722
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Python](./1200-1300q/1277.py)|Medium|
1823
|1276|[Number of Burgers with No Waste of Ingredients](https://leetcode.com/problems/number-of-burgers-with-no-waste-of-ingredients/)|[Python](./1200-1300q/1276.py)|Medium|
1924
|1275|[Find Winner on a Tic Tac Toe Game](https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/)|[Python](./1200-1300q/1275.py)|Easy|

0 commit comments

Comments
 (0)