Skip to content

Instantly share code, notes, and snippets.

View munguial's full-sized avatar

Alberto Munguia munguial

View GitHub Profile
@munguial
munguial / Solution.py
Created July 19, 2020 20:22
July - Day 18 - Course Schedule II
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3394/
from typing import Dict, Set
from collections import defaultdict
class Solution:
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
G = self.buildGraph(numCourses, prerequisites)
self.cyclic = False
res = self.topologicalSort(numCourses, G)
@munguial
munguial / Solution.py
Created July 17, 2020 20:57
July - Day 17 - Top K Frequent Elements
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3393/
from heapq import heappush, heappop
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
counter = collections.Counter(nums)
h = []
for v, c in counter.items():
heappush(h, (c, v))
@munguial
munguial / Solution.py
Created July 16, 2020 16:50
July - Day 16 - Pox(x, n)
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3392/
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n == 1:
return x
if n < 0:
@munguial
munguial / Solution.py
Created July 15, 2020 18:13
July - Day 15 - Reverse Words in a String
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3391/
class Solution:
def reverseWords(self, s: str) -> str:
def reverse(i: int, j: int):
while i < j:
s1[i], s1[j] = s1[j], s1[i]
i += 1
j -= 1
@munguial
munguial / Solution.py
Created July 15, 2020 06:30
July - Day 12 - Reverse Bits
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/545/week-2-july-8th-july-14th/3388/
class Solution:
def reverseBits(self, n: int) -> int:
res = 0
i = 0
while i < 32:
if (1 << i) & n:
res |= 1 << (31 - i)
@munguial
munguial / Solution.java
Created July 15, 2020 06:14
July - Day 13 - Same Tree
// https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/545/week-2-july-8th-july-14th/3389/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null)
return true;
if(p == null || q == null)
return false;
@munguial
munguial / Solution.py
Created July 15, 2020 06:12
July - Day 11 - Subsets
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/545/week-2-july-8th-july-14th/3387/
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = []
array = []
self.dfs(nums, 0, array, res)
return res
def dfs(self, nums: List[int], i: int, arr: List[int], res: List[List[int]]):
@munguial
munguial / Solution.py
Created July 15, 2020 05:45
July - Day 8 - Three Sum
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/545/week-2-july-8th-july-14th/3384/
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
res = set()
nums.sort()
for i in range(len(nums)):
if nums[i] > 0:
continue
@munguial
munguial / Solution.py
Created July 15, 2020 05:03
July - Day 7 - Island Perimeter
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3383/
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
def isInvalid(i: int, j: int) -> bool:
return i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0])
res = 0
for i in range(len(grid)):
@munguial
munguial / Solution.py
Created July 15, 2020 04:52
July - Day 6 - Plus One
# https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3382/
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
i = len(digits) - 1
carry = 1
res = []
while i >= 0 or carry:
value = digits[i] + carry if i >= 0 else carry