From 2338b350392b998e0ca155d9fbd4bc71c2d4858a Mon Sep 17 00:00:00 2001 From: krahets Date: Fri, 21 Jul 2023 15:14:40 +0800 Subject: [PATCH] build --- .../space_complexity.md | 2 +- .../binary_search_recur.md | 29 ++- chapter_graph/graph_operations.md | 157 ++++++------ chapter_graph/graph_traversal.md | 74 +++++- chapter_greedy/fractional_knapsack_problem.md | 26 +- chapter_greedy/greedy_algorithm.md | 6 +- chapter_greedy/index.md | 1 + chapter_greedy/max_capacity_problem.md | 227 ++++++++++++++++++ chapter_tree/array_representation_of_tree.md | 88 ++++++- 9 files changed, 503 insertions(+), 107 deletions(-) create mode 100644 chapter_greedy/max_capacity_problem.md diff --git a/chapter_computational_complexity/space_complexity.md b/chapter_computational_complexity/space_complexity.md index 30d622e17..cba009a9f 100755 --- a/chapter_computational_complexity/space_complexity.md +++ b/chapter_computational_complexity/space_complexity.md @@ -375,7 +375,7 @@ comments: true int a = 0; // O(1) int b[10000]; // O(1) if (n > 10) - vector nums(n); // O(n) + int nums[n] = {0}; // O(n) } ``` diff --git a/chapter_divide_and_conquer/binary_search_recur.md b/chapter_divide_and_conquer/binary_search_recur.md index 6fd5e9910..fb862ebeb 100644 --- a/chapter_divide_and_conquer/binary_search_recur.md +++ b/chapter_divide_and_conquer/binary_search_recur.md @@ -128,9 +128,34 @@ status: new === "Go" ```go title="binary_search_recur.go" - [class]{}-[func]{dfs} + /* 二分查找:问题 f(i, j) */ + func dfs(nums []int, target, i, j int) int { + // 如果区间为空,代表没有目标元素,则返回 -1 + if i > j { + return -1 + } + // 计算索引中点 + m := i + ((j - i) >> 1) + //判断中点与目标元素大小 + if nums[m] < target { + // 小于则递归右半数组 + // 递归子问题 f(m+1, j) + return dfs(nums, target, m+1, j) + } else if nums[m] > target { + // 小于则递归左半数组 + // 递归子问题 f(i, m-1) + return dfs(nums, target, i, m-1) + } else { + // 找到目标元素,返回其索引 + return m + } + } - [class]{}-[func]{binarySearch} + /* 二分查找 */ + func binarySearch(nums []int, target int) int { + n := len(nums) + return dfs(nums, target, 0, n-1) + } ``` === "JavaScript" diff --git a/chapter_graph/graph_operations.md b/chapter_graph/graph_operations.md index 77b4a9535..252cde054 100644 --- a/chapter_graph/graph_operations.md +++ b/chapter_graph/graph_operations.md @@ -575,32 +575,38 @@ comments: true ```c title="graph_adjacency_matrix.c" /* 基于邻接矩阵实现的无向图类结构 */ struct graphAdjMat { - int *vertices; - unsigned int **adjMat; - unsigned int size; - unsigned int capacity; + int *vertices; // 顶点列表 + unsigned int **adjMat; // 邻接矩阵,元素代表“边”,索引代表“顶点索引” + unsigned int size; // 顶点数量 + unsigned int capacity; // 图容量 }; typedef struct graphAdjMat graphAdjMat; /* 添加边 */ + // 参数 i, j 对应 vertices 元素索引 void addEdge(graphAdjMat *t, int i, int j) { // 越界检查 if (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) { printf("Out of range in %s:%d\n", __FILE__, __LINE__); exit(1); } + // 添加边 + // 参数 i, j 对应 vertices 元素索引 t->adjMat[i][j] = 1; t->adjMat[j][i] = 1; } /* 删除边 */ + // 参数 i, j 对应 vertices 元素索引 void removeEdge(graphAdjMat *t, int i, int j) { // 越界检查 if (i < 0 || j < 0 || i >= t->size || j >= t->size || i == j) { printf("Out of range in %s:%d\n", __FILE__, __LINE__); exit(1); } + // 删除边 + // 参数 i, j 对应 vertices 元素索引 t->adjMat[i][j] = 0; t->adjMat[j][i] = 0; } @@ -609,13 +615,13 @@ comments: true void addVertex(graphAdjMat *t, int val) { // 如果实际使用不大于预设空间,则直接初始化新空间 if (t->size < t->capacity) { - t->vertices[t->size] = val; + t->vertices[t->size] = val; // 初始化新顶点值 - // 邻接矩新列阵置0 + for (int i = 0; i < t->size; i++) { - t->adjMat[i][t->size] = 0; + t->adjMat[i][t->size] = 0; // 邻接矩新列阵置0 } - memset(t->adjMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); + memset(t->adjMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // 将新增行置 0 t->size++; return; } @@ -637,23 +643,21 @@ comments: true tempMat[k] = tempMatLine + k * (t->size * 2); } - // 原数据复制到新数组 for (int i = 0; i < t->size; i++) { - memcpy(tempMat[i], t->adjMat[i], sizeof(unsigned int) * t->size); + memcpy(tempMat[i], t->adjMat[i], sizeof(unsigned int) * t->size); // 原数据复制到新数组 } - // 新列置0 for (int i = 0; i < t->size; i++) { - tempMat[i][t->size] = 0; + tempMat[i][t->size] = 0; // 将新增列置 0 } - memset(tempMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); + memset(tempMat[t->size], 0, sizeof(unsigned int) * (t->size + 1)); // 将新增行置 0 // 释放原数组 free(t->adjMat[0]); free(t->adjMat); // 扩容后,指向新地址 - t->adjMat = tempMat; + t->adjMat = tempMat; // 指向新的邻接矩阵地址 t->capacity = t->size * 2; t->size++; } @@ -665,28 +669,21 @@ comments: true printf("Out of range in %s:%d\n", __FILE__, __LINE__); exit(1); } - - // 清除删除的顶点,并将其后所有顶点前移 for (int i = index; i < t->size - 1; i++) { - t->vertices[i] = t->vertices[i + 1]; + t->vertices[i] = t->vertices[i + 1]; // 清除删除的顶点,并将其后所有顶点前移 } - - // 将被前移的最后一个顶点置0 - t->vertices[t->size - 1] = 0; + t->vertices[t->size - 1] = 0; // 将被前移的最后一个顶点置 0 // 清除邻接矩阵中删除的列 for (int i = 0; i < t->size - 1; i++) { if (i < index) { - // 被删除列后的所有列前移 for (int j = index; j < t->size - 1; j++) { - t->adjMat[i][j] = t->adjMat[i][j + 1]; + t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移 } - } else { - // 被删除行的下方所有行上移 - memcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size); - // 被删除列后的所有列前移 + } else { + memcpy(t->adjMat[i], t->adjMat[i + 1], sizeof(unsigned int) * t->size); // 被删除行的下方所有行上移 for (int j = index; j < t->size; j++) { - t->adjMat[i][j] = t->adjMat[i][j + 1]; + t->adjMat[i][j] = t->adjMat[i][j + 1]; // 被删除列后的所有列前移 } } } @@ -724,28 +721,27 @@ comments: true } /* 构造函数 */ - graphAdjMat *newGraphic(unsigned int numberVertices, int *vertices, unsigned int **adjMat) { - // 函数指针 - graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); - + graphAdjMat *newGraphAjdMat(unsigned int numberVertices, int *vertices, unsigned int **adjMat) { // 申请内存 - newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); - newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); - unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); - newGraph->size = numberVertices; - newGraph->capacity = numberVertices * 2; + graphAdjMat *newGraph = (graphAdjMat *)malloc(sizeof(graphAdjMat)); // 为图分配内存 + newGraph->vertices = (int *)malloc(sizeof(int) * numberVertices * 2); // 为顶点列表分配内存 + newGraph->adjMat = (unsigned int **)malloc(sizeof(unsigned int *) * numberVertices * 2); // 为邻接矩阵分配二维内存 + unsigned int *temp = (unsigned int *)malloc(sizeof(unsigned int) * numberVertices * 2 * numberVertices * 2); // 为邻接矩阵分配一维内存 + newGraph->size = numberVertices; // 初始化顶点数量 + newGraph->capacity = numberVertices * 2; // 初始化图容量 // 配置二维数组 for (int i = 0; i < numberVertices * 2; i++) { - newGraph->adjMat[i] = temp + i * numberVertices * 2; + newGraph->adjMat[i] = temp + i * numberVertices * 2; // 将二维指针指向一维数组 } // 赋值 memcpy(newGraph->vertices, vertices, sizeof(int) * numberVertices); for (int i = 0; i < numberVertices; i++) { - memcpy(newGraph->adjMat[i], adjMat[i], sizeof(unsigned int) * numberVertices); + memcpy(newGraph->adjMat[i], adjMat[i], sizeof(unsigned int) * numberVertices); // 将传入的邻接矩阵赋值给结构体内邻接矩阵 } + // 返回结构体指针 return newGraph; } ``` @@ -1552,12 +1548,9 @@ comments: true ```c title="graph_adjacency_list.c" /* 基于邻接链表实现的无向图类结构 */ struct graphAdjList { - // 顶点列表 - Vertex **verticesList; - // 顶点数量 - unsigned int size; - // 当前容量 - unsigned int capacity; + Vertex **verticesList; // 邻接表 + unsigned int size; // 顶点数量 + unsigned int capacity; // 顶点容量 }; typedef struct graphAdjList graphAdjList; @@ -1569,13 +1562,13 @@ comments: true printf("Out of range in %s:%d\n", __FILE__, __LINE__); return; } - // 查找待连接的节点 - Vertex *v1 = t->verticesList[i]; - Vertex *v2 = t->verticesList[j]; + // 查找欲添加边的顶点 vet1 - vet2 + Vertex *vet1 = t->verticesList[i]; + Vertex *vet2 = t->verticesList[j]; - // 连接节点 - pushBack(v1->linked, v2); - pushBack(v2->linked, v1); + // 连接顶点 vet1 - vet2 + pushBack(vet1->linked, vet2); + pushBack(vet2->linked, vet1); } /* 删除边 */ @@ -1586,13 +1579,13 @@ comments: true return; } - // 查找待删除边的相关节点 - Vertex *v1 = t->verticesList[i]; - Vertex *v2 = t->verticesList[j]; + // 查找欲删除边的顶点 vet1 - vet2 + Vertex *vet1 = t->verticesList[i]; + Vertex *vet2 = t->verticesList[j]; - // 移除待删除边 - removeLink(v1->linked, v2); - removeLink(v2->linked, v1); + // 移除待删除边 vet1 - vet2 + removeLink(vet1->linked, vet2); + removeLink(vet2->linked, vet1); } /* 添加顶点 */ @@ -1601,16 +1594,15 @@ comments: true if (t->size >= t->capacity) { Vertex **tempList = (Vertex **)malloc(sizeof(Vertex *) * 2 * t->capacity); memcpy(tempList, t->verticesList, sizeof(Vertex *) * t->size); - free(t->verticesList); - // 指向新顶点表 - t->verticesList = tempList; - t->capacity = t->capacity * 2; + free(t->verticesList); // 释放原邻接表内存 + t->verticesList = tempList; // 指向新邻接表 + t->capacity = t->capacity * 2; // 容量扩大至2倍 } // 申请新顶点内存并将新顶点地址存入顶点列表 - Vertex *newV = newVertex(val); - newV->pos = t->size; - newV->linked = newLinklist(newV); - t->verticesList[t->size] = newV; + Vertex *newV = newVertex(val); // 建立新顶点 + newV->pos = t->size; // 为新顶点标记下标 + newV->linked = newLinklist(newV); // 为新顶点建立链表 + t->verticesList[t->size] = newV; // 将新顶点加入邻接表 t->size++; } @@ -1622,31 +1614,30 @@ comments: true exit(1); } - // 查找待删节点 - Vertex *v = t->verticesList[index]; - // 若不存在该节点,则返回 - if (v == 0) { + Vertex *vet = t->verticesList[index]; // 查找待删节点 + if (vet == 0) { // 若不存在该节点,则返回 printf("index is:%d\n", index); printf("Out of range in %s:%d\n", __FILE__, __LINE__); return; } - // 遍历待删除节点链表,将所有与待删除结点有关的边删除 - Node *temp = v->linked->head->next; + // 遍历待删除顶点的链表,将所有与待删除结点有关的边删除 + Node *temp = vet->linked->head->next; while (temp != 0) { - removeLink(temp->val->linked, v); - temp = temp->next; + removeLink(temp->val->linked, vet); // 删除与该顶点有关的边 + temp = temp->next; } - // 定点列表前移 + // 将顶点前移 for (int i = index; i < t->size - 1; i++) { - t->verticesList[i] = t->verticesList[i + 1]; + t->verticesList[i] = t->verticesList[i + 1]; // 顶点前移 + t->verticesList[i]->pos--; // 所有前移的顶点索引值减1 } - t->verticesList[t->size - 1] = 0; + t->verticesList[t->size - 1] = 0; // 将被删除顶点的位置置 0 t->size--; //释放被删除顶点的内存 - freeVertex(v); + freeVertex(vet); } /* 打印顶点与邻接矩阵 */ @@ -1668,16 +1659,16 @@ comments: true } /* 构造函数 */ - graphAdjList *newGraphic(unsigned int verticesNumber) { + graphAdjList *newGraphAdjList(unsigned int verticesCapacity) { // 申请内存 graphAdjList *newGraph = (graphAdjList *)malloc(sizeof(graphAdjList)); // 建立顶点表并分配内存 - newGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesNumber); - memset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesNumber); - // 初始化大小和容量 - newGraph->size = 0; - newGraph->capacity = verticesNumber; - return newGraph; + newGraph->verticesList = (Vertex **)malloc(sizeof(Vertex *) * verticesCapacity); // 为顶点列表分配内存 + memset(newGraph->verticesList, 0, sizeof(Vertex *) * verticesCapacity); // 顶点列表置 0 + newGraph->size = 0; // 初始化顶点数量 + newGraph->capacity = verticesCapacity; // 初始化顶点容量 + // 返回图指针 + return newGraph; } ``` diff --git a/chapter_graph/graph_traversal.md b/chapter_graph/graph_traversal.md index 33a5f1295..68f1335e2 100644 --- a/chapter_graph/graph_traversal.md +++ b/chapter_graph/graph_traversal.md @@ -219,7 +219,44 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质 === "C" ```c title="graph_bfs.c" - [class]{}-[func]{graphBFS} + /* 广度优先遍历 */ + // 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点 + Vertex **graphBFS(graphAdjList *t, Vertex *startVet) { + // 顶点遍历序列 + Vertex **res = (Vertex **)malloc(sizeof(Vertex *) * t->size); + memset(res, 0, sizeof(Vertex *) * t->size); + // 队列用于实现 BFS + queue *que = newQueue(t->size); + // 哈希表,用于记录已被访问过的顶点 + hashTable *visited = newHash(t->size); + int resIndex = 0; + queuePush(que, startVet); // 将第一个元素入队 + hashMark(visited, startVet->pos); // 标记第一个入队的顶点 + // 以顶点 vet 为起点,循环直至访问完所有顶点 + while (que->head < que->tail) { + // 遍历该顶点的边链表,将所有与该顶点有连接的,并且未被标记的顶点入队 + Node *n = queueTop(que)->linked->head->next; + while (n != 0) { + // 查询哈希表,若该索引的顶点已入队,则跳过,否则入队并标记 + if (hashQuery(visited, n->val->pos) == 1) { + n = n->next; + continue; // 跳过已被访问过的顶点 + } + queuePush(que, n->val); // 只入队未访问的顶点 + hashMark(visited, n->val->pos); // 标记该顶点已被访问 + } + // 队首元素存入数组 + res[resIndex] = queueTop(que); // 队首顶点加入顶点遍历序列 + resIndex++; + queuePop(que); // 队首元素出队 + } + // 释放内存 + freeQueue(que); + freeHash(visited); + resIndex = 0; + // 返回顶点遍历序列 + return res; + } ``` === "C#" @@ -557,9 +594,40 @@ BFS 通常借助「队列」来实现。队列具有“先入先出”的性质 === "C" ```c title="graph_dfs.c" - [class]{}-[func]{dfs} + /* 深度优先遍历 DFS 辅助函数 */ + int resIndex = 0; + void dfs(graphAdjList *graph, hashTable *visited, Vertex *vet, Vertex **res) { + if (hashQuery(visited, vet->pos) == 1) { + return; // 跳过已被访问过的顶点 + } + hashMark(visited, vet->pos); // 标记顶点并将顶点存入数组 + res[resIndex] = vet; // 将顶点存入数组 + resIndex++; + // 遍历该顶点链表 + Node *n = vet->linked->head->next; + while (n != 0) { + // 递归访问邻接顶点 + dfs(graph, visited, n->val, res); + n = n->next; + } + return; + } - [class]{}-[func]{graphDFS} + /* 深度优先遍历 DFS */ + // 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点 + Vertex **graphDFS(graphAdjList *graph, Vertex *startVet) { + // 顶点遍历序列 + Vertex **res = (Vertex **)malloc(sizeof(Vertex *) * graph->size); + memset(res, 0, sizeof(Vertex *) * graph->size); + // 哈希表,用于记录已被访问过的顶点 + hashTable *visited = newHash(graph->size); + dfs(graph, visited, startVet, res); + // 释放哈希表内存并将数组索引归零 + freeHash(visited); + resIndex = 0; + // 返回遍历数组 + return res; + } ``` === "C#" diff --git a/chapter_greedy/fractional_knapsack_problem.md b/chapter_greedy/fractional_knapsack_problem.md index 194f262d8..c5bea0ff3 100644 --- a/chapter_greedy/fractional_knapsack_problem.md +++ b/chapter_greedy/fractional_knapsack_problem.md @@ -40,17 +40,7 @@ status: new

Fig. 分数背包的贪心策略

-**第三步:正确性证明** - -采用反证法。假设物品 $x$ 是单位价值最高的物品,使用某算法求得最大价值为 $res$ ,但该解中不包含物品 $x$ 。 - -现在从背包中拿出单位重量的任意物品,并替换为单位重量的物品 $x$ 。由于物品 $x$ 的单位价值最高,因此替换后的总价值一定大于 $res$ 。**这与 $res$ 是最优解矛盾,说明最优解中必须包含物品 $x$ 。** - -对于该解中的其他物品,我们也可以构建出上述矛盾。总而言之,**单位价值更大的物品总是更优选择**,这说明贪心策略是有效的。 - -**实现代码** - -我们构建了一个物品类 `Item` ,以便将物品按照单位价值进行排序。在循环贪心选择中,分为放入整个物品或放入部分物品两种情况。当背包已满时,则跳出循环并返回解。 +我们构建了一个物品类 `Item` ,以便将物品按照单位价值进行排序。循环进行贪心选择,当背包已满时跳出并返回解。 === "Java" @@ -228,10 +218,18 @@ status: new [class]{}-[func]{fractionalKnapsack} ``` -如下图所示,如果将一个 2D 图表的横轴和纵轴分别看作物品重量和物品单位价值,则分数背包问题可被转化为“求在有限横轴区间下的最大围成面积”。这个类比可以帮助我们从几何角度清晰地看到贪心策略的有效性。 +最差情况下,需要遍历整个物品列表,**因此时间复杂度为 $O(n)$** ,其中 $n$ 为物品数量。由于初始化了一个 `Item` 对象列表,**因此空间复杂度为 $O(n)$** 。 + +**第三步:正确性证明** + +采用反证法。假设物品 $x$ 是单位价值最高的物品,使用某算法求得最大价值为 $res$ ,但该解中不包含物品 $x$ 。 + +现在从背包中拿出单位重量的任意物品,并替换为单位重量的物品 $x$ 。由于物品 $x$ 的单位价值最高,因此替换后的总价值一定大于 $res$ 。**这与 $res$ 是最优解矛盾,说明最优解中必须包含物品 $x$ 。** + +对于该解中的其他物品,我们也可以构建出上述矛盾。总而言之,**单位价值更大的物品总是更优选择**,这说明贪心策略是有效的。 + +如下图所示,如果将物品重量和物品单位价值分别看作一个 2D 图表的横轴和纵轴,则分数背包问题可被转化为“求在有限横轴区间下的最大围成面积”。这个类比可以帮助我们从几何角度清晰地看到贪心策略的有效性。 ![分数背包问题的几何表示](fractional_knapsack_problem.assets/fractional_knapsack_area_chart.png)

Fig. 分数背包问题的几何表示

- -最差情况下,需要遍历整个物品列表,**因此时间复杂度为 $O(n)$** ,其中 $n$ 为物品数量。由于初始化了一个 `Item` 对象列表,**因此空间复杂度为 $O(n)$** 。 diff --git a/chapter_greedy/greedy_algorithm.md b/chapter_greedy/greedy_algorithm.md index 2fa5d90b4..5a05c95d2 100644 --- a/chapter_greedy/greedy_algorithm.md +++ b/chapter_greedy/greedy_algorithm.md @@ -16,7 +16,7 @@ status: new !!! question - 给定 $n$ 种硬币,第 $i$ 个硬币的面值为 $coins[i - 1]$ ,目标金额为 $amt$ ,**每种硬币可以重复选取**,问能够凑出目标金额的最少硬币个数。如果无法凑出目标金额则返回 $-1$ 。 + 给定 $n$ 种硬币,第 $i$ 个硬币的面值为 $coins[i - 1]$ ,目标金额为 $amt$ ,每种硬币可以重复选取,问能够凑出目标金额的最少硬币个数。如果无法凑出目标金额则返回 $-1$ 。 贪心算法会迭代地做出一个又一个的贪心选择,每轮都将问题转化成一个规模更小的子问题,直到问题被解决。 @@ -190,8 +190,8 @@ status: new 确定贪心策略是求解问题的核心步骤,但实施起来并没有那么容易。主要有两方面原因: -1. **不同问题的贪心策略的差异较大**。对于许多问题来说,贪心策略都比较浅显,我们通过一些大概的思考与尝试就能得出。而对于一些复杂问题,贪心策略可能非常隐蔽,这种情况就非常考验个人的解题经验与算法能力了。 -2. **某些贪心策略具有较强的迷惑性**。当我们满怀信心设计好贪心策略,写出解题代码并提交运行,很可能发现部分测试样例无法通过。这是因为设计的贪心策略只是“部分正确”的,上文介绍的零钱兑换就是个很好的例子。 +- **不同问题的贪心策略的差异较大**。对于许多问题来说,贪心策略都比较浅显,我们通过一些大概的思考与尝试就能得出。而对于一些复杂问题,贪心策略可能非常隐蔽,这种情况就非常考验个人的解题经验与算法能力了。 +- **某些贪心策略具有较强的迷惑性**。当我们满怀信心设计好贪心策略,写出解题代码并提交运行,很可能发现部分测试样例无法通过。这是因为设计的贪心策略只是“部分正确”的,上文介绍的零钱兑换就是个很好的例子。 为了保证正确性,我们应该对贪心策略进行严谨的数学证明,**通常需要用到反证法或数学归纳法**。 diff --git a/chapter_greedy/index.md b/chapter_greedy/index.md index 78300d396..5c1645682 100644 --- a/chapter_greedy/index.md +++ b/chapter_greedy/index.md @@ -16,3 +16,4 @@ status: new - [15.1   贪心算法](https://www.hello-algo.com/chapter_greedy/greedy_algorithm/) - [15.2   分数背包问题](https://www.hello-algo.com/chapter_greedy/fractional_knapsack_problem/) +- [15.3   最大容量问题](https://www.hello-algo.com/chapter_greedy/max_capacity_problem/) diff --git a/chapter_greedy/max_capacity_problem.md b/chapter_greedy/max_capacity_problem.md new file mode 100644 index 000000000..6eeca896f --- /dev/null +++ b/chapter_greedy/max_capacity_problem.md @@ -0,0 +1,227 @@ +--- +comments: true +status: new +--- + +# 15.3.   最大容量问题 + +!!! question + + 输入一个数组 $ht$ ,数组中的每个元素代表一个垂直隔板的高度。数组中的任意两个隔板,以及它们之间的空间可以组成一个容器。容器的容量等于高度和宽度的乘积(即面积),其中高度由较短的隔板决定,宽度是两个隔板的数组索引之差。 + + 请在数组中选择两个隔板,使得组成的容器的容量最大,返回最大容量。 + +![最大容量问题的示例数据](max_capacity_problem.assets/max_capacity_example.png) + +

Fig. 最大容量问题的示例数据

+ +**第一步:问题分析** + +容器由任意两个隔板围成,**因此本题的状态为两个隔板的索引,记为 $[i, j]$** 。 + +根据定义,容量等于高度乘以宽度,其中高度由短板决定,宽度是两隔板的索引之差。设容量为 $cap[i, j]$ ,可得计算公式: + +$$ +cap[i, j] = \min(ht[i], ht[j]) \times (j - i) +$$ + +设数组长度为 $n$ ,两个隔板的组合数量(即状态总数)为 $C_n^2 = \frac{n(n - 1)}{2}$ 个。最直接地,**我们可以穷举所有状态**,从而求得最大容量,时间复杂度为 $O(n^2)$ 。 + +**第二步:贪心策略确定** + +当然,这道题还有更高效率的解法。如下图所示,现选取一个状态 $[i, j]$ ,其满足索引 $i < j$ 且高度 $ht[i] < ht[j]$ ,即 $i$ 为短板、 $j$ 为长板。 + +![初始状态](max_capacity_problem.assets/max_capacity_initial_state.png) + +

Fig. 初始状态

+ +我们发现,**如果将长板 $j$ 向短板 $i$ 靠近,则容量一定变小**。这是因为在移动长板 $j$ 后: + +- 宽度 $j-i$ 肯定变小; +- 高度由短板决定,因此高度只可能不变( $i$ 仍为短板)或变小(移动后的 $j$ 成为短板); + +![向内移动长板后的状态](max_capacity_problem.assets/max_capacity_moving_long_board.png) + +

Fig. 向内移动长板后的状态

+ +反向思考,**我们只有向内收缩短板 $i$ ,才有可能使容量变大**。因为虽然宽度一定变小,**但高度可能会变大**(移动后的短板 $i$ 变长了)。 + +![向内移动长板后的状态](max_capacity_problem.assets/max_capacity_moving_short_board.png) + +

Fig. 向内移动长板后的状态

+ +由此便可推出本题的贪心策略: + +1. 初始状态下,指针 $i$ , $j$ 分列与数组两端。 +2. 计算当前状态的容量 $cap[i, j]$ ,并更新最大容量。 +3. 比较板 $i$ 和 板 $j$ 的高度,并将短板向内移动一格。 +4. 循环执行第 `2.` , `3.` 步,直至 $i$ 和 $j$ 相遇时结束。 + +=== "<1>" + ![最大容量问题的贪心过程](max_capacity_problem.assets/max_capacity_greedy_step1.png) + +=== "<2>" + ![max_capacity_greedy_step2](max_capacity_problem.assets/max_capacity_greedy_step2.png) + +=== "<3>" + ![max_capacity_greedy_step3](max_capacity_problem.assets/max_capacity_greedy_step3.png) + +=== "<4>" + ![max_capacity_greedy_step4](max_capacity_problem.assets/max_capacity_greedy_step4.png) + +=== "<5>" + ![max_capacity_greedy_step5](max_capacity_problem.assets/max_capacity_greedy_step5.png) + +=== "<6>" + ![max_capacity_greedy_step6](max_capacity_problem.assets/max_capacity_greedy_step6.png) + +=== "<7>" + ![max_capacity_greedy_step7](max_capacity_problem.assets/max_capacity_greedy_step7.png) + +=== "<8>" + ![max_capacity_greedy_step8](max_capacity_problem.assets/max_capacity_greedy_step8.png) + +=== "<9>" + ![max_capacity_greedy_step9](max_capacity_problem.assets/max_capacity_greedy_step9.png) + +代码实现如下所示。最多循环 $n$ 轮,**因此时间复杂度为 $O(n)$** 。变量 $i$ , $j$ , $res$ 使用常数大小额外空间,**因此空间复杂度为 $O(1)$** 。 + +=== "Java" + + ```java title="max_capacity.java" + /* 最大容量:贪心 */ + int maxCapacity(int[] ht) { + // 初始化 i, j 分列数组两端 + int i = 0, j = ht.length - 1; + // 初始最大容量为 0 + int res = 0; + // 循环贪心选择,直至两板相遇 + while (i < j) { + // 更新最大容量 + int cap = Math.min(ht[i], ht[j]) * (j - i); + res = Math.max(res, cap); + // 向内移动短板 + if (ht[i] < ht[j]) { + i++; + } else { + j--; + } + } + return res; + } + ``` + +=== "C++" + + ```cpp title="max_capacity.cpp" + /* 最大容量:贪心 */ + int maxCapacity(vector &ht) { + // 初始化 i, j 分列数组两端 + int i = 0, j = ht.size() - 1; + // 初始最大容量为 0 + int res = 0; + // 循环贪心选择,直至两板相遇 + while (i < j) { + // 更新最大容量 + int cap = min(ht[i], ht[j]) * (j - i); + res = max(res, cap); + // 向内移动短板 + if (ht[i] < ht[j]) { + i++; + } else { + j--; + } + } + return res; + } + ``` + +=== "Python" + + ```python title="max_capacity.py" + def max_capacity(ht: list[int]) -> int: + """最大容量:贪心""" + # 初始化 i, j 分列数组两端 + i, j = 0, len(ht) - 1 + # 初始最大容量为 0 + res = 0 + # 循环贪心选择,直至两板相遇 + while i < j: + # 更新最大容量 + cap = min(ht[i], ht[j]) * (j - i) + res = max(res, cap) + # 向内移动短板 + if ht[i] < ht[j]: + i += 1 + else: + j -= 1 + return res + ``` + +=== "Go" + + ```go title="max_capacity.go" + [class]{}-[func]{maxCapacity} + ``` + +=== "JavaScript" + + ```javascript title="max_capacity.js" + [class]{}-[func]{maxCapacity} + ``` + +=== "TypeScript" + + ```typescript title="max_capacity.ts" + [class]{}-[func]{maxCapacity} + ``` + +=== "C" + + ```c title="max_capacity.c" + [class]{}-[func]{maxCapacity} + ``` + +=== "C#" + + ```csharp title="max_capacity.cs" + [class]{max_capacity}-[func]{maxCapacity} + ``` + +=== "Swift" + + ```swift title="max_capacity.swift" + [class]{}-[func]{maxCapacity} + ``` + +=== "Zig" + + ```zig title="max_capacity.zig" + [class]{}-[func]{maxCapacity} + ``` + +=== "Dart" + + ```dart title="max_capacity.dart" + [class]{}-[func]{maxCapacity} + ``` + +**第三步:正确性证明** + +之所以贪心比穷举更快,是因为每轮的贪心选择都会“跳过”一些状态。 + +比如在状态 $cap[i, j]$ 下,$i$ 为短板、$j$ 为长板。若贪心地将短板 $i$ 向内移动一格,会导致以下状态被“跳过”,**意味着之后无法验证这些状态的容量大小**。 + +$$ +cap[i, i+1], cap[i, i+2], \cdots, cap[i, j-2], cap[i, j-1] +$$ + +![移动短板导致被跳过的状态](max_capacity_problem.assets/max_capacity_skipped_states.png) + +

Fig. 移动短板导致被跳过的状态

+ +观察发现,**这些被跳过的状态实际上就是将长板 $j$ 向内移动的所有状态**。而在第二步中,我们已经证明内移长板一定会导致容量变小,也就是说这些被跳过的状态的容量一定更小。 + +也就是说,被跳过的状态都不可能是最优解,**跳过它们不会导致错过最优解**。 + +以上的分析说明,**移动短板的操作是“安全”的**,贪心策略是有效的。 diff --git a/chapter_tree/array_representation_of_tree.md b/chapter_tree/array_representation_of_tree.md index 7d7fda1ef..d1a7cd2e1 100644 --- a/chapter_tree/array_representation_of_tree.md +++ b/chapter_tree/array_representation_of_tree.md @@ -415,7 +415,93 @@ comments: true === "C#" ```csharp title="array_binary_tree.cs" - [class]{ArrayBinaryTree}-[func]{} + /* 数组表示下的二叉树类 */ + class ArrayBinaryTree { + private List tree; + + /* 构造方法 */ + public ArrayBinaryTree(List arr) { + tree = new List(arr); + } + + /* 节点数量 */ + public int size() { + return tree.Count; + } + + /* 获取索引为 i 节点的值 */ + public int? val(int i) { + // 若索引越界,则返回 null ,代表空位 + if (i < 0 || i >= size()) + return null; + return tree[i]; + } + + /* 获取索引为 i 节点的左子节点的索引 */ + public int left(int i) { + return 2 * i + 1; + } + + /* 获取索引为 i 节点的右子节点的索引 */ + public int right(int i) { + return 2 * i + 2; + } + + /* 获取索引为 i 节点的父节点的索引 */ + public int parent(int i) { + return (i - 1) / 2; + } + + /* 层序遍历 */ + public List levelOrder() { + List res = new List(); + // 直接遍历数组 + for (int i = 0; i < size(); i++) { + if (val(i).HasValue) + res.Add(val(i).Value); + } + return res; + } + + /* 深度优先遍历 */ + private void dfs(int i, string order, List res) { + // 若为空位,则返回 + if (!val(i).HasValue) + return; + // 前序遍历 + if (order == "pre") + res.Add(val(i).Value); + dfs(left(i), order, res); + // 中序遍历 + if (order == "in") + res.Add(val(i).Value); + dfs(right(i), order, res); + // 后序遍历 + if (order == "post") + res.Add(val(i).Value); + } + + /* 前序遍历 */ + public List preOrder() { + List res = new List(); + dfs(0, "pre", res); + return res; + } + + /* 中序遍历 */ + public List inOrder() { + List res = new List(); + dfs(0, "in", res); + return res; + } + + /* 后序遍历 */ + public List postOrder() { + List res = new List(); + dfs(0, "post", res); + return res; + } + } ``` === "Swift"