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
| # source: https://www.hackerrank.com/challenges/3d-surface-area/problem | |
| # video: https://youtu.be/tIG0GF2EqjE | |
| def surfaceArea(A): | |
| n_rows = len(A) | |
| n_cols = len(A[0]) | |
| total = (n_rows * n_cols) * 2 # top and bottom | |
| for row_i in range(n_rows): # O(n) | |
| for col_i in range(n_cols): # O(m) | |
| val = A[row_i][col_i] |
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
| # source: https://leetcode.com/problems/combination-sum | |
| # video: https://youtu.be/0Jt9_qKimCU | |
| def combinationSum(candidates, target): | |
| dp = [[] for _ in range(target + 1)] # len(dp) = target + 1 b/c want to start with 0 | |
| # intitialize with target = 0, must be empty | |
| dp[0].append([]) # start out as empty list, with sum total = 0 | |
| # since combinations mean order doesn't matter, we should ensure we don't pass over | |
| # parts already calculated ("old" ground) |
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
| # source: https://www.hackerrank.com/challenges/find-maximum-index-product/problem?isFullScreen=false | |
| # video: https://youtu.be/z8iSOFUbQUM | |
| # Stack convenience wrapper for list | |
| class Stack(): | |
| def __init__(self): | |
| self._stack = [] # store internal list | |
| def is_empty(self): # true when stack is empty | |
| return len(self._stack) == 0 |
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
| # video: https://youtu.be/I7RFycpqbDk | |
| from math import ceil, log2 | |
| class SegmentationTree(): | |
| def __init__(self, input_list): | |
| self._input_list = input_list[:] # copy of input_list | |
| self._init_tree() | |
| self._is_propogated = True | |
| def _init_tree(self): |
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
| # source: https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/distinct-integers-in-range-66eca44b/description/ | |
| import re | |
| ### SETUP ### | |
| max_int = 5000 | |
| MAX_VALUE = (1 << max_int) - 1 | |
| re_splitter = re.compile(" ") | |
| def agg_func(val_1, val_2): | |
| output = val_1 | val_2 |
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
| # source: https://www.hackerrank.com/challenges/bear-and-steady-gene | |
| # video: https://youtu.be/eIW7vBWzibE | |
| def extras_available(all_counts, max_nucleotide_count): | |
| '''Returns True if any nucleotide count is greater than max_nucleotide_count''' | |
| # loop through all counts | |
| for counts in all_counts.values(): | |
| # if any count is greater than max_nucleotide_count, return True | |
| if counts > max_nucleotide_count: | |
| return True | |
| # if got past loop, no extras found => return False |
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
| # source: https://www.hackerrank.com/challenges/lilys-homework | |
| # video: https://youtu.be/HmyDuVZoE8Y | |
| def lilysHomework_helper(arr, reverse = False): | |
| # clone arr, since modifying | |
| arr = arr[:] # O(n)? | |
| # arr = [2, 5, 3, 1] | |
| # store sorted array | |
| sorted_arr = sorted(arr, reverse = reverse) # O(nlogn) | |
| # sorted_arr = [1, 2, 3, 5] | |
| # OR [5, 3, 2, 1] if reverse = True |
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
| # source: https://www.hackerrank.com/challenges/gridland-metro | |
| # video: https://youtu.be/H1Qbd652Oig | |
| def gridlandMetro(n, m, k, tracks): # ALTERED NAME TO PLURAL | |
| # tracks = [ | |
| # [row, col_start, col_end], | |
| # ... | |
| # ] | |
| # sort by col start | |
| tracks.sort(key = lambda track: min(track[1], track[2])) # O(nlogn) |
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
| # source: https://www.hackerrank.com/challenges/kindergarten-adventures | |
| # video: https://youtu.be/9vcrruySa9s | |
| def sanitize_index(index, n): | |
| # index already valid | |
| if 0 <= index and index < n: | |
| return index | |
| # negative index | |
| if index < 0: | |
| return n + index |
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
| // source: https://www.youtube.com/redirect?q=https%3A%2F%2Fwww.hackerrank.com%2Fchallenges%2Fwaiter%2Fproblem%3Fh_r%3Dinternal-search&event=video_description&redir_token=QUFFLUhqbWRvZWVfbjJJUVQ0Q2J0WnpTLTVVUnBPYnFYd3xBQ3Jtc0trUXJ5MGF5UVJ5UlgxakpSQnZsXzB0QXY2bk5INzJ4MmhkRk5YLWUzV1RjLXQzX2xqUFl3LUtUVnhlT29QV0VoY3FhOVVqMDZrZ21JQjRXVEdvbUdHR3BZajllbmNuVGJBMTFtcXY0VWxaMGxyUmt1NA%3D%3D&v=UW-1KB9PFrA | |
| // video: https://www.youtube.com/watch?v=UW-1KB9PFrA&lc=UgyNhlGoJNiMul7Byad4AaABAg | |
| function getPrimes(n){ | |
| const primesStr = "2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,7 |