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.prerequisite_for = set() | |
def can_complet_course(self): | |
return self.num_of_prerequisites == 0 | |
class Solution: | |
def canFinish(self, numCourses: int, prerequisites): |
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): | |
arr = list(range(1,n+1)) | |
while len(arr) !=1: | |
arr [1::2] | |
arr.reverse() | |
return arr.pop() |
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 | |
max_sequence = [] | |
for i in range(len(nums)): | |
max_sequence.append(1) | |
for j in range(i): | |
if nums[j] < nums[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
def knapsack(vals, weights, limit): | |
table = [[0 for w in range(limit + 1)]] | |
for j in range(1, len(vals)+1): | |
curr_item_max_value = [0] | |
for w in range(1, limit + 1): | |
if weights[j-1] > w: | |
curr_item_max_value.append(table[j-1][w]) | |
else: | |
curr_item_max_value.append(max(table[j-1][w], table[j-1][w - weights[j-1]] + vals[j-1])) | |
table.append(curr_item_max_value) |
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_in_pairs(n): | |
if n < 10: | |
return n | |
last_dig = n % 10 | |
before_last = (n % 100) // 10 | |
return (last_dig * 10 + before_last) + 100 * flip_in_pairs(n // 100) |