Skip to content

Commit 567eb45

Browse files
committed
Added Solution for 1000-1100q/1072 1000-1100q/1073 1000-1100q/1074 1000-1100q/1078 1000-1100q/1079 1000-1100q/1080 1000-1100q/1081 1000-1100q/1085 1000-1100q/1086 1000-1100q/1087 1000-1100q/1088 1000-1100q/1089
1 parent b0136eb commit 567eb45

File tree

12 files changed

+537
-0
lines changed

12 files changed

+537
-0
lines changed

1000-1100q/1072.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Given a matrix consisting of 0s and 1s, we may choose any number of columns in the matrix and flip every cell in that column. Flipping a cell changes the value of that cell from 0 to 1 or from 1 to 0.
3+
4+
Return the maximum number of rows that have all values equal after some number of flips.
5+
6+
7+
8+
Example 1:
9+
10+
Input: [[0,1],[1,1]]
11+
Output: 1
12+
Explanation: After flipping no values, 1 row has all values equal.
13+
Example 2:
14+
15+
Input: [[0,1],[1,0]]
16+
Output: 2
17+
Explanation: After flipping values in the first column, both rows have equal values.
18+
Example 3:
19+
20+
Input: [[0,0,0],[0,0,1],[1,1,0]]
21+
Output: 2
22+
Explanation: After flipping values in the first two columns, the last two rows have equal values.
23+
24+
25+
Note:
26+
27+
1 <= matrix.length <= 300
28+
1 <= matrix[i].length <= 300
29+
All matrix[i].length's are equal
30+
matrix[i][j] is 0 or 1
31+
'''

1000-1100q/1073.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
Given two numbers arr1 and arr2 in base -2, return the result of adding them together.
3+
4+
Each number is given in array format: as an array of 0s and 1s, from most significant bit to least significant bit. For example, arr = [1,1,0,1] represents the number (-2)^3 + (-2)^2 + (-2)^0 = -3. A number arr in array format is also guaranteed to have no leading zeros: either arr == [0] or arr[0] == 1.
5+
6+
Return the result of adding arr1 and arr2 in the same format: as an array of 0s and 1s with no leading zeros.
7+
8+
9+
10+
Example 1:
11+
12+
Input: arr1 = [1,1,1,1,1], arr2 = [1,0,1]
13+
Output: [1,0,0,0,0]
14+
Explanation: arr1 represents 11, arr2 represents 5, the output represents 16.
15+
16+
17+
Note:
18+
19+
1 <= arr1.length <= 1000
20+
1 <= arr2.length <= 1000
21+
arr1 and arr2 have no leading zeros
22+
arr1[i] is 0 or 1
23+
arr2[i] is 0 or 1
24+
'''

1000-1100q/1074.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Given a matrix, and a target, return the number of non-empty submatrices that sum to target.
3+
4+
A submatrix x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y <= y2.
5+
6+
Two submatrices (x1, y1, x2, y2) and (x1', y1', x2', y2') are different if they have some coordinate that is different: for example, if x1 != x1'.
7+
8+
9+
10+
Example 1:
11+
12+
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
13+
Output: 4
14+
Explanation: The four 1x1 submatrices that only contain 0.
15+
Example 2:
16+
17+
Input: matrix = [[1,-1],[-1,1]], target = 0
18+
Output: 5
19+
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
20+
21+
22+
Note:
23+
24+
1 <= matrix.length <= 300
25+
1 <= matrix[0].length <= 300
26+
-1000 <= matrix[i] <= 1000
27+
-10^8 <= target <= 10^8
28+
'''

1000-1100q/1078.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second.
3+
4+
For each such occurrence, add "third" to the answer, and return the answer.
5+
6+
7+
8+
Example 1:
9+
10+
Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
11+
Output: ["girl","student"]
12+
Example 2:
13+
14+
Input: text = "we will we will rock you", first = "we", second = "will"
15+
Output: ["we","rock"]
16+
17+
18+
Note:
19+
20+
1 <= text.length <= 1000
21+
text consists of space separated words, where each word consists of lowercase English letters.
22+
1 <= first.length, second.length <= 10
23+
first and second consist of lowercase English letters.
24+
'''
25+
26+
class Solution(object):
27+
def findOcurrences(self, text, first, second):
28+
"""
29+
:type text: str
30+
:type first: str
31+
:type second: str
32+
:rtype: List[str]
33+
"""
34+
result = []
35+
if not text:
36+
return []
37+
splitted_text = text.split(' ')
38+
indi = 0
39+
for index in range(len(splitted_text)-1):
40+
if splitted_text[index] == first and splitted_text[index+1] == second:
41+
index = index+2
42+
if index < len(splitted_text):
43+
result.append(splitted_text[index])
44+
return result

1000-1100q/1079.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
You have a set of tiles, where each tile has one letter tiles[i] printed on it. Return the number of possible non-empty sequences of letters you can make.
3+
4+
5+
6+
Example 1:
7+
8+
Input: "AAB"
9+
Output: 8
10+
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
11+
Example 2:
12+
13+
Input: "AAABBC"
14+
Output: 188
15+
'''
16+
17+
class Solution(object):
18+
def numTilePossibilities(self, tiles):
19+
"""
20+
:type tiles: str
21+
:rtype: int
22+
"""
23+
24+
if not tiles:
25+
return 0
26+
27+
import collections
28+
unique = set(tiles)
29+
freq_map = collections.Counter(tiles)
30+
total_len = 1
31+
while total_len < len(tiles):
32+
new = set()
33+
for char in tiles:
34+
for comb in unique:
35+
new_seq = comb+char
36+
up_freq = collections.Counter(new_seq)
37+
flag =True
38+
for key, val in up_freq.items():
39+
if val > freq_map[key]:
40+
flag = False
41+
if flag:
42+
new.add(new_seq)
43+
# print new
44+
unique.update(new)
45+
46+
total_len += 1
47+
return len(unique)

1000-1100q/1080.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'''
2+
Given the root of a binary tree, consider all root to leaf paths: paths from the root to any leaf. (A leaf is a node with no children.)
3+
4+
A node is insufficient if every such root to leaf path intersecting this node has sum strictly less than limit.
5+
6+
Delete all insufficient nodes simultaneously, and return the root of the resulting binary tree.
7+
8+
9+
10+
Example 1:
11+
12+
13+
Input: root = [1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14], limit = 1
14+
15+
Output: [1,2,3,4,null,null,7,8,9,null,14]
16+
Example 2:
17+
18+
19+
Input: root = [5,4,8,11,null,17,4,7,1,null,null,5,3], limit = 22
20+
21+
Output: [5,4,8,11,null,17,4,7,null,null,null,5]
22+
23+
24+
Example 3:
25+
26+
27+
Input: root = [1,2,-3,-5,null,4,null], limit = -1
28+
29+
Output: [1,null,-3,4]
30+
31+
32+
Note:
33+
34+
The given tree will have between 1 and 5000 nodes.
35+
-10^5 <= node.val <= 10^5
36+
-10^9 <= limit <= 10^9
37+
'''
38+
39+
# Definition for a binary tree node.
40+
# class TreeNode(object):
41+
# def __init__(self, x):
42+
# self.val = x
43+
# self.left = None
44+
# self.right = None
45+
46+
class Solution(object):
47+
def sufficientSubset(self, root, limit):
48+
"""
49+
:type root: TreeNode
50+
:type limit: int
51+
:rtype: TreeNode
52+
"""
53+
def reduce_tree(root, limit, curr_sum):
54+
if not root:
55+
return None
56+
57+
l_sum = [curr_sum[0] + root.val]
58+
r_sum = [l_sum[0]]
59+
60+
root.left = reduce_tree(root.left, limit, l_sum)
61+
root.right = reduce_tree(root.right, limit, r_sum)
62+
63+
curr_sum[0] = max(l_sum[0], r_sum[0])
64+
if curr_sum[0] < limit:
65+
root = None
66+
return root
67+
curr_sum = [0]
68+
return reduce_tree(root, limit, curr_sum)

1000-1100q/1081.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
Return the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once.
3+
4+
5+
Example 1:
6+
7+
Input: "cdadabcc"
8+
Output: "adbc"
9+
Example 2:
10+
11+
Input: "abcd"
12+
Output: "abcd"
13+
Example 3:
14+
15+
Input: "ecbacba"
16+
Output: "eacb"
17+
Example 4:
18+
19+
Input: "leetcode"
20+
Output: "letcod"
21+
22+
23+
Note:
24+
25+
1 <= text.length <= 1000
26+
text consists of lowercase English letters.
27+
'''
28+
class Solution(object):
29+
def smallestSubsequence(self, text):
30+
"""
31+
:type text: str
32+
:rtype: str
33+
"""
34+
if not text:
35+
return ''
36+
import collections
37+
freq_map = collections.Counter(text)
38+
used = [False]*26
39+
result = ''
40+
41+
for char in text:
42+
freq_map[char] -= 1
43+
if used[ord(char)-97]:
44+
continue
45+
while (result and result[-1] > char and freq_map[result[-1]] > 0):
46+
used[ord(result[-1])-97] = False
47+
result = result[:-1]
48+
49+
used[ord(char)-97] = True
50+
result += char
51+
return result

1000-1100q/1085.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Given an array A of positive integers, let S be the sum of the digits of the minimal element of A.
3+
4+
Return 0 if S is odd, otherwise return 1.
5+
6+
7+
8+
Example 1:
9+
10+
Input: [34,23,1,24,75,33,54,8]
11+
Output: 0
12+
Explanation:
13+
The minimal element is 1, and the sum of those digits is S = 1 which is odd, so the answer is 0.
14+
Example 2:
15+
16+
Input: [99,77,33,66,55]
17+
Output: 1
18+
Explanation:
19+
The minimal element is 33, and the sum of those digits is S = 3 + 3 = 6 which is even, so the answer is 1.
20+
21+
22+
Note:
23+
24+
1 <= A.length <= 100
25+
1 <= A[i].length <= 100
26+
'''
27+
class Solution(object):
28+
def sumOfDigits(self, A):
29+
"""
30+
:type A: List[int]
31+
:rtype: int
32+
"""
33+
if not A:
34+
return 0
35+
36+
mini = min(A)
37+
result = 0
38+
while mini > 0:
39+
quo = mini%10
40+
rem = mini/10
41+
result += quo
42+
mini = rem
43+
44+
return 0 if result%2 else 1

0 commit comments

Comments
 (0)