Skip to content

Instantly share code, notes, and snippets.

View EfrainReyes's full-sized avatar

Efrain Reyes EfrainReyes

View GitHub Profile
@EfrainReyes
EfrainReyes / online_stock_span.py
Created November 9, 2022 01:02
901. Online Stock Span
class StockSpanner:
def __init__(self):
self.span_stack = []
def next(self, price: int) -> int:
span = 1
while self.span_stack and price >= self.span_stack[-1][0]:
span += self.span_stack.pop()[1]
@EfrainReyes
EfrainReyes / nested_list_weight_sum.py
Created November 9, 2022 02:36
339. Nested List Weight Sum
class Solution:
def depthSum(self, nestedList: List[NestedInteger]) -> int:
def depth_sum_recursive(current_item, depth):
if current_item.isInteger():
return current_item.getInteger() * depth
return sum(depth_sum_recursive(item, depth + 1) for item in current_item.getList())
return sum(depth_sum_recursive(item, 1) for item in nestedList)
@EfrainReyes
EfrainReyes / remove_all_adjacent_duplicates_in_string.py
Created November 10, 2022 01:05
1047. Remove All Adjacent Duplicates In String
class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for char in s:
if stack and stack[-1] == char:
stack.pop()
else:
stack.append(char)
@EfrainReyes
EfrainReyes / remove_duplicates_from_sorted_array.py
Last active November 11, 2022 01:17
26. Remove Duplicates from Sorted Array (Python)
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums) == 1:
return 1
non_dupes_ptr = 1
for num in nums:
if nums[non_dupes_ptr - 1] < num:
nums[non_dupes_ptr] = num
@EfrainReyes
EfrainReyes / remove_duplicates_from_sorted_array.cs
Created November 11, 2022 01:17
26. Remove Duplicates from Sorted Array (C#)
public class Solution {
public int RemoveDuplicates(int[] nums) {
if (nums.Length == 0)
return 0;
var currentModifiableIndex = 1;
var lastModifiedValue = nums[0];
for (var i = 0; i < nums.Length; i++) {
var currentValue = nums[i];
@EfrainReyes
EfrainReyes / find_median_from_data_stream.py
Created November 12, 2022 16:53
295. Find Median from Data Stream
class MedianFinder:
def __init__(self):
self.data = []
def addNum(self, num: int) -> None:
bisect.insort(self.data, num)
def findMedian(self) -> float:
data_length = len(self.data)