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 Course: | |
def __init__(self): | |
self.num_of_prerequisites = 0 | |
self.prerequisites_for = set() | |
def can_complete_course(self): | |
return self.num_of_prerequisites == 0 | |
def can_finish(num_cources , prerequiesities): | |
graph = defaultdict(Course) |
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 arg_max_and_max(self, lst): | |
maximum, arg_max = float("-inf"), -1 | |
for i, num in enumerate(lst): | |
if num >= maximum: | |
maximum, arg_max = num, i | |
return maximum, arg_max | |
def find132pattern(self, nums): | |
if len(nums) < 3: | |
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
def last_remaining(n): | |
def backtrack(n, left_to_right): | |
if n == 1: | |
return 1 | |
if left_to_right: | |
return 2 * backtrack(n//2, False) | |
if n % 2 == 1: | |
return 2 * backtrack(n//2, True) | |
else: | |
return 2 * backtrack(n//2, False) - 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
def last_remaining(n): | |
def backtrack(n, left_to_right): | |
if n == 1: | |
return 1 | |
if left_to_right: | |
return 2 * backtrack(n//2, False) | |
if n % 2 == 1: | |
return 2 * backtrack(n//2, True) | |
else: | |
return 2 * backtrack(n//2, False) - 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 lengthOfLIS(self, nums: List[int]) -> int: | |
if not nums: | |
return 0 | |
dp = [] | |
for i in range(len(nums)): | |
dp.append(1) | |
for j in range(len(nums)): | |
if nums[j] < nums[i]: | |
dp[i] = max(dp[i], dp[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
def flip_positive_number(num): | |
if num < 10: | |
return num | |
last_digit = num % 10 | |
befor_last = num % 100 // 10 | |
return 100 * flip_positive_number(num // 100) + 10 * last_digit + befor_last |