Skip to content

Instantly share code, notes, and snippets.

from sys import getsizeof
from timeit import timeit
bitset = 0
N = 100000
def populate_bitset():
global bitset
@alxfv
alxfv / backspace_string_compare.py
Last active April 9, 2020 09:06
Backspace String Compare
class Solution:
def backspaceCompare(self, *all_strings) -> bool:
def next_index(s: str, i: int) -> int:
"""
Get the index of the next undeleted letter
@param R: str string
@param i: index
@return: current letter index
"""
@alxfv
alxfv / backspace_string_compare.py
Created April 9, 2020 08:33
Backspace String Compare
class Solution:
def backspaceCompare(self, S: str, T: str) -> bool:
def next_index(R: str, i: int) -> int:
"""
Get the index of the next undeleted letter
@param R: str string
@param i: index
@return: current letter index
"""
@alxfv
alxfv / pypy2_competitive_template.py
Created April 4, 2020 14:01
PyPy2 Python Competitive Template: Fast IO + unittest
# <editor-fold desc="Fast IO">
# !/usr/bin/env python
from __future__ import division, print_function
import os
import random
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
def isValid(node, floor, ceiling):
if not node:
return True
if not floor < node.val < ceiling:
return False
return isValid(node.left, floor, node.val) and isValid(node.right, node.val, ceiling)
class Solution:
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
prefix_sum = [0] * (n + 1)
# 1. Build prefix sum array
# for booking [1, 3, 5] and n = 5 prefix sum array:
# [0, 0, 0, 0, 0] -> [5, 0, 0, -5, 0, 0]
for lo, hi, seats in bookings:
prefix_sum[lo - 1] += seats
import sys
def read_line():
return sys.stdin.readline().split()
def read_int_iter():
return map(int, read_line())
@alxfv
alxfv / producer-consumer.go
Created April 13, 2019 15:39
Producer-consumer in Golang
package main
import (
"fmt"
"math/rand"
"time"
)
const (
N = 20
def oswalk(dirname):
if not os.path.isdir(dirname):
return
names = os.listdir(dirname)
filenames = []
dirnames = []
dirpaths = []
@alxfv
alxfv / island_count.py
Created November 1, 2018 09:18
Island Count
def mark_island(grid, i, j):
if i >= 0 and j >= 0 and i < len(grid) and j < len(grid[i]) and grid[i][j] == 1:
grid[i][j] = 0
mark_island(grid, i, j+1)
mark_island(grid, i+1, j)
mark_island(grid, i, j-1)
mark_island(grid, i-1, j)
def get_number_of_islands(grid):