├── README.md ├── main.py ├── mainAA.py ├── mainAB.py ├── mainAC.py ├── mainAD.py ├── mainAE.py ├── mainAF.py ├── mainAG.py ├── mainAH.py ├── mainAI.py ├── mainB.py ├── mainC.py ├── mainD.py ├── mainE.py ├── mainF.py ├── mainG.py ├── mainH.py ├── mainI.py ├── mainJ.py ├── mainK.py ├── mainL.py ├── mainM.py ├── mainN.py ├── mainO.py ├── mainP.py ├── mainQ.py ├── mainR.py ├── mainS.py ├── mainT.py ├── mainU.py ├── mainV.py ├── mainW.py ├── mainX.py ├── mainY.py └── mainZ.py /README.md: -------------------------------------------------------------------------------- 1 | For learning. 2 | 3 | 150 interview question using python3 4 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def merge(self, nums1, m, nums2, n): 3 | i = m - 1 4 | j = n - 1 5 | k = m + n - 1 6 | 7 | while j >= 0: 8 | if i >= 0 and nums1[i] > nums2[j]: 9 | nums1[k] = nums1[i] 10 | i -= 1 11 | else: 12 | nums1[k] = nums2[j] 13 | j -= 1 14 | k -= 1 15 | -------------------------------------------------------------------------------- /mainAA.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, numbers: List[int], target: int) -> List[int]: 3 | l, r = 0, len(numbers) - 1 4 | 5 | while l < r: 6 | curSum = numbers[l] + numbers[r] 7 | 8 | if curSum > target: 9 | r -=1 10 | elif curSum < target: 11 | l += 1 12 | else: 13 | return [l + 1, r + 1] 14 | return [] -------------------------------------------------------------------------------- /mainAB.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxArea(self, height: List[int]) -> int: 3 | res = 0 4 | l, r = 0, len(height) - 1 5 | 6 | while l < r: 7 | area = (r - l) * min(height[l], height[r]) 8 | res = max(res, area) 9 | 10 | if height[l] < height[r]: 11 | l += 1 12 | else: 13 | r -= 1 14 | 15 | return res -------------------------------------------------------------------------------- /mainAC.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def threeSum(self, nums: List[int]) -> List[List[int]]: 3 | res = [] 4 | nums.sort() 5 | 6 | for i, a in enumerate(nums): 7 | if i > 0 and a == nums[i - 1]: 8 | continue 9 | l, r = i + 1, len(nums) - 1 10 | while l < r: 11 | threeSum = a + nums[l] + nums[r] 12 | if threeSum > 0: 13 | r -= 1 14 | elif threeSum < 0: 15 | l += 1 16 | else: 17 | res.append([a, nums[l], nums[r]]) 18 | l += 1 19 | while nums[l] == nums[l - 1] and l < r: 20 | l += 1 21 | return res -------------------------------------------------------------------------------- /mainAD.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSubArrayLen(self, target: int, nums: List[int]) -> int: 3 | l, total = 0, 0 4 | res = float("inf") 5 | 6 | for r in range(len(nums)): 7 | total += nums[r] 8 | while total >= target: 9 | res = min(r - l + 1, res) 10 | total -= nums[l] 11 | l += 1 12 | return 0 if res == float("inf") else res -------------------------------------------------------------------------------- /mainAE.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLongestSubstring(self, s: str) -> int: 3 | map = [-1] * 256 # Array to store last seen indices 4 | length, right, left = 0, 0, 0 5 | n = len(s) 6 | 7 | while right < n: 8 | # If the character has been seen, adjust the left pointer 9 | if map[ord(s[right])] != -1: 10 | left = max(map[ord(s[right])] + 1, left) 11 | # Update the last seen index 12 | map[ord(s[right])] = right 13 | # Calculate the length of the current window 14 | length = max(length, right - left + 1) 15 | right += 1 16 | return length -------------------------------------------------------------------------------- /mainAF.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findSubstring(self, s: str, words: List[str]) -> List[int]: 3 | if not s or not words: 4 | return [] 5 | 6 | word_freq = {} 7 | for word in words: 8 | word_freq[word] = 1 + word_freq.get(word, 0) 9 | 10 | word_len = len(words[0]) 11 | window_len = len(words) * word_len 12 | 13 | res = [] 14 | 15 | for i in range(len(s) - window_len + 1): 16 | substr_freq = {} 17 | j = i 18 | 19 | while j < i + window_len: 20 | cur_word = s[j:j+word_len] 21 | 22 | if cur_word not in word_freq: 23 | break 24 | substr_freq[cur_word] = substr_freq.get(cur_word, 0) + 1 25 | 26 | if substr_freq[cur_word] > word_freq[cur_word]: 27 | break 28 | j += word_len 29 | 30 | if j == i + window_len: 31 | res.append(i) 32 | return res -------------------------------------------------------------------------------- /mainAG.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minWindow(self, s: str, t: str) -> str: 3 | if len(s) < len(t): 4 | return "" 5 | 6 | char_count = defaultdict(int) 7 | for ch in t: 8 | char_count[ch] += 1 9 | 10 | target_chars_remaining = len(t) 11 | min_window = (0, float("inf")) 12 | start_index = 0 13 | 14 | for end_index, ch in enumerate(s): 15 | if char_count[ch] > 0: 16 | target_chars_remaining -= 1 17 | char_count[ch] -= 1 18 | 19 | if target_chars_remaining == 0: 20 | while True: 21 | char_at_start = s[start_index] 22 | if char_count[char_at_start] == 0: 23 | break 24 | char_count[char_at_start] += 1 25 | start_index += 1 26 | 27 | if end_index - start_index < min_window[1] - min_window[0]: 28 | min_window = (start_index, end_index) 29 | 30 | char_count[s[start_index]] += 1 31 | target_chars_remaining += 1 32 | start_index += 1 33 | 34 | return "" if min_window[1] > len(s) else s[min_window[0]:min_window[1]+1] -------------------------------------------------------------------------------- /mainAH.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isValidSudoku(self, board: List[List[str]]) -> bool: 3 | cols = collections.defaultdict(set) 4 | rows = collections.defaultdict(set) 5 | squares = collections.defaultdict(set) 6 | 7 | for r in range(9): 8 | for c in range(9): 9 | if board[r][c] == ".": 10 | continue 11 | if (board[r][c] in rows[r] or 12 | board[r][c] in cols[c] or 13 | board[r][c] in squares[(r // 3, c // 3)]): 14 | return False 15 | cols[c].add(board[r][c]) 16 | rows[r].add(board[r][c]) 17 | squares[(r // 3, c // 3)].add(board[r][c]) 18 | return True -------------------------------------------------------------------------------- /mainAI.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def spiralOrder(self, matrix: List[List[int]]) -> List[int]: 3 | res = [] 4 | left, right = 0, len(matrix[0]) 5 | top, bottom = 0, len(matrix) 6 | 7 | while left < right and top < bottom: 8 | for i in range(left, right): 9 | res.append(matrix[top][i]) 10 | top += 1 11 | 12 | for i in range(top, bottom): 13 | res.append(matrix[i][right - 1]) 14 | right -= 1 15 | if not (left < right and top < bottom): 16 | break 17 | for i in range(right - 1, left - 1, -1): 18 | res.append(matrix[bottom -1][i]) 19 | bottom -= 1 20 | for i in range(bottom - 1, top - 1, -1): 21 | res.append(matrix[i][left]) 22 | left += 1 23 | 24 | return res -------------------------------------------------------------------------------- /mainB.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def removeElement(self, nums, val): 3 | """ 4 | :type nums: List[int] 5 | :type val: int 6 | :rtype: int 7 | """ 8 | count = i = 0 9 | while i < len(nums): 10 | if nums[i] == val: 11 | nums.pop(i) 12 | count += 1 13 | continue 14 | i += 1 15 | return len(nums) -------------------------------------------------------------------------------- /mainC.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TerranceFinleyZ/Leetcode-/eece161572db8272c67f5f9e4b7993e9229acf05/mainC.py -------------------------------------------------------------------------------- /mainD.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TerranceFinleyZ/Leetcode-/eece161572db8272c67f5f9e4b7993e9229acf05/mainD.py -------------------------------------------------------------------------------- /mainE.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def majorityElement(self, nums: list[int]) -> int: 3 | ans = None 4 | count = 0 5 | for num in nums: 6 | if count == 0: 7 | ans = num 8 | count += (1 if num == ans else -1) 9 | return ans -------------------------------------------------------------------------------- /mainF.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, nums: List[int], k: int) -> None: 3 | """ 4 | Do not return anything, modify nums in-place instead. 5 | """ 6 | 7 | k = k%len(nums) #normalize k value 8 | nums[:] = nums[-k:] + nums[:-k] #rearrange list by k rotation 9 | -------------------------------------------------------------------------------- /mainG.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, prices: List[int]) -> int: 3 | profit = 0 4 | l = 0 5 | for r in range(len(prices)): 6 | if prices[l] > prices[r]: 7 | l = r 8 | else: 9 | profit = max(profit, prices[r] - prices[l]) 10 | return profit -------------------------------------------------------------------------------- /mainH.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxProfit(self, prices: List[int]) -> int: 3 | x = float('inf') 4 | max_profit = 0 5 | 6 | for i in prices: 7 | if i > x: 8 | max_profit += i-x 9 | x = i 10 | return max_profit -------------------------------------------------------------------------------- /mainI.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canJump(self, nums: List[int]) -> bool: 3 | gas = 0 4 | for n in nums: 5 | if gas < 0: 6 | return False 7 | elif n > gas: 8 | gas = n 9 | gas -= 1 10 | 11 | return True -------------------------------------------------------------------------------- /mainJ.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def jump(self, nums: List[int]) -> int: 3 | near = far = jumps = 0 4 | while far < len(nums) - 1: 5 | farthest = 0 6 | for i in range(near, far + 1): 7 | farthest = max(farthest, i + nums[i]) 8 | 9 | near = far + 1 10 | far = farthest 11 | jumps += 1 12 | 13 | return jumps -------------------------------------------------------------------------------- /mainK.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hIndex(self, citations: List[int]) -> int: 3 | n = len(citations) 4 | paper_counts = [0] * (n+1) 5 | 6 | for c in citations: 7 | paper_counts[min(n, c)] += 1 8 | 9 | h = n 10 | papers = paper_counts[n] 11 | 12 | while papers < h: 13 | h -= 1 14 | papers += paper_counts[h] 15 | 16 | return h -------------------------------------------------------------------------------- /mainL.py: -------------------------------------------------------------------------------- 1 | class RandomizedSet: 2 | 3 | def __init__(self): 4 | self.numMap = {} 5 | self.numList = [] 6 | 7 | 8 | def insert(self, val: int) -> bool: 9 | res = val not in self.numMap 10 | if res: 11 | self.numMap[val] = len(self.numList) 12 | self.numList.append(val) 13 | return res 14 | 15 | def remove(self, val: int) -> bool: 16 | res = val in self.numMap 17 | if res: 18 | idx = self.numMap[val] 19 | lastVal = self.numList[-1] 20 | self.numList[idx] = lastVal 21 | self.numList.pop() 22 | self.numMap[lastVal] = idx 23 | del self.numMap[val] 24 | return res 25 | 26 | def getRandom(self) -> int: 27 | return random.choice(self.numList) 28 | 29 | 30 | # Your RandomizedSet object will be instantiated and called as such: 31 | # obj = RandomizedSet() 32 | # param_1 = obj.insert(val) 33 | # param_2 = obj.remove(val) 34 | # param_3 = obj.getRandom() -------------------------------------------------------------------------------- /mainM.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def productExceptSelf(self, nums: List[int]) -> List[int]: 3 | res = [1] * (len(nums)) 4 | 5 | prefix = 1 6 | for i in range(len(nums)): 7 | res[i] = prefix 8 | prefix *= nums[i] 9 | postfix = 1 10 | for i in range(len(nums) - 1, -1, -1): 11 | res[i] *= postfix 12 | postfix *= nums[i] 13 | return res -------------------------------------------------------------------------------- /mainN.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: 3 | if sum(gas) < sum(cost): 4 | return -1 5 | 6 | curernt_gas = 0 7 | start = 0 8 | for i in range(len(gas)): 9 | curernt_gas += gas[i] - cost[i] 10 | if curernt_gas < 0: 11 | curernt_gas = 0 12 | start = i + 1 13 | 14 | return start -------------------------------------------------------------------------------- /mainO.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def candy(self, ratings: List[int]) -> int: 3 | n = len(ratings) 4 | total_candies = n 5 | i = 1 6 | 7 | while i < n: 8 | if ratings[i] == ratings[i - 1]: 9 | i += 1 10 | continue 11 | 12 | current_peak = 0 13 | while i < n and ratings[i] > ratings[i - 1]: 14 | current_peak += 1 15 | total_candies += current_peak 16 | i += 1 17 | 18 | if i == n: 19 | return total_candies 20 | 21 | current_valley = 0 22 | while i < n and ratings[i] < ratings[i - 1]: 23 | current_valley += 1 24 | total_candies += current_valley 25 | i += 1 26 | 27 | total_candies -= min(current_peak, current_valley) 28 | 29 | return total_candies -------------------------------------------------------------------------------- /mainP.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def trap(self, height: List[int]) -> int: 3 | left = 0 4 | right = len(height) - 1 5 | left_max = height[left] 6 | right_max = height[right] 7 | water = 0 8 | 9 | while left < right: 10 | if left_max < right_max: 11 | left += 1 12 | left_max = max(left_max, height[left]) 13 | water += left_max - height[left] 14 | else: 15 | right -= 1 16 | right_max = max(right_max, height[right]) 17 | water += right_max - height[right] 18 | 19 | return water -------------------------------------------------------------------------------- /mainQ.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def romanToInt(self, s: str) -> int: 3 | roman = { "I" : 1, "V" : 5, "X" : 10, "L" : 50, "C" : 100, "D" : 500, "M" : 1000} 4 | 5 | res = 0 6 | 7 | for i in range(len(s)): 8 | if i + 1 < len(s) and roman[s[i]] < roman[s[i + 1]]: 9 | res -= roman[s[i]] 10 | else: 11 | res += roman[s[i]] 12 | return res -------------------------------------------------------------------------------- /mainR.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intToRoman(self, num: int) -> str: 3 | symList = [["I", 1], ["IV" , 4], ["V" , 5], ["IX" , 9], 4 | ["X" , 10], ["XL" , 40], ["L" , 50], ["XC" , 90], 5 | ["C" , 100], ["CD" , 400], ["D", 500], ["CM", 900], 6 | ["M" , 1000]] 7 | 8 | res = "" 9 | for sym, val in reversed(symList): 10 | if num // val: 11 | count = num // val 12 | res += (sym * count) 13 | num = num % val 14 | return res -------------------------------------------------------------------------------- /mainS.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s: str) -> int: 3 | end = len(s) - 1 4 | 5 | while s[end] == " ": 6 | end -= 1 7 | 8 | start = end 9 | while start >= 0 and s[start] != " ": 10 | start -= 1 11 | 12 | return end - start -------------------------------------------------------------------------------- /mainT.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestCommonPrefix(self, strs: List[str]) -> str: 3 | res = "" 4 | 5 | for i in range(len(strs[0])): 6 | for s in strs: 7 | if i == len(s) or s[i] != strs[0][i]: 8 | return res 9 | res += strs[0][i] 10 | 11 | return res -------------------------------------------------------------------------------- /mainU.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def trap(self, height: List[int]) -> int: 3 | left = 0 4 | right = len(height) - 1 5 | left_max = height[left] 6 | right_max = height[right] 7 | water = 0 8 | 9 | while left < right: 10 | if left_max < right_max: 11 | left += 1 12 | left_max = max(left_max, height[left]) 13 | water += left_max - height[left] 14 | else: 15 | right -= 1 16 | right_max = max(right_max, height[right]) 17 | water += right_max - height[right] 18 | 19 | return water -------------------------------------------------------------------------------- /mainV.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def romanToInt(self, s: str) -> int: 3 | roman = { "I" : 1, "V" : 5, "X" : 10, "L" : 50, "C" : 100, "D" : 500, "M" : 1000} 4 | 5 | res = 0 6 | 7 | for i in range(len(s)): 8 | if i + 1 < len(s) and roman[s[i]] < roman[s[i + 1]]: 9 | res -= roman[s[i]] 10 | else: 11 | res += roman[s[i]] 12 | return res -------------------------------------------------------------------------------- /mainW.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intToRoman(self, num: int) -> str: 3 | symList = [["I", 1], ["IV" , 4], ["V" , 5], ["IX" , 9], 4 | ["X" , 10], ["XL" , 40], ["L" , 50], ["XC" , 90], 5 | ["C" , 100], ["CD" , 400], ["D", 500], ["CM", 900], 6 | ["M" , 1000]] 7 | 8 | res = "" 9 | for sym, val in reversed(symList): 10 | if num // val: 11 | count = num // val 12 | res += (sym * count) 13 | num = num % val 14 | return res -------------------------------------------------------------------------------- /mainX.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s: str) -> int: 3 | end = len(s) - 1 4 | 5 | while s[end] == " ": 6 | end -= 1 7 | 8 | start = end 9 | while start >= 0 and s[start] != " ": 10 | start -= 1 11 | 12 | return end - start -------------------------------------------------------------------------------- /mainY.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s: str) -> bool: 3 | newStr = "" 4 | 5 | for c in s: 6 | if c.isalnum(): 7 | newStr += c.lower() 8 | return newStr == newStr[::-1] -------------------------------------------------------------------------------- /mainZ.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isSubsequence(self, s: str, t: str) -> bool: 3 | i, j = 0, 0 4 | 5 | while i < len(s) and j < len(t): 6 | if s[i] == t[j]: 7 | i += 1 8 | j += 1 9 | 10 | return True if i == len(s) else False --------------------------------------------------------------------------------