This file contains 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
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
type Fetcher interface { | |
// Fetch returns the body of URL and | |
// a slice of URLs found on that page. |
This file contains 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 Counter | |
from itertools import product | |
class Solution: | |
def fourSumCount(self, *lists): | |
k = len(lists) | |
sum_to_count = Counter(sum(nums) for nums in product(*lists[:k // 2])) | |
return sum(sum_to_count[-sum(nums)] for nums in product(*lists[k // 2:])) |
This file contains 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
import functools | |
class TextEditor: | |
def __init__(self): | |
self.text = '' | |
self.left = 0 | |
self.right = 0 | |
self.clipboard = '' | |
self.operation_stack = OperationStack() |
This file contains 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 deque | |
from functools import wraps | |
class QueueStack: | |
def __init__(self): | |
self.q = deque() | |
def __repr__(self): | |
return f'QueueStack(q={self.q})' |
This file contains 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 deque | |
from itertools import chain | |
def diagonal_walk(mat): | |
m, n = len(mat), len(mat[0]) | |
diagonals = [deque() for _ in range(m + n - 1)] | |
for i in range(m): | |
for j in range(n): | |
if (i + j) % 2 == 0: | |
diagonals[i + j].appendleft(mat[i][j]) | |
else: |
This file contains 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
/* char_map.h */ | |
#ifndef CHAR_MAP_H | |
#define CHAR_MAP_H | |
typedef struct char_map CharMap; | |
struct char_map { | |
char wall; | |
char corridor; | |
char path; | |
char entrance; |
This file contains 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 itertools import chain | |
from collections import namedtuple | |
from heapq import heappop, heappush | |
# The heapq module maintains a min-heap invariant, so we have to use | |
# the negative of the buildings' height because we'd like a max-heap | |
Building = namedtuple('Building', ['neg_height', 'end']) | |
Line = namedtuple('Line', ['start', 'height']) | |
# buildings[i] = [left_i, right_i, height_i] | |
def get_skyline(buildings): |
This file contains 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 n_queens(n): | |
def solve(): | |
i = len(columns) | |
if i == n: | |
solutions.append([f"{'.' * col}Q{'.' * (n - col - 1)}" for col in columns]) | |
return | |
for j in range(n): | |
if is_legal(i, j): | |
add_candidate(i, j) | |
solve() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 spiral_order(matrix): | |
output = [] | |
if matrix is None: | |
return output | |
# Copy the matrix to avoid modifying the input | |
matrix = [line[:] for line in matrix] | |
while matrix: | |
output.extend(matrix[0]) | |
del matrix[0] | |
# Transpose the matrix, and then reverse its rows |
NewerOlder