Skip to content

Instantly share code, notes, and snippets.

View sdasara95's full-sized avatar
💬
Continual Learning

Satya Dasara sdasara95

💬
Continual Learning
View GitHub Profile
class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
nums.append(0)
for i in range(2,len(nums)):
nums[i] += max(nums[i-2],nums[i-3])
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
trie_root = build_trie(words)
answers = set()
m = len(board)
n = len(board[0])
def dfs(i, j, trie_node, visited):
if not trie_node:
'''
https://leetcode.com/discuss/interview-question/325845/how-to-solve-this-string-decoding-problem
Given an encoded string in form of "ab[cd]{2}def"
You have to return decoded string "abcdcddef"
Notice that if there is a number inside curly braces, then it means preceding string in square brackets has to be repeated the same number of times. It becomes tricky where you have nested braces.
Example 1:
@sdasara95
sdasara95 / Max_items_purchase.py
Created July 25, 2020 23:57
Given a fixed amount of money, find the maximum number of items that can be purchased.
input = [2,3,5,1,1,2,1]
money = 5
# input = [10,10,10]
# money = 5
input.sort()
res = []
for inp in input:
if inp<=money:
@sdasara95
sdasara95 / Date_to_quarter.py
Last active July 25, 2020 23:55
Given a list of dates in Y-M-d format, return the output as YQ{quarter number}
input = ["2014-03-25", "2012-08-05", "2015-05-19"]
# Output = ["2014Q1", "20123Q", "2015Q2"]
def quarter(inp):
year,month,day = inp.split('-')
month = int(month)
q = (month-1)//3
q = int(q)+1
return f"{year}Q{q}"
@sdasara95
sdasara95 / Two_circles.py
Created July 25, 2020 23:53
Given the input for two circles as cx1 cy1 r1 cx2 cy2 r2, find the relation between the two circles i.e. concentric, intersecting, touching, disjoint.
import math
a = input()
def two_circles(inp):
x1,y1,r1,x2,y2,r2 = [int(i) for i in inp.split()]
c1c2 = abs(x1-x2)+abs(y2-y1)
# c1c2 = math.sqrt((x1-x2)**2 + (y1-y2)**2)
ret = None
if x1==x2 and y1==y2:
ret = 'Concentric'
@sdasara95
sdasara95 / uniform_buffer.py
Created May 24, 2020 01:26
Return a buffer item with uniform probability
'''
Good morning! Here's your coding interview problem for today.
This problem was asked by Facebook.
Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability.
The idea here is to update the count everytime we get a new number to our buffer. This way our total elements in the buffer
stays updated. We then take any arbitrary value (in this case 1) and generate a random number between 0 and n-1(number of elements
in current buffer). The probability of us getting 1 is 1/n-1 which is Uniform Probability. So everytime we get a new number we
@sdasara95
sdasara95 / pi_monte_carlo.py
Created May 23, 2020 21:09
Approximate value of Pi using Monte Carlo method
"""
The area of a circle is defined as πr^2. Estimate π to 3 decimal places using a Monte Carlo method.
Hint: The basic equation of a circle is x2 + y2 = r2.
Imagine a square of side 1.
Circle inside it will have radius 0.5.
Center of circle will be (0.5,0.5)
Randomly generate points for some number of iteration.
@sdasara95
sdasara95 / max_positions.py
Last active May 24, 2020 01:26
Maximum applications when alternative only allowed
# https://docs.google.com/document/d/1EPMkzF8NwoE6xYdFw_RuJKseo4wEvzw6P6jRI_XqtSE/edit
class BinaryTree(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None
'''