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
| class Solution(object): | |
| def isValid(self, s): | |
| """ | |
| :type s: str | |
| :rtype: bool | |
| """ | |
| stack = [] | |
| closeToOpen = { |
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
| class Solution(object): | |
| def threeSum(self, nums): | |
| nums.sort() | |
| res = [] | |
| for i in range(len(nums) - 2): | |
| if i > 0 and nums[i] == nums[i-1]: # skip duplicate i | |
| continue | |
| low, high = i + 1, len(nums) - 1 | |
| while low < high: |
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
| class Solution(object): | |
| def maxArea(self, height): | |
| """ | |
| :type height: List[int] | |
| :rtype: int | |
| """ | |
| left, right = 0, len(height) - 1 | |
| maxArea = 0 | |
| while left < right: |
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
| class Solution: | |
| def longestConsecutive(self, nums: List[int]) -> int: | |
| s = set(nums) | |
| longest = 0 | |
| for num in s: | |
| if num - 1 not in s: | |
| next_num = num + 1 | |
| length = 1 | |
| while next_num in 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
| class Solution: | |
| def topKFrequent(self, nums: List[int], k: int) -> List[int]: | |
| # lets use the bucket sort | |
| # dict | |
| # count: [0, 1, 2, 3] | |
| # freq: [[], [3], [2], [1]] | |
| # basically values how many time have appeared | |
| # (index is count) | |
| n = len(nums) |
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
| from collections import defaultdict | |
| class Solution: | |
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | |
| anagrams = defaultdict(list) | |
| for st in strs: | |
| count = [0] * 26 | |
| for c in st: | |
| count[ord(c) - ord('a')] += 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
| class Solution: | |
| def isValidSudoku(self, board: List[List[str]]) -> bool: | |
| rows = collections.defaultdict(set) | |
| cols = collections.defaultdict(set) | |
| boxes = collections.defaultdict(set) | |
| for r in range(9): | |
| for c in range(9): | |
| if board[r][c] == '.': | |
| continue |
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
| # Thanks to Greg Hogg for an amazing explaination | |
| # Video: https://www.youtube.com/watch?v=yKZFurr4GQA | |
| class Solution: | |
| def productExceptSelf(self, nums: List[int]) -> List[int]: | |
| l_mult = 1 | |
| r_mult = 1 | |
| n = len(nums) | |
| l_arr = [0] * n | |
| r_arr = [0] * 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
| public class LCS { | |
| public static int lcs(String X, String Y, int m, int n) { | |
| // Base case: If either string is empty, LCS is 0 | |
| if (m == 0 || n == 0) return 0; | |
| // If last characters match, include in LCS and recurse | |
| if (X.charAt(m-1) == Y.charAt(n-1)) | |
| return 1 + lcs(X, Y, m-1, n-1); | |
| // If they don't match, take max of excluding last char from X or Y | |
| else | |
| return Math.max(lcs(X, Y, m, n-1), lcs(X, Y, m-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
| function revWithReduce(str: string): string { | |
| return str.split("").reduce((acc, curr) => curr + acc, ""); | |
| } | |
| function revWithLoop(str: string): string { | |
| let reversed = ""; | |
| for (let i = str.length - 1; i >= 0; i--) { | |
| reversed += str.at(i); | |
| } |
NewerOlder