algorithmzuo / algorithmbasic2020 Goto Github PK
View Code? Open in Web Editor NEW算法和数据结构体系学习班
算法和数据结构体系学习班
没有问题,我看错了
while (L < R) 这里应该是 while (L <= R) ,老师可以测试一下
https://github.com/algorithmzuo/algorithmbasic2020/blob/master/src/class10/Code01_UnionFind.java
中,这部分的代码:
public UnionSet(List<V> values) {
for (V cur : values) {
Node<V> node = new Node<>(cur);
nodes.put(cur, node);
parents.put(node, node);
sizeMap.put(node, 1);
}
}
构造函数中需要先初始化
nodes
parents
sizeMap
这三个表,否则会报空指针异常。
即改成如下:
public UnionSet(List<V> values) {
nodes = new HashMap<>();
parents = new HashMap<>();
sizeMap = new HashMap<>();
for (V cur : values) {
Node<V> node = new Node<>(cur);
nodes.put(cur, node);
parents.put(node, node);
sizeMap.put(node, 1);
}
}
因为每次只会加入一个节点,所以平衡因子的绝对值只会多一个
建议将类型修改为 long 或是取模
对于 10,10 的用例,目前输出是 886008608
在将 ways3 的数据类型修改为 long 后
public static long ways3(int N, int M) {
if (N < 1 || M < 1 || ((N * M) & 1) != 0) {
return 0;
}
if (N == 1 || M == 1) {
return 1;
}
int max = Math.max(N, M);
int min = Math.min(N, M);
int pre = (1 << min) - 1;
long[][] dp = new long[1 << min][max + 1];
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
dp[i][j] = -1;
}
}
return process3(pre, 0, max, min, dp);
}
public static long process3(int pre, int i, int N, int M, long[][] dp) {
if (dp[pre][i] != -1) {
return dp[pre][i];
}
long ans = 0;
if (i == N) {
ans = pre == ((1 << M) - 1) ? 1 : 0;
} else {
int op = ((~pre) & ((1 << M) - 1));
ans = dfs3(op, M - 1, i, N, M, dp);
}
dp[pre][i] = ans;
return ans;
}
public static long dfs3(int op, int col, int level, int N, int M, long[][] dp) {
if (col == -1) {
return process3(op, level + 1, N, M, dp);
}
long ans = 0;
ans += dfs3(op, col - 1, level, N, M, dp);
if (col > 0 && (op & (3 << (col - 1))) == 0) {
ans += dfs3((op | (3 << (col - 1))), col - 2, level, N, M, dp);
}
return ans;
}
输出为 258584046368
看到class22中的最小硬币数问题优化完是一个二维数组,但是在leetcode 322上看到可以压缩为一维数组,请问老师可以给出一份压缩为一维数组的代码嘛
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.