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
# 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) |
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
# 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)) |
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
# 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: |
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
# 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 | |
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
# 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) |
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
// 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; |
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
# 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]]): |
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
# 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 |
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
# 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)): |
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
# 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 |