Skip to content

Commit

Permalink
update bunch2
Browse files Browse the repository at this point in the history
  • Loading branch information
keon committed Mar 14, 2017
1 parent b3583c1 commit d0a0ce6
Show file tree
Hide file tree
Showing 53 changed files with 2,720 additions and 33 deletions.
154 changes: 125 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,109 @@ List of Implementations:
```
.
├── array
│   ├── circular_counter.py
│   ├── flatten.py
│   ├── garage.py
│   ├── house_robber.py
│   ├── longest_increasing_subsequence.py
│   ├── longest_non_repeat.py
│   ├── merge_intervals.py
│   ├── missing_ranges.py
│   ├── plus_one.py
│   └── wiggle_sort.py
│   ├── rotate_array.py
│   ├── summary_ranges.py
│   ├── three_sum.py
│   └── two_sum.py
├── backtrack
│   ├── anagram.py
│   ├── array_sum_combinations.py
│   ├── combination_sum.py
│   ├── expression_add_operators.py
│   ├── factor_combinations.py
│   ├── general_solution.md
│   ├── generate_abbreviations.py
│   ├── generate_parenthesis.py
│   ├── letter_combination.py
│   ├── palindrome_partitioning.py
│   ├── pattern_match.py
│   ├── permute.py
│   ├── permute_unique.py
│   ├── subsets.py
│   └── subsets_unique.py
├── bfs
│   └── shortest_distance_from_all_buildings.py
├── divide-and-conquer
│   ├── expression-add-operators.py
│   └── the-skyline-problem.py
│   ├── shortest_distance_from_all_buildings.py
│   └── word_ladder.py
├── bit
│   ├── count_ones.py
│   ├── power_of_two.py
│   ├── reverse_bits.py
│   ├── single_number2.py
│   ├── single_number.py
│   └── subsets.py
├── design
│   ├── alarm_system.md
│   ├── all_o_one_ds.md
│   ├── calculator.md
│   ├── excel_table.md
│   ├── LRUcache.md
│   ├── nearby_drivers.md
│   ├── ride_sharing.md
│   ├── task_runner.md
│   └── twitter_feeds.md
├── dfs
│   ├── all_factors.py
│   ├── count_islands.py
│   ├── pacific_atlantic.py
│   ├── sudoku_solver.py
│   └── walls_and_gates.py
├── dp
│   ├── buy_sell_stock.py
│   ├── climbing_stairs.py
│   ├── combination_sum.py
│   ├── house_robber.py
│   ├── longest_increasing.py
│   ├── max_product_subarray.py
│   ├── max_subarray.py
│   ├── num_decodings.py
│   ├── regex_matching.py
│   └── word_break.py
├── graph
│   ├── clone_graph.py
│   ├── find_path.py
│   ├── graph.py
│   └── traversal.py
├── hashtable
│   └── hashtable.py
├── heap
│   ├── merge_sorted_k_lists.py
│   ├── skyline.py
│   └── sliding_window_max.py
├── linkedlist
│   ├── add_two_numbers.py
│   ├── copy_random_pointer.py
│   ├── delete_node.py
│   ├── first_cyclic_node.py
│   ├── is_cyclic.py
│   ├── is_palindrome.py
│   ├── kth_to_last.py
│   ├── linkedlist.py
│   └── remove_duplicates.py
│   ├── remove_duplicates.py
│   ├── reverse.py
│   ├── rotate_list.py
│   └── swap_in_pairs.py
├── map
│   ├── hashtable.py
│   ├── longest_common_subsequence.py
│   ├── randomized_set.py
│   └── valid_sudoku.py
├── math
│   ├── generate_strobogrammtic.py
│   ├── is_strobogrammatic.py
│   ├── nth_digit.py
│   └── sqrt_precision_factor.py
├── matrix
│   ├── bomb_enemy.py
│   ├── matrix_rotation.txt
│   └── pacific_atlantic.py
├── out.txt
│   ├── rotate_image.py
│   ├── sparse_dot_vector.py
│   ├── sparse_mul.py
│   └── spiral_traversal.py
├── queue
│   ├── __init__.py
│   ├── max_sliding_window.py
Expand All @@ -59,56 +123,88 @@ List of Implementations:
│   ├── count_elem.py
│   ├── first_occurance.py
│   └── last_occurance.py
├── sorting
├── set
│   └── randomized_set.py
├── sort
│   ├── insertion_sort.py
│   ├── meeting_rooms.py
│   ├── merge_sort.py
│   ├── quick_sort.py
│   ├── selection_sort.py
│   └── sort_colors.py
│   ├── sort_colors.py
│   ├── topsort.py
│   └── wiggle_sort.py
├── stack
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── longest_abs_path.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   └── stack.cpython-35.pyc
│   ├── simplify_path.py
│   ├── stack.py
│   └── stack.pyc
│   ├── stack.pyc
│   └── valid_parenthesis.py
├── string
│   ├── add_binary.py
│   ├── breaking_bad.py
│   ├── decode_string.py
│   ├── encode_decode.py
│   ├── group_anagrams.py
│   ├── int_to_roman.py
│   ├── is_palindrome.py
│   ├── license_number.py
│   ├── missing_ranges.py
│   ├── make_sentence.py
│   ├── multiply_strings.py
│   ├── one_edit_distance.py
│   ├── rabin_karp.py
│   ├── reverse_string.py
│   ├── reverse_vowel.py
│   ├── reverse_words.py
│   ├── roman_to_int.py
│   └── word_squares.py
├── tests
│   └── test_stack.py
├── tmp
│   └── temporary.md
├── tree
│   ├── array2bst.py
│   ├── binary_tree_paths.py
│   ├── bintree2list.py
│   ├── bst_closest_value.py
│   ├── BSTIterator.py
│   ├── bst
│   │   ├── array2bst.py
│   │   ├── bst_closest_value.py
│   │   ├── BSTIterator.py
│   │   ├── delete_node.py
│   │   ├── is_bst.py
│   │   ├── kth_smallest.py
│   │   ├── lowest_common_ancestor.py
│   │   ├── predecessor.py
│   │   ├── serialize_deserialize.py
│   │   ├── successor.py
│   │   └── unique_bst.py
│   ├── deepest_left.py
│   ├── invert_tree.py
│   ├── is_balanced.py
│   ├── is_subtree.py
│   ├── is_symmetric.py
│   ├── longest_consecutive.py
│   ├── lowest_common_ancestor.py
│   ├── max_height.py
│   ├── max_path_sum.py
│   ├── min_height.py
│   ├── predecessor.py
│   ├── path_sum2.py
│   ├── path_sum.py
│   ├── pretty_print.py
│   ├── same_tree.py
│   ├── successor.py
│   └── tree.py
└── trie
├── add_and_search.py
└── trie.py
│   ├── traversal
│   │   ├── inorder.py
│   │   ├── level_order.py
│   │   └── zigzag.py
│   ├── tree.py
│   └── trie
│   ├── add_and_search.py
│   └── trie.py
├── tree.md
└── union-find
└── count_islands.py
```

46 changes: 46 additions & 0 deletions array/circular_counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
There are people sitting in a circular fashion,
print every third member while removing them,
the next counter starts immediately after the member is removed.
Print till all the members are exhausted.
For example:
Input: consider 123456789 members sitting in a circular fashion,
Output: 369485271
"""

a = ['1','2','3','4','5','6','7','8','9']

def josepheus(int_list, skip):
skip = skip - 1 #list starts with 0 index
idx = 0
while len(int_list)>0:
idx = (skip+idx)%len(int_list) #hashing to keep changing the index to every 3rd
print int_list.pop(idx)


josepheus(a,3)

"""
the reason for hashing is that we have to find the index of the item which needs to be removed.
So for e.g. if you iterate with the initial list of folks with every 3rd item eliminated:
INPUT
int_list = 123456789
skip = 3
While Iteration:
int_list = 123456789
len(int_list) = 9
skip = 2 # as int_list starts from 0
idx = (0 + 2) % 9 #here previous index was 0
so 3rd element which is 3 in this case eliminated
int_list = 12456789
len(int_list) = 8
idx = (2 + 2) % 8 #here previous index was 2
so 3rd element starting from 4th person which is 6 would be deleted.
and so on
The reason why we have to do this way is I am not putting the people who escape at the back of list so ideally in 2 while iteration the list should have been
45678912 and then no hashing needed to be done, which means you can directly remove the third element
"""
60 changes: 60 additions & 0 deletions array/flatten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Implement Flatten Arrays.
Given an array that may contain nested arrays,
give a single resultant array.
function flatten(input){
}
Example:
Input: var input = [2, 1, [3, [4, 5], 6], 7, [8]];
flatten(input);
Output: [2, 1, 3, 4, 5, 6, 7, 8]
"""


def list_flatten(l, a=None):
# check a
if a is None:
# initialize with empty list
a = []

for i in l:
if isinstance(i, list):
list_flatten(i, a)
else:
a.append(i)
return a


# stack version
# public static List<Integer> flatten(List<NestedList> l) {
# List<Integer> main = new ArrayList<Integer>();
# Stack<List<NestedList>> stack = new Stack<List<NestedList>>();
# Stack<Integer> indexes = new Stack<Integer>();
# stack.add(l);
# indexes.add(0);
# while (true) {
# if (stack.isEmpty())
# break;
# int index1 = indexes.pop();
# l = stack.pop();
# for (int i = index1; i < l.size(); i++) {
# NestedList n = l.get(i);
# if (n.isInteger()) {
# main.add(n.value);
# } else {
# stack.add(l);
# indexes.add(i+1);
# l = n.list;
# stack.add(l);
# indexes.add(0);
# break;

# }
# }
# }

# return main;
# }
27 changes: 27 additions & 0 deletions array/summary_ranges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Given a sorted integer array without duplicates,
return the summary of its ranges.
For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].
"""


def summary_ranges(nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
res = []
if len(nums) == 1:
return [str(nums[0])]
i = 0
while i < len(nums):
num = nums[i]
while i+1 < len(nums) and nums[i+1] - nums[i] == 1:
i += 1
if nums[i] != num:
res.append(str(num) + "->" + str(nums[i]))
else:
res.append(str(num))
i += 1
return res
Loading

0 comments on commit d0a0ce6

Please sign in to comment.