Skip to content

Instantly share code, notes, and snippets.

@ranasaani
ranasaani / cutt_off_trees_for_golf_event.py
Created June 18, 2020 14:01
675. Cut Off Trees for Golf Event
# 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;
@ranasaani
ranasaani / hammingWeight.py
Last active May 13, 2019 18:42
191. Number of 1 Bits
def hammingWeight(self, n):
nstr = '{0:32b}'.format(n)
count = 0
for x in nstr:
if x == "1":
count += 1
return count
"""
@ranasaani
ranasaani / hamming_distance.py
Last active May 13, 2019 18:43
461. Hamming Distance
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))
@ranasaani
ranasaani / remove_outer_parentheses.py
Last active May 13, 2019 18:43
Remove Outermost Parentheses
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):
@ranasaani
ranasaani / bst_sum.py
Last active May 13, 2019 18:56
BST: return the sum of values of all nodes with value between L and R
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def __init__(self):
self.sum = 0
@ranasaani
ranasaani / merge_two_sorted_arrays.py
Created May 9, 2019 17:37
Merge two sorted arrays
# 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):
@ranasaani
ranasaani / non-repeating.py
Created May 9, 2019 17:05
First non-repeating integers in an array in Python
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
@ranasaani
ranasaani / second_minimum_element.py
Created May 9, 2019 16:50
Find the second minimum element of an array
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
@ranasaani
ranasaani / stones_n_jewels.py
Created May 8, 2019 17:00
Stones and Jewels
""" 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.
@ranasaani
ranasaani / find_closest_palindrome_number.py
Created May 3, 2019 17:55
Find the Closest Palindrome
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 )