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 two_sum(nums, target): | |
| n = len(nums) | |
| for i in range(n): | |
| for j in range(i + 1, n): # we start the second loop from j = i+1. | |
| if nums[i] + nums[j] == target: | |
| return [i, j] | |
| return [] |
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 twoSum(nums, target): | |
| mapper = {} | |
| for i, e in enumerate(nums): | |
| # Store the numbers inside the hashmap, | |
| # where the keys are the numbers, | |
| # and the values are the coressponding indicies. | |
| mapper[e] = 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
| def three_sum_brute(nums): | |
| nums = sorted(nums) # sort nums. | |
| n = len(nums) | |
| target = 0 | |
| result = set() | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| for k in range(j + 1, 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
| def three_sum_optimized(nums): | |
| nums = sorted(nums) | |
| n = len(nums) | |
| result = set() | |
| for i in range(n): | |
| left = i + 1 | |
| right = n - 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
| def fourSum(nums, target): | |
| nums = sorted(nums) | |
| n = len(nums) | |
| result = set() | |
| for i in range(n): # first loop | |
| second_taget = target - nums[i] # target 2 | |
| for j in range(i + 1, n): # second loop | |
| third_target = second_taget - nums[j] # target 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
| def letterCombinations(digits): | |
| hashmap = { | |
| "1": "None", | |
| "2": ["a", "b", "c"], | |
| "3": ["d", "e", "f"], | |
| "4": ["g", "h", "i"], | |
| "5": ["j", "k", "l"], | |
| "6": ["m", "n", "o"], | |
| "7": ["p", "q", "r", "s"], |
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 letterCombinations(digits): | |
| hashmap = { | |
| "1": "None", | |
| "2": ["a", "b", "c"], | |
| "3": ["d", "e", "f"], | |
| "4": ["g", "h", "i"], | |
| "5": ["j", "k", "l"], | |
| "6": ["m", "n", "o"], | |
| "7": ["p", "q", "r", "s"], |
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 generateParenthesis(n): | |
| def generate(opening_count, closing_count, path, result): | |
| # Constraint 1 | |
| if opening_count > closing_count: | |
| return | |
| # Constraint 2 | |
| if opening_count < 0: | |
| return | |
| # Constraint 3 | |
| if closing_count < 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
| def generateParenthesis(n): | |
| # Create is_valid parenthesis to check if a parenthesis is | |
| # well-formed. | |
| def is_valid_parenthesis(string): | |
| if len(string) < 2: | |
| 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
| def max_water_area(height): | |
| n = len(height) | |
| area = 0 | |
| for i in range(n): | |
| for j in range(i + 1, n): | |
| h = min(height[i], height[j]) | |
| w = j - i | |
| area = max(area, h * w) |
OlderNewer