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 bit_strings(index, array, result) | |
# Base case | |
if index == array.length | |
result << array.dup | |
else | |
# Make a choice to include | |
array[index] = 1 | |
bit_strings(index+1, array, result) | |
# Make a choice to exclude | |
array[index] = 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 print_subset_sum(i, sol, psum, elements, x): | |
# Base case | |
if psum == x: | |
print_subset_binary(sol, elements) | |
elif i < len(elements): | |
# Generate candidates | |
for k in range(0, 2): | |
# Check if recursion tree can be pruned | |
if psum + k * elements[i] <= x: |
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 generate_subsets(i, sol, elements): | |
# Base case | |
if i == len(elements): | |
# Print complete solution | |
print_subset_binary(sol, elements) | |
else: | |
# Generate candidate elements | |
for k in range(0, 2): | |
# Include candidate in partial solution | |
sol[i] = k |
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 read_binary_watch(num) | |
case num | |
when 0 | |
["0:00"] | |
when 1 | |
["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"] | |
when 2 | |
["0:03","0:05","0:06","0:09","0:10","0:12","0:17","0:18","0:20","0:24","0:33","0:34","0:36","0:40","0:48","1:01","1:02","1:04","1:08","1:16","1:32","2:01","2:02","2:04","2:08","2:16","2:32","3:00","4:01","4:02","4:04","4:08","4:16","4:32","5:00","6:00","8:01","8:02","8:04","8:08","8:16","8:32","9:00","10:00"] | |
when 3 | |
["0:07","0:11","0:13","0:14","0:19","0:21","0:22","0:25","0:26","0:28","0:35","0:37","0:38","0:41","0:42","0:44","0:49","0:50","0:52","0:56","1:03","1:05","1:06","1:09","1:10","1:12","1:17","1:18","1:20","1:24","1:33","1:34","1:36","1:40","1:48","2:03","2:05","2:06","2:09","2:10","2:12","2:17","2:18","2:20","2:24","2:33","2:34","2:36","2:40","2:48","3:01","3:02","3:04","3:08","3:16","3:32","4:03","4:05","4:06","4:09","4:10","4:12","4:17","4:18","4:20","4:24","4:33","4:34","4:36","4:40","4:48","5:01","5:02","5:04","5:08","5 |
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
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Solution: | |
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': | |
''' |
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
# Definition for a binary tree node. | |
# class TreeNode | |
# attr_accessor :val, :left, :right | |
# def initialize(val = 0, left = nil, right = nil) | |
# @val = val | |
# @left = left | |
# @right = right | |
# end | |
# end | |
# @param {TreeNode} root |
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
# Definition for a binary tree node. | |
# class TreeNode | |
# attr_accessor :val, :left, :right | |
# def initialize(val = 0, left = nil, right = nil) | |
# @val = val | |
# @left = left | |
# @right = right | |
# end | |
# end | |
# @param {TreeNode} root |
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(object): | |
def insert(self, intervals, newInterval): | |
""" | |
:type intervals: List[List[int]] | |
:type newInterval: List[int] | |
:rtype: List[List[int]] | |
""" | |
''' | |
Test cases | |
i) [], [2,5] => [[2,5]] |
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
# 1004: Max ConsecutiveOnes III | |
maxLength = 0 | |
numZeros = 0 | |
# 1052: Grumpy Bookstore Owner | |
maxCustWhenGrumpy = 0 | |
idxOfStartX = 0 | |
custWhenGrumpy = 0 | |
# 1456: Max Number of Vowels in Substring of given length |
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
Interval Operations | |
There are a few operations that can be performed on intervals: | |
1. Merge interval[] s.t. the all intervals are disjoint. | |
[0,5],[5,7] ==> [0,7] | |
2. Insert interval into interval[] s.t. all intervals are disjoint. | |
3. Find intersection between two interval[]. | |
[0,5],[5,7] => [5,5] | |
In order to perform these operations, it is important to understand the 6 |