- Forgetting Go 😕
- Training Python 🥱
- Picking up Java 😵💫
Drown by C++ 🫠Is JavaScript the best programming language to become a full-stack developer 🤔
- Python3 template and tricks 🤫
- Weekly contest tool 🔥
- Try it at here 🔬:
print(inspect.getsource(customfunction.__class__))
- Typically:
- Java is most likely to AC a problem with brute force 🙃.
- Python can AC many string-related problems with time complexity that cannot be passed 😅.
- You can import any Go library, the download time won't be counted in the testing time 😈.
Algorithms for Competitive Programming (translate from http://e-maxx.ru/algo/)
LCCN - Medal and achievement rules
LCCN - Weekly contest rating algorithm
LCUS - Weekly contest rating algorithm
LCCN - Histroy of Guardian / Knight score
isbn: 978-7-83009-313-6
-
math.lcm(*integers)
, New in version 3.9, If any of the arguments is zero, then the returned value is 0. lcm() without arguments returns 1. -
@functools.cache(user_function)
, New in version 3.9, Returns the same as lru_cache(maxsize=None). -
int.bit_count()
, New in version 3.10, Equivalent to:bin(self).count("1")
. -
itertools.pairwise(iterable)
, New in version 3.10, list(pairwise('a')) -> [], list(pairwise('abc')) -> [('ab', 'bc')]. -
itertools.accumulate(iterable[, func, *, initial=None])
, Changed in version 3.8, if the keyword argumentinitial
is provided, the output has one more element than the input iterable. Seefunctools.reduce()
for a similar function that returns only the FINAL accumulated value. -
collections.Counter(a) > collections.Counter(b)
, New in version 3.10, All of those tests treat missing elements as having zero counts so that Counter(a=1) == Counter(a=1, b=0) returns true. -
c = Counter(a=3, b=1); d = Counter(a=1, b=2); # c - d -> Counter({'a': 2})
, New in version 3.10, Counter() subtract ONLY keep positive counts. if we want to include negative values, useobj1.subtract(obj2)
. -
x != y != z
behaves asx != y and y != z
, instead ofx != b and x != z and b != z
, sox < y > z
is possible, 6.10. Comparisons. -
quotes in type hint,
def lca(root: 'TreeNode')
, PEP 484. -
Type aliases, New in version 3.10. Like
type null = None
will causeType alias statement requires Python 3.12 or newer
. PEP 613. How to deal with it in Py 3.8? Usetyping_extension.TypeAlias
. typing-extensions - PyPI -
NoneType
- In Python 2.7,
NoneType
is a type defined in the standard library module types - In Python 3.0 to 3.9,
NoneType
has been removed from moduletypes
, presumably because there is only a single value of this type. It effectively exists nevertheless, it only has no built-in name: You can access NoneType by writingtype(None)
. - In Python 3.10+,
NoneType
is again a type defined in the standard library moduletypes
, introduced in order to help type checkers do their work
- In Python 2.7,
var
:- Java SE 10 introduced type inference for local variables. With type inference, the explicit type can be replaced by the reserved type name var for local variable declarations that have initializers.
- You can use
var
only for local variables (in methods). It cannot be used for instance variables (at class level). - cannot use
var
in Lambda expressions. - cannot use
var
for method signatures (in return types and parameters). - cannot use it with a generic type.
x += s.charAt(i) - 'a';
is much faster thanx += s.charAt(i) == 'b' ? 1 : 0;
System.arraycopy
vsArrays.copyOf
?native
keyword shows thatSystem.arraycopy
is implemented by other languages like C/C++ (fast)Arrays.copyOf
will create a new item.System.arraycopy
only copy from src to dst.Arrays.copyOf
will callSystem.arraycopy
, so it is a bit slow.
static
- when we declare a field static, exactly a single copy of that field is created and shared among all instances of that class (access static fields without object initialization!)
- static variables are stored in the heap memory (fast)
ArrayList
vsLinkedList
?ArrayList
: implements it with a dynamically re-sizing array, O(1) accessing, O(n) addingLinkedList
: implements it with a doubly-linked list. O(n) accessing, O(n) adding. Memory costing. So why not useArrayDeque
?-
Algorithm ArrayList LinkedList seek front O(1) O(1) seek back O(1) O(1) seek to index O(1) O(N) insert at front O(N) O(1) insert at back O(1) O(1) insert after an item O(N) O(1)
- print List:
- System.out.println(Arrays.toString()), which will call String.valueOf()
- System.out.println(Arrays.asList(intArray));
- Stream.of(target).forEach(System.out::println);
- Arrays.stream(target).forEach(System.out::println);
- Arrays.asList(target).stream().forEach(s -> System.out.println(s));
- System.out.println(Arrays.deepToString(twoDimensionalArray));
The O is short for “Order of”. If we’re discussing an algorithm with O(n), we say its order of, or rate of growth, is n, or linear complexity.
O | Complexity |
---|---|
O(1) | constant |
O(log n) | logarithmic |
O(n) | linear |
O(n * log n) | log linear |
O(n^2) | quadratic |
O(n^3) | cubic |
O(2^n) | exponential |
O(n!) | factorial |
大佬们的经验之谈:
-
MOD:
- 不取余python超时
- dp中有减法, 负数 x 取余, 防止变一个大数:
(x + MOD) % MOD
- 区别:
- 取余(rem): 采用fix(), 向 0 方向舍入,
rem(x, y) = x - y. * fix(x./y)
- 取模(mod): 采用floor(), 向无穷小方向舍入,
mod(x, y) = x - y. * floor(x./y)
- 取模和求余应该是同一种运算, 只是在被除数和除数符号不同时, 余数的符号是有歧义的, 可为正也可为负
- C, Go, JavaScript, Rust, Java, Swift, PHP中结果与被除数同符号
- Python 中结果与除数同符号
- 取余(rem): 采用fix(), 向 0 方向舍入,
-
精度:
- 32 位整数开方的整数部分是准确的
- 64 位开方可能得到类似 xxx.9999999 的结果
-
回溯:
- 两重for循环, 内层for break之后, 回溯"路径"被打断了, 不能复原到初始状态
-
python堆 / heapq / 堆不保证直接list的顺序
# 不断push tuple 到堆中 0 [(5, 0)] 0 [(5, 0), (5, 0)] 1 [(4, 1), (5, 0), (5, 0)] 2 [(3, 2), (4, 1), (5, 0), (5, 0)] 3 [(2, 3), (3, 2), (5, 0), (5, 0), (4, 1)] 3 [(2, 3), (3, 2), (2, 3), (5, 0), (4, 1), (5, 0)] 4 [(1, 4), (3, 2), (2, 3), (5, 0), (4, 1), (5, 0), (2, 3)] 7 [(1, 4), (2, 7), (2, 3), (3, 2), (4, 1), (5, 0), (2, 3), (5, 0)] 7 [(1, 4), (2, 7), (2, 3), (2, 7), (4, 1), (5, 0), (2, 3), (5, 0), (3, 2)]
-
XOR (exclusive OR)
- 半加运算,不带进位的二进制加法
- 与0异或 = 本身
- 与1异或 = 取反 -> 翻转特定位
- 翻转10100001的第2位和第3位 -> 10100001 ^ 00000110 = 10100111
- 异或自己 = 置0
- a ^ b ^ a = b (^ caret)
- 半加运算,不带进位的二进制加法
-
位运算(整数在计算机中是以补码的形式储存的)
- 求整数二进制的最低位1
n & (-n)
: 任何整数,其二进制表示的最后一个'1',可由该整数与其相反数按位取与得到n & (n-1)
: 二进制数字 n 最右边的 1 变成 0, 其余不变, 消去二进制数中的最低位'1', Why: 'n-1'会一直向前寻找可借的位,从而跳过低位连续的'0',而向最低位的'1'借位,借位后最低位的'1'变为'0',原先最低位'1'的下一位从'0'变为'1',其余位都不变,相与之后其它位不变n > 0 && n & (n - 1) == 0
判断是否是'2'的幂-
upper, lower exchange: asc ^= 32 upper, lower to lower: asc |= 32 lower, upper to upper: asc &= -33
- 求整数二进制的最低位1
-
Py Fast Reader
# cf 快读 fn = sys.stdin.readline l = int(fn()) for _ in range(l): n = int(fn()) h = list(map(int, fn().split()))