Skip to content

Instantly share code, notes, and snippets.

View munguial's full-sized avatar

Alberto Munguia munguial

View GitHub Profile
class Solution(object):
def diffWaysToCompute(self, input):
memo = {}
return self.buildTrees(input, memo)
def buildTrees(self, s, memo):
if s in memo:
return memo[s]
response = []
isOperand = True
@munguial
munguial / remove-vowels-from-string.py
Created April 1, 2020 05:06
Leetcode - Remove vowels from string
class Solution:
def removeVowels(self, S: str) -> str:
vowels = ['a', 'e', 'i', 'o', 'u']
result = ""
for x in S:
if x not in vowels:
result += x
return result
@munguial
munguial / SingleNumber.py
Last active April 1, 2020 07:48
Day 1 - Single number
# https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3283/
class Solution:
def singleNumber(self, nums: List[int]) -> int:
result = 0
for number in nums:
result ^= number
return result
@munguial
munguial / Solution.py
Created April 2, 2020 16:41
Day 2 - Happy number
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3284/
class Solution:
def isHappy(self, n: int) -> bool:
knownNumbers = {}
while True:
if n == 1:
return True
if n in knownNumbers:
@munguial
munguial / Solution.py
Last active April 3, 2020 18:41
Day 3 - Maximum subarray
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3285/
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
maxi = nums[0]
tempSum = nums[0]
for n in nums[1:]:
if tempSum < 0:
tempSum = n
else:
@munguial
munguial / Solution.py
Created April 4, 2020 07:36
Day 4 - Move zeroes
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
def swap(a: int, b: int, nums: List[int]) -> None:
temp = nums[a]
nums[a] = nums[b]
nums[b] = temp
lastNonZeroPos = 0
for i in range(len(nums)):
if nums[i] != 0:
@munguial
munguial / Solution.py
Last active April 5, 2020 21:40
Day 5 - Best time to buy and sell stock II
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3287/
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
for i in range(1, len(prices)):
if prices[i] > prices[i- 1]:
profit += prices[i] - prices[i - 1]
return profit
@munguial
munguial / Solution.py
Last active April 6, 2020 19:39
Day 6 - Group anagrams
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3288/
from collections import defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = defaultdict(list)
for s in strs:
anagrams[str(sorted(s))].append(s)
return anagrams.values()
@munguial
munguial / Program.py
Created April 7, 2020 06:25
Max sum of K numbers taken from beginning or end of the array
import unittest
class Program:
def maxSubArrayWithKElements(self, array, k):
j = len(array) - k
currSum = minSum = sum(i for i in array[:j])
for i in range(j, len(array)):
currSum += array[i]
@munguial
munguial / Solution.py
Created April 7, 2020 07:08
Day 7 - Counting elements
# https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3289/
class Solution:
def countElements(self, arr: List[int]) -> int:
s = set(arr)
c = 0
for n in arr:
if n + 1 in s:
c += 1
return c