forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Solution for 1000-1100q/1072 1000-1100q/1073 1000-1100q/1074 10…
…00-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
- Loading branch information
Showing
12 changed files
with
537 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
''' | ||
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. | ||
Return the maximum number of rows that have all values equal after some number of flips. | ||
Example 1: | ||
Input: [[0,1],[1,1]] | ||
Output: 1 | ||
Explanation: After flipping no values, 1 row has all values equal. | ||
Example 2: | ||
Input: [[0,1],[1,0]] | ||
Output: 2 | ||
Explanation: After flipping values in the first column, both rows have equal values. | ||
Example 3: | ||
Input: [[0,0,0],[0,0,1],[1,1,0]] | ||
Output: 2 | ||
Explanation: After flipping values in the first two columns, the last two rows have equal values. | ||
Note: | ||
1 <= matrix.length <= 300 | ||
1 <= matrix[i].length <= 300 | ||
All matrix[i].length's are equal | ||
matrix[i][j] is 0 or 1 | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
''' | ||
Given two numbers arr1 and arr2 in base -2, return the result of adding them together. | ||
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. | ||
Return the result of adding arr1 and arr2 in the same format: as an array of 0s and 1s with no leading zeros. | ||
Example 1: | ||
Input: arr1 = [1,1,1,1,1], arr2 = [1,0,1] | ||
Output: [1,0,0,0,0] | ||
Explanation: arr1 represents 11, arr2 represents 5, the output represents 16. | ||
Note: | ||
1 <= arr1.length <= 1000 | ||
1 <= arr2.length <= 1000 | ||
arr1 and arr2 have no leading zeros | ||
arr1[i] is 0 or 1 | ||
arr2[i] is 0 or 1 | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
''' | ||
Given a matrix, and a target, return the number of non-empty submatrices that sum to target. | ||
A submatrix x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= y <= y2. | ||
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'. | ||
Example 1: | ||
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0 | ||
Output: 4 | ||
Explanation: The four 1x1 submatrices that only contain 0. | ||
Example 2: | ||
Input: matrix = [[1,-1],[-1,1]], target = 0 | ||
Output: 5 | ||
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix. | ||
Note: | ||
1 <= matrix.length <= 300 | ||
1 <= matrix[0].length <= 300 | ||
-1000 <= matrix[i] <= 1000 | ||
-10^8 <= target <= 10^8 | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
''' | ||
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. | ||
For each such occurrence, add "third" to the answer, and return the answer. | ||
Example 1: | ||
Input: text = "alice is a good girl she is a good student", first = "a", second = "good" | ||
Output: ["girl","student"] | ||
Example 2: | ||
Input: text = "we will we will rock you", first = "we", second = "will" | ||
Output: ["we","rock"] | ||
Note: | ||
1 <= text.length <= 1000 | ||
text consists of space separated words, where each word consists of lowercase English letters. | ||
1 <= first.length, second.length <= 10 | ||
first and second consist of lowercase English letters. | ||
''' | ||
|
||
class Solution(object): | ||
def findOcurrences(self, text, first, second): | ||
""" | ||
:type text: str | ||
:type first: str | ||
:type second: str | ||
:rtype: List[str] | ||
""" | ||
result = [] | ||
if not text: | ||
return [] | ||
splitted_text = text.split(' ') | ||
indi = 0 | ||
for index in range(len(splitted_text)-1): | ||
if splitted_text[index] == first and splitted_text[index+1] == second: | ||
index = index+2 | ||
if index < len(splitted_text): | ||
result.append(splitted_text[index]) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
''' | ||
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. | ||
Example 1: | ||
Input: "AAB" | ||
Output: 8 | ||
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA". | ||
Example 2: | ||
Input: "AAABBC" | ||
Output: 188 | ||
''' | ||
|
||
class Solution(object): | ||
def numTilePossibilities(self, tiles): | ||
""" | ||
:type tiles: str | ||
:rtype: int | ||
""" | ||
|
||
if not tiles: | ||
return 0 | ||
|
||
import collections | ||
unique = set(tiles) | ||
freq_map = collections.Counter(tiles) | ||
total_len = 1 | ||
while total_len < len(tiles): | ||
new = set() | ||
for char in tiles: | ||
for comb in unique: | ||
new_seq = comb+char | ||
up_freq = collections.Counter(new_seq) | ||
flag =True | ||
for key, val in up_freq.items(): | ||
if val > freq_map[key]: | ||
flag = False | ||
if flag: | ||
new.add(new_seq) | ||
# print new | ||
unique.update(new) | ||
|
||
total_len += 1 | ||
return len(unique) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
''' | ||
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.) | ||
A node is insufficient if every such root to leaf path intersecting this node has sum strictly less than limit. | ||
Delete all insufficient nodes simultaneously, and return the root of the resulting binary tree. | ||
Example 1: | ||
Input: root = [1,2,3,4,-99,-99,7,8,9,-99,-99,12,13,-99,14], limit = 1 | ||
Output: [1,2,3,4,null,null,7,8,9,null,14] | ||
Example 2: | ||
Input: root = [5,4,8,11,null,17,4,7,1,null,null,5,3], limit = 22 | ||
Output: [5,4,8,11,null,17,4,7,null,null,null,5] | ||
Example 3: | ||
Input: root = [1,2,-3,-5,null,4,null], limit = -1 | ||
Output: [1,null,-3,4] | ||
Note: | ||
The given tree will have between 1 and 5000 nodes. | ||
-10^5 <= node.val <= 10^5 | ||
-10^9 <= limit <= 10^9 | ||
''' | ||
|
||
# Definition for a binary tree node. | ||
# class TreeNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
|
||
class Solution(object): | ||
def sufficientSubset(self, root, limit): | ||
""" | ||
:type root: TreeNode | ||
:type limit: int | ||
:rtype: TreeNode | ||
""" | ||
def reduce_tree(root, limit, curr_sum): | ||
if not root: | ||
return None | ||
|
||
l_sum = [curr_sum[0] + root.val] | ||
r_sum = [l_sum[0]] | ||
|
||
root.left = reduce_tree(root.left, limit, l_sum) | ||
root.right = reduce_tree(root.right, limit, r_sum) | ||
|
||
curr_sum[0] = max(l_sum[0], r_sum[0]) | ||
if curr_sum[0] < limit: | ||
root = None | ||
return root | ||
curr_sum = [0] | ||
return reduce_tree(root, limit, curr_sum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
''' | ||
Return the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once. | ||
Example 1: | ||
Input: "cdadabcc" | ||
Output: "adbc" | ||
Example 2: | ||
Input: "abcd" | ||
Output: "abcd" | ||
Example 3: | ||
Input: "ecbacba" | ||
Output: "eacb" | ||
Example 4: | ||
Input: "leetcode" | ||
Output: "letcod" | ||
Note: | ||
1 <= text.length <= 1000 | ||
text consists of lowercase English letters. | ||
''' | ||
class Solution(object): | ||
def smallestSubsequence(self, text): | ||
""" | ||
:type text: str | ||
:rtype: str | ||
""" | ||
if not text: | ||
return '' | ||
import collections | ||
freq_map = collections.Counter(text) | ||
used = [False]*26 | ||
result = '' | ||
|
||
for char in text: | ||
freq_map[char] -= 1 | ||
if used[ord(char)-97]: | ||
continue | ||
while (result and result[-1] > char and freq_map[result[-1]] > 0): | ||
used[ord(result[-1])-97] = False | ||
result = result[:-1] | ||
|
||
used[ord(char)-97] = True | ||
result += char | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
''' | ||
Given an array A of positive integers, let S be the sum of the digits of the minimal element of A. | ||
Return 0 if S is odd, otherwise return 1. | ||
Example 1: | ||
Input: [34,23,1,24,75,33,54,8] | ||
Output: 0 | ||
Explanation: | ||
The minimal element is 1, and the sum of those digits is S = 1 which is odd, so the answer is 0. | ||
Example 2: | ||
Input: [99,77,33,66,55] | ||
Output: 1 | ||
Explanation: | ||
The minimal element is 33, and the sum of those digits is S = 3 + 3 = 6 which is even, so the answer is 1. | ||
Note: | ||
1 <= A.length <= 100 | ||
1 <= A[i].length <= 100 | ||
''' | ||
class Solution(object): | ||
def sumOfDigits(self, A): | ||
""" | ||
:type A: List[int] | ||
:rtype: int | ||
""" | ||
if not A: | ||
return 0 | ||
|
||
mini = min(A) | ||
result = 0 | ||
while mini > 0: | ||
quo = mini%10 | ||
rem = mini/10 | ||
result += quo | ||
mini = rem | ||
|
||
return 0 if result%2 else 1 |
Oops, something went wrong.