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): | |
g = 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
def next_greater_element(nums): | |
ans = [-1] * len(nums) | |
stack = [] | |
for i in reversed(range(len(nums))): | |
while stack and stack[-1] <= nums[i]: | |
stack.pop() | |
if stack: | |
ans[i] = stack[-1] | |
stack.append(nums[i]) | |
return ans |
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: int) -> int: | |
head = 1 | |
step = 1 | |
left = True | |
remaining = n | |
while remaining > 1: | |
if left or remaining % 2 == 1: | |
head += step | |
remaining //= 2 |
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): | |
if not nums: | |
return 0 | |
longest_by_end = [] | |
for i in range(len(nums)): | |
longest_by_end.append(1) | |
for j in range(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 lengthOfLIS(self, nums): | |
if not nums: | |
return 0 | |
tails = [] | |
for num in nums: | |
if not tails or num > tails[-1]: | |
tails.append(num) | |
else: | |
l, r = 0, len(tails) - 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 singleNumber(nums): | |
ones = 0 | |
twos = 0 | |
for num in nums: | |
ones = (ones ^ num) & ~twos | |
twos = (twos ^ num) & ~ones | |
return ones |
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_pairs(num): | |
if num < 10: | |
return num | |
last_digit = num % 10 | |
second_last_digit = (num // 10) % 10 | |
swapped_last_two = last_digit * 10 + second_last_digit | |
remaining = num // 100 | |
return flip_pairs(remaining) * 100 + swapped_last_two |
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
public int singleNumber(int[] nums) { | |
int ones = 0, twos = 0; | |
for (int num : nums) { | |
ones = (ones ^ num) & ~twos; | |
twos = (twos ^ num) & ~ones; | |
} | |
return ones; | |
} |