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/sherlock-and-valid-string/problem | |
# video: https://youtu.be/WTwih7Ghvig | |
def isValid(s): | |
count_of_chars = dict() | |
for char in s: | |
if char not in count_of_chars: | |
count_of_chars[char] = 0 | |
count_of_chars[char] += 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
# source: https://www.hackerrank.com/challenges/beautiful-triplets/problem | |
# video: https://youtu.be/fOUWTyxYXUQ | |
def beautifulTriplets(d, arr): | |
# Keep a running count dict | |
counts = {} | |
total = 0 | |
# loop through list to get counts | |
for val in arr: # O(n) | |
if val not in counts: |
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/equality-in-a-array/problem | |
# video: https://youtu.be/CLIVaqu39wU | |
def equalizeArray(arr): | |
# keep dict of counts | |
counts = {} | |
# keep highest count item | |
highest_count_item = arr[0] | |
# loop through list to take counts | |
for item in arr: # O(n) |
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/acm-icpc-team/problem | |
# video: https://youtu.be/tfHd30CGAWs | |
def acmTeam(topic): | |
# keep track of teams (hashmap/dict) | |
teams = {} | |
length = len(topic) | |
for i in range(length - 1): # O(n) | |
curr_bin_string = topic[i] # first team member | |
# Take each bit set and use OR operation to create combined topic knowledge team |
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/taum-and-bday/problem | |
# video: https://youtu.be/9vajRE0bJ5w | |
def taumBday(b, w, bc, wc, z): | |
# if black gifts > white gifts + z conversion cost | |
if bc > wc + z: | |
return (w * wc) + (b * (wc + z)) | |
# if white gifts > black gifts + z conversion cost | |
if wc > bc + z: | |
return (b * bc) + (w * (bc + z)) |
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
def roadsAndLibraries(n, c_lib, c_road, cities): | |
# if library cost is less than road cost, then cheaper to only build libraries | |
if c_lib <= c_road: | |
return n * c_lib | |
city_groups = {city_id:{city_id} for city_id in range(1,n + 1)} | |
# groups = {1: {1}, 2: {2}, 3: {3}, 4: {4}, 5: {5}} | |
for city_a, city_b in cities: # O(m); where m = number of all city roads | |
city_groups[city_a].add(city_b) |
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/board-cutting/problem | |
def boardCutting(cost_y, cost_x): | |
MOD_INT = (10**9) + 7 | |
# sort each cost (asc) | |
cost_y.sort() # O(nlogn) | |
cost_x.sort() # O(mlogm) | |
horizontal_boards = 1 | |
vertical_boards = 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
# source: https://www.hackerrank.com/challenges/hackerland-radio-transmitters | |
def hackerlandRadioTransmitters(houses, transmitter): | |
houses.sort() # requires sorting => O(nlogn) | |
transmitter_count = 0 | |
i = 0 | |
while i < len(houses): # O(n) | |
transmitter_count += 1 | |
max_transmitting_house = houses[i] + transmitter | |
# position after transmitting house |
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) |