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
# 675. Cut Off Trees for Golf Event | |
# Maze with obstacles | |
class Solution: | |
def cutOffTree(self, forest: List[List[int]]) -> int: | |
R, C = len(forest), len(forest[0]) | |
queue = [(0,0,0)] # sr, sc, steps | |
target = (0,0) | |
maxWeight = 0; |
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
def hammingWeight(self, n): | |
nstr = '{0:32b}'.format(n) | |
count = 0 | |
for x in nstr: | |
if x == "1": | |
count += 1 | |
return count | |
""" |
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
def hammingDistance(x, y): | |
xor = '{0:32b}'.format(x ^ y) | |
count = 0 | |
for x in xor: | |
if x == "1": | |
count += 1 | |
return count | |
print(hammingDistance(1, 4)) |
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
class Solution: | |
def removeOuterParentheses(self, S): | |
complete_pairs = self.getCompletePairs(list(S)) | |
output = "" | |
for pair in complete_pairs: | |
output += "".join(pair[1:-1]) | |
return output | |
def getCompletePairs(self ,arr): |
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
class TreeNode: | |
def __init__(self, x): | |
self.val = x | |
self.left = None | |
self.right = None | |
class Solution: | |
def __init__(self): | |
self.sum = 0 |
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
# Merge two sorted arrays | |
def merge_two_arrays(arr1, arr2): | |
merged = [] | |
i = 0 | |
j = 0 | |
k = len(arr1) | |
m = len(arr2) | |
while len(merged) < (k + m): |
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
def non_repeating(arr): | |
non_repeating = [] | |
for n in arr: | |
if n in non_repeating: | |
non_repeating.pop(non_repeating.index(n)) | |
else: | |
non_repeating.append(n) | |
return non_repeating[0] if non_repeating else None |
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
def second_minimum(arr): | |
second = arr[1] | |
first = arr[0] | |
for n in arr: | |
if n < first: | |
first = n | |
if n < second and n > first: | |
second = n | |
return second | |
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
""" You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels. | |
The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A". | |
Example 1: | |
Input: J = "aA", S = "aAAbbbb" | |
Output: 3 | |
Example 2: | |
Input: J = "z", S = "ZZ" | |
Output: 0 | |
Note: | |
• S and J will consist of letters and have length at most 50. |
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
def convert_palindrome(_str): | |
first_half = _str[:int(len(_str)/2)] | |
middle = _str[int(len(_str)/2)] | |
if middle == '0': | |
middle = '9' | |
first_half = str(int(first_half) -1 ) | |
elif middle == '9': | |
middle = '0' | |
first_half = str(int(first_half) +1 ) |
NewerOlder