This file contains hidden or 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 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]) | |
This file contains hidden or 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 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: |
This file contains hidden or 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
''' | |
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: |
This file contains hidden or 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
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: |
This file contains hidden or 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
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}" |
This file contains hidden or 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
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' |
This file contains hidden or 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
''' | |
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 |
This file contains hidden or 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
""" | |
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. |
This file contains hidden or 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
# 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 | |
''' |