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 bisect | |
''' | |
1. sort the array | |
2. iterate i from 0 to n-3 | |
3. j and k as another two rectangle point, j set to i+1, k set to i+2 | |
4. if a[i] + a[j] >= a[k], move k forward | |
5. if a[i] + a[j] < a[k], [j+1,...k-1] is legal rectangle, | |
nums += k-j-1, then move j forward | |
6. if k equal to n, [i, [j, ..., n-2], n-1] could be rectangle |
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
''' | |
Given integer array and integer k, return all possible equation that array can be calculated to k. | |
Operations can be +, -, *, /, () | |
1. searchRecursive is the solution that the relative position of array can not be changed | |
2. search2Recursive is the solution that the relative position can be changed | |
''' | |
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 bisect import bisect_left | |
def countTriangleTripleIndex(a): | |
a.sort() | |
n, count = len(a), 0 | |
for i in range(n-2): | |
j = i+1 | |
k = bisect_left(a, a[i]+a[j])-1 | |
while k < n: |
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
#include <iostream> | |
using namespace std; | |
void removeContinuous(string &s) { | |
int n = s.size(); | |
int i = 1, j = 0; | |
while (i < n) { | |
if (s[i] != s[j]) { | |
s[++j] = s[i]; |
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
# -*- coding: utf-8 -*- | |
import random | |
import sys | |
def random_select(a): | |
ret = a[0] | |
for i in range(1, len(a)): | |
j = random.randint(0, i) | |
if j == i: |
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 heapq | |
class Vertex: | |
def __init__(self, val): | |
self.val = val | |
self.adjs = {} | |
def add_edge(self, j, val): | |
self.adjs[j] = val |
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
ALPHABETSIZE = 26 | |
def get_index(c): | |
return ord(c)-ord('a') | |
class PostfixTreeNode: |
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
ALPHABETSIZE = 26 | |
def get_index(c): | |
return ord(c)-ord('a') | |
class PostfixNode: | |
def __init__(self): |
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
class TreeNode: | |
def __init__(self, val): | |
self.children, self.val = [], val | |
class PostorderIterator: | |
def __init__(self, root): | |
self.st = [] |
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
ef add(self, val): | |
if self.size >= self.m * self.n: | |
return False | |
i, j = self.m-1, self.n-1 | |
self.mat[i][j] = val | |
while True: | |
ni, nj = i, j | |
if i > 0 and self.mat[i-1][j] > self.mat[ni][nj]: | |
ni, nj = i-1, j | |
if j > 0 and self.mat[i][j-1] > self.mat[ni][nj]: |