tag:blogger.com,1999:blog-56213399711611429442024-03-05T10:01:42.794-08:00Massive AlgorithmsData Structures and Algorithm Interview Questions from the internet.
Help programmers to get hired by dream companies like Google or Facebook.Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comBlogger5157125tag:blogger.com,1999:blog-5621339971161142944.post-26064480497280526602021-04-08T17:37:00.002-07:002021-04-08T17:37:33.544-07:00LeetCode 1766 - Tree of Coprimes <p><a href="https://ophaxor.com/leetcode-1766-tree-of-coprimes">https://ophaxor.com/leetcode-1766-tree-of-coprimes</a></p><p style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.8; margin: 0px 0px 28px;">There is a tree (i.e., a connected, undirected graph that has no cycles) consisting of <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">n</code> nodes numbered from <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">0</code> to <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">n - 1</code> and exactly <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">n - 1</code> edges. Each node has a value associated with it, and the <span style="box-sizing: border-box; font-weight: 700;">root</span> of the tree is node <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">0</code>.</p><p style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.8; margin: 28px 0px;">To represent this tree, you are given an integer array <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">nums</code> and a 2D array <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">edges</code>. Each <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">nums[i]</code> represents the <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">i<span style="box-sizing: border-box; font-size: 10.8px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">th</span></code> node’s value, and each <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">edges[j] = [u<span style="bottom: -0.25em; box-sizing: border-box; font-size: 10.8px; line-height: 0; position: relative; vertical-align: baseline;">j</span>, v<span style="bottom: -0.25em; box-sizing: border-box; font-size: 10.8px; line-height: 0; position: relative; vertical-align: baseline;">j</span>]</code> represents an edge between nodes <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">u<span style="bottom: -0.25em; box-sizing: border-box; font-size: 10.8px; line-height: 0; position: relative; vertical-align: baseline;">j</span></code> and <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">v<span style="bottom: -0.25em; box-sizing: border-box; font-size: 10.8px; line-height: 0; position: relative; vertical-align: baseline;">j</span></code> in the tree.</p><p style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.8; margin: 28px 0px;">Two values <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">x</code> and <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">y</code> are <span style="box-sizing: border-box; font-weight: 700;">coprime</span> if <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">gcd(x, y) == 1</code> where <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">gcd(x, y)</code> is the <span style="box-sizing: border-box; font-weight: 700;">greatest common divisor</span> of <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">x</code> and <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">y</code>.</p><p style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.8; margin: 28px 0px;">An ancestor of a node <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">i</code> is any other node on the shortest path from node <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">i</code> to the <span style="box-sizing: border-box; font-weight: 700;">root</span>. A node is <span style="box-sizing: border-box; font-weight: 700;">not </span>considered an ancestor of itself.</p><p style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.8; margin: 28px 0px;">Return <em style="box-sizing: border-box;">an array </em><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">ans</code><em style="box-sizing: border-box;"> of size </em><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">n</code>, <em style="box-sizing: border-box;">where </em><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">ans[i]</code><em style="box-sizing: border-box;"> is the closest ancestor to node </em><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">i</code><em style="box-sizing: border-box;"> such that </em><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">nums[i]</code> <em style="box-sizing: border-box;">and </em><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">nums[ans[i]]</code> are <span style="box-sizing: border-box; font-weight: 700;">coprime</span>, or <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.4px; padding: 2px 4px;">-1</code><em style="box-sizing: border-box;"> if there is no such ancestor</em>.</p><p style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.8; margin: 28px 0px;"><span style="box-sizing: border-box; font-weight: 700;">Example 1:</span></p><figure class="wp-block-image" style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 15.9992px; margin: 0px 0px 28px; max-width: 100%;"><img alt="" src="https://assets.leetcode.com/uploads/2021/01/06/untitled-diagram.png" style="border: 0px; box-sizing: border-box; height: auto; max-width: 100%; vertical-align: middle;" /></figure><pre class="wp-block-preformatted" style="background-color: #fafafa; border-radius: 0px; border: 1px solid rgba(0, 0, 0, 0.1); box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.9em; line-height: 1.6; margin-bottom: 28px; margin-top: 28px; overflow-wrap: break-word; overflow: auto; padding: 1.6em; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 700;">Input:</span> nums = [2,3,3,2], edges = [[0,1],[1,2],[1,3]]
<span style="box-sizing: border-box; font-weight: 700;">Output:</span> [-1,0,0,1]
<span style="box-sizing: border-box; font-weight: 700;">Explanation:</span> In the above figure, each node's value is in parentheses.
- Node 0 has no coprime ancestors.
- Node 1 has only one ancestor, node 0. Their values are coprime (gcd(2,3) == 1).
- Node 2 has two ancestors, nodes 1 and 0. Node 1's value is not coprime (gcd(3,3) == 3), but node 0's
value is (gcd(2,3) == 1), so node 0 is the closest valid ancestor.
- Node 3 has two ancestors, nodes 1 and 0. It is coprime with node 1 (gcd(3,2) == 1), so node 1 is its
closest valid ancestor.
</pre><p style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.8; margin: 28px 0px;"><span style="box-sizing: border-box; font-weight: 700;">Example 2:</span></p><figure class="wp-block-image" style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 15.9992px; margin: 0px 0px 28px; max-width: 100%;"><img alt="" src="https://assets.leetcode.com/uploads/2021/01/06/untitled-diagram1.png" style="border: 0px; box-sizing: border-box; height: auto; max-width: 100%; vertical-align: middle;" /></figure><pre class="wp-block-preformatted" style="background-color: #fafafa; border-radius: 0px; border: 1px solid rgba(0, 0, 0, 0.1); box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.9em; line-height: 1.6; margin-bottom: 28px; margin-top: 28px; overflow-wrap: break-word; overflow: auto; padding: 1.6em; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 700;">Input:</span> nums = [5,6,10,2,3,6,15], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
<span style="box-sizing: border-box; font-weight: 700;">Output:</span> [-1,0,-1,0,0,0,-1]
</pre><p style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 16px; line-height: 1.8; margin: 28px 0px;"><span style="box-sizing: border-box; font-weight: 700;">Constraints:</span></p><ul style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.8); font-family: "Open Sans", Arial, Helvetica, sans-serif; font-size: 15.9992px; margin: 0px 0px 1.6em; padding: 0px 0px 0px 1.8em;"><li style="box-sizing: border-box; margin-bottom: 1rem !important; margin-top: 1rem !important;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.3993px; padding: 2px 4px;">nums.length == n</code></li><li style="box-sizing: border-box; margin-bottom: 1rem !important; margin-top: 1rem !important;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.3993px; padding: 2px 4px;">1 <= nums[i] <= 50</code></li><li style="box-sizing: border-box; margin-bottom: 1rem !important; margin-top: 1rem !important;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.3993px; padding: 2px 4px;">1 <= n <= 10<span style="box-sizing: border-box; font-size: 10.7995px; line-height: 0; position: relative; top: -0.5em; vertical-align: baseline;">5</span></code></li><li style="box-sizing: border-box; margin-bottom: 1rem !important; margin-top: 1rem !important;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.3993px; padding: 2px 4px;">edges.length == n - 1</code></li><li style="box-sizing: border-box; margin-bottom: 1rem !important; margin-top: 1rem !important;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.3993px; padding: 2px 4px;">edges[j].length == 2</code></li><li style="box-sizing: border-box; margin-bottom: 1rem !important; margin-top: 1rem !important;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.3993px; padding: 2px 4px;">0 <= u<span style="bottom: -0.25em; box-sizing: border-box; font-size: 10.7995px; line-height: 0; position: relative; vertical-align: baseline;">j</span>, v<span style="bottom: -0.25em; box-sizing: border-box; font-size: 10.7995px; line-height: 0; position: relative; vertical-align: baseline;">j</span> < n</code></li><li style="box-sizing: border-box;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14.3993px; padding: 2px 4px;">u<span style="bottom: -0.25em; box-sizing: border-box; font-size: 10.7995px; line-height: 0; position: relative; vertical-align: baseline;">j</span> != v<span style="bottom: -0.25em; box-sizing: border-box; font-size: 10.7995px; line-height: 0; position: relative; vertical-align: baseline;">j</span></code></li><li><br /></li></ul><p style="background-color: white; color: #333333; font-family: "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 13.3333px; margin: 10px auto; padding: 0px;">题意: 对于树上的每个节点,找出与它的值互质的最近的祖先节点。</p><p></p><pre style="background-color: white; color: #333333; font-size: 13.3333px; margin-bottom: 10px; margin-top: 10px; overflow: auto; padding: 0px;"></pre><p></p><p style="-webkit-text-stroke-width: 0px; background-color: white; color: #333333; font-family: "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 13.3333px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 10px auto; orphans: 2; padding: 0px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">题解: 由于节点上的值在1-50之间,所以算互质很好算,事先算法。然后就是深度优先遍历树的时候维护路径上的节点的位置,利用1-50这个小范围,快速找到与当前节点值互质的值出现在哪些位置上</p><p><a href="https://leetcode.com/problems/tree-of-coprimes/discuss/1074581/Java-or-DFS-or-O(n)">https://leetcode.com/problems/tree-of-coprimes/discuss/1074581/Java-or-DFS-or-O(n)</a></p><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">gcd</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> n1, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> n2)</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (n2 == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> n1;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> gcd(n2, n1 % n2);
}
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">dfs</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] nums, LinkedList<Integer>[] tree, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> depth, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> node, boolean[] visited, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] ans, Map<Integer,<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[]> <span class="hljs-built_in" style="box-sizing: border-box;">map</span>, boolean[][] poss)</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(visited[node]) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span>;
visited[node] = <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">true</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> ancestor = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> d = Integer.MAX_VALUE;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>; i < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">51</span>; i++) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(poss[nums[node]][i] && <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>.containsKey(i)) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(depth - <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>.get(i)[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>] <= d) {
d = depth - <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>.get(i)[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>];
ancestor = <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>.get(i)[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>];
}
}
}
ans[node] = ancestor;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] exist = (<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>.containsKey(nums[node])) ? <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>.get(nums[node]) : <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[]{<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>,<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>};
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>.put(nums[node],<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[]{depth,node});
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> child : tree[node]) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(visited[child]) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">continue</span>;
dfs(nums, tree, depth+<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, child, visited, ans, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>, poss);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(exist[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>] != <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>) <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>.put(nums[node], exist);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>.remove(nums[node]);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span>;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] getCoprimes(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] nums, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[][] edges) {
boolean[][] poss = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> boolean[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">51</span>][<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">51</span>];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>; i < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">51</span>; i++) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> j = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>; j < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">51</span>; j++) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(gcd(i,j) == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) {
poss[i][j] = <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">true</span>;
poss[j][i] = <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">true</span>;
}
}
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> n = nums.length;
LinkedList<Integer>[] tree = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> LinkedList[n];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i =<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> ; i < tree.length; i++) tree[i] = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> LinkedList<>();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> edge[] : edges) {
tree[edge[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>]].add(edge[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>]);
tree[edge[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>]].add(edge[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>]);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] ans = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[n];
Arrays.fill(ans, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>);
ans[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>] = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>;
Map<Integer,<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[]> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span> = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> HashMap<>();
boolean[] visited = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> boolean[n];
dfs(nums, tree, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, visited, ans, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">map</span>, poss);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans;
} </code></pre><p><a href="https://leetcode.com/problems/tree-of-coprimes/discuss/1074737/Python-3-DFS-Clean-and-Concise">https://leetcode.com/problems/tree-of-coprimes/discuss/1074737/Python-3-DFS-Clean-and-Concise</a></p><ul style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;"><li style="box-sizing: border-box;">Time: <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(50 * N)</code></li><li style="box-sizing: border-box;">Space: <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(N)</code></li></ul><ul style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;"><li style="box-sizing: border-box;">Please notice in the constraints that all node values is in range <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1..50</code>.</li><li style="box-sizing: border-box;">We create an array of 51 elements <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">path[51]</code> where <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">path[x]</code> contains list of nodes from <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">root</code> to current <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">node</code> which their node's value are equal to <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">x</code>.</li><li style="box-sizing: border-box;">While traversing nodes using DFS, for current <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">node</code> we can check all paths <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">path[x]</code> (up to 50) where <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">x</code> and current <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">node</code> are co-prime then we try to pick the node which is close to current <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">node</code> as our ancestor.</li><li style="box-sizing: border-box;">A node is close to current <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">node</code> if its depth is largest.</li></ul><p></p><p style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></p><p></p><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; widows: 2;"><code style="-webkit-text-stroke-width: 0px; background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; padding: 0px; tab-size: 4; text-transform: none; word-spacing: 0px;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">getCoprimes</span>(<span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">self, nums: List[<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>], edges: List[List[<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>]]</span>) -> List[int]:</span>
ans = [-<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>] * <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">len</span>(nums)
path = [[] <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> _ <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">range</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">51</span>)]
graph = defaultdict(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">list</span>)
seen = <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">set</span>()
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> u, v <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> edges:
graph[u].append(v)
graph[v].append(u)
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">dfs</span>(<span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">node, depth</span>):</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> node <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> seen: <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span>
seen.add(node)
largestDepth = -<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> x <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">range</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">51</span>):
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> gcd(nums[node], x) == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>: <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;"># co-prime</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">len</span>(path[x]) > <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>:
topNode, topDepth = path[x][-<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>]
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> largestDepth < topDepth: <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;"># Pick node which has largestDepth and co-prime with current node as our ancestor node</span>
largestDepth = topDepth
ans[node] = topNode
path[nums[node]].append((node, depth))
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> nei <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> graph[node]:
dfs(nei, depth + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>)
path[nums[node]].pop()
dfs(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>)
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans</code><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; padding: 0px; tab-size: 4; white-space: normal;"><span style="font-family: Times;">
</span></code></pre><p><a href="https://easyforces.com/solutions/1766/">https://easyforces.com/solutions/1766/</a></p><div class="section" id="problem-description" style="background-color: white; box-sizing: border-box; color: #212529; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px;"><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">Like many other problems, let's figure out the input, limitation and output from the description.</p><ul class="simple" style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;"><li style="box-sizing: border-box;">The input of this problem is a tree where each node has a value. It is given by an array <span style="box-sizing: border-box; font-weight: bolder;">nums</span> representing values of each node. And <span style="box-sizing: border-box; font-weight: bolder;">edges</span> representing the edge between the <span style="box-sizing: border-box; font-weight: bolder;">edge[i][0]</span> and <span style="box-sizing: border-box; font-weight: bolder;">edge[i][1]</span>.</li><li style="box-sizing: border-box;">The limitation is given by the definition <span style="box-sizing: border-box; font-weight: bolder;">Coprime</span>, where the greatest common divisor(GCD) of 2 node values is <span style="box-sizing: border-box; font-weight: bolder;">1</span>.</li><li style="box-sizing: border-box;">The required output is the <span style="box-sizing: border-box; font-weight: bolder;">closest coprime anacestor</span> for each node. The result should be returned by an array where <span style="box-sizing: border-box; font-weight: bolder;">result[i]</span> is the <em style="box-sizing: border-box;">index</em> of node <cite style="box-sizing: border-box;">i</cite>'s closest coprime anacestor.</li></ul></div><div class="section" id="data-structure" style="background-color: white; box-sizing: border-box; color: #212529; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px;"><h5 style="box-sizing: border-box; font-size: 1.25rem; font-weight: 500; line-height: 1.2; margin-bottom: 0.5rem; margin-top: 0px;">Data Structure</h5><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">Even if the input is a tree, since we are only given <span style="box-sizing: border-box; font-weight: bolder;">edges</span>, we can use <span style="box-sizing: border-box; font-weight: bolder;">adjacent list</span> to represent the tree, where <cite style="box-sizing: border-box;">adj[i]</cite> is a list of nodes who is node i's neighbors.</p></div><div class="section" id="basic-idea" style="background-color: white; box-sizing: border-box; color: #212529; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px;"><h5 style="box-sizing: border-box; font-size: 1.25rem; font-weight: 500; line-height: 1.2; margin-bottom: 0.5rem; margin-top: 0px;">Basic Idea</h5><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">The basic idea of this problem is to traverse the tree. And during the traversal, for each node, calculate the closest coprime anacestor. Now let's consider the time complexity of this basic solution.</p><ul class="simple" style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;"><li style="box-sizing: border-box;">To traverse a tree, we can use Depth-first Search (DFS). The basic time complexity of DFS is <cite style="box-sizing: border-box;">O(nodes.length + edges.length)</cite>.</li><li style="box-sizing: border-box;">For each node, we need to go through all it's ancestors from bottom to top to check if they are coprime. The worst case would be there are no coprime in it's ancestors, and the time complexity of this step would be <cite style="box-sizing: border-box;">O(tree.depth)</cite>. Since the tree is not a balanced tree, for it's depth, we need to consider it's the same as node number, which is <cite style="box-sizing: border-box;">O(nodes.length)</cite>.</li><li style="box-sizing: border-box;">To check if 2 numbers are coprime, we would need to check all number which is less than or equal to the minimum number between the 2 numbers, to see if it's a common divisor of them. Time complexity of this step is <cite style="box-sizing: border-box;">O(num)</cite>.</li></ul><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">The overall time complexity would be:</p><pre class="literal-block" style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; margin-bottom: 1rem; margin-top: 0px; overflow: auto;">O((nodes.length + edges.length) * nodes.length * max(nums))
</pre><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">The input range is given in the problem description.</p><blockquote style="box-sizing: border-box; margin: 0px 0px 1rem;"><ul class="simple" style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;"><li style="box-sizing: border-box;"><cite style="box-sizing: border-box;">nums.length == n</cite></li><li style="box-sizing: border-box;"><cite style="box-sizing: border-box;">1 <= nums[i] <= 50</cite></li><li style="box-sizing: border-box;"><cite style="box-sizing: border-box;">1 <= n <= 100,000</cite></li><li style="box-sizing: border-box;"><cite style="box-sizing: border-box;">edges.length == n - 1</cite></li><li style="box-sizing: border-box;"><cite style="box-sizing: border-box;">edges[j].length == 2</cite></li><li style="box-sizing: border-box;"><cite style="box-sizing: border-box;">0 <= uj, vj < n</cite></li><li style="box-sizing: border-box;"><cite style="box-sizing: border-box;">uj != vj</cite></li></ul></blockquote><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">And the number of operations is:</p><pre class="literal-block" style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; margin-bottom: 1rem; margin-top: 0px; overflow: auto;">O( (n + n) * n * len(nums) )
= O( n ** 2 * len(nums) )
= O( (10 ** 10) * 50 )
= O( 10 ** 11 )
</pre><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">This is high above standard which is <span style="box-sizing: border-box; font-weight: bolder;">1,000,000</span>.</p></div><div class="section" id="improvement" style="background-color: white; box-sizing: border-box; color: #212529; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px;"><h5 style="box-sizing: border-box; font-size: 1.25rem; font-weight: 500; line-height: 1.2; margin-bottom: 0.5rem; margin-top: 0px;">Improvement</h5><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">The basic solution spend too much time because there are too many operations. In this case, we can consider to pre-calculate something. So the time complexity would be O(pre) + O(solution).</p><ul class="simple" style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;"><li style="box-sizing: border-box;">One key point of this problem is that the range of each number is between <span style="box-sizing: border-box; font-weight: bolder;">1</span> and <span style="box-sizing: border-box; font-weight: bolder;">50</span>. So</li></ul><div class="system-message" style="box-sizing: border-box;"><p class="system-message-title" style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">System Message: WARNING/2 (<tt class="docutils" style="box-sizing: border-box;"><string></tt>, line 69)</p>Bullet list ends without a blank line; unexpected unindent.</div><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">we could calculate isCoprime for all integer pairs in time <cite style="box-sizing: border-box;">O(max(nums) ** 2) = O(2,500)</cite>.</p><ul class="simple" style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;"><li style="box-sizing: border-box;">Then, for each node, instead of calculate through all it's ancestors, we maintain a array storing</li></ul><div class="system-message" style="box-sizing: border-box;"><p class="system-message-title" style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">System Message: WARNING/2 (<tt class="docutils" style="box-sizing: border-box;"><string></tt>, line 72)</p>Bullet list ends without a blank line; unexpected unindent.</div><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">the closest anacestor for each number. And update this array for each node. This would only take <cite style="box-sizing: border-box;">O(max(nums))</cite>.</p><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">So now, the time complexity is:</p><pre class="literal-block" style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; margin-bottom: 1rem; margin-top: 0px; overflow: auto;">O(max(nums) ** 2) + O(n + n) * O(max(nums))
= O(max(nums) ** 2) + O( n * max(nums) )
= O( max(nums) * (max(nums) + n) )
= O( 50 * (50 + 100,000) )
~= O( 5 * (10 ** 6) )
= O( 5,000,000 )
</pre><p style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;">This would be a <span style="box-sizing: border-box; font-weight: bolder;">just fine</span> time time complexity (around 1,000,000).</p></div><div class="section" id="about-code" style="background-color: white; box-sizing: border-box; color: #212529; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; font-size: 16px;"><h5 style="box-sizing: border-box; font-size: 1.25rem; font-weight: 500; line-height: 1.2; margin-bottom: 0.5rem; margin-top: 0px;">About Code</h5><ul class="simple" style="box-sizing: border-box; margin-bottom: 1rem; margin-top: 0px;"><li style="box-sizing: border-box;">The method <span style="box-sizing: border-box; font-weight: bolder;">isCoprime</span> is for calculating whether 2 numbers are coprime.</li><li style="box-sizing: border-box;">The method <span style="box-sizing: border-box; font-weight: bolder;">_dfs</span> is the main part of the solution, which traverses the tree and calculate the closest coprime ancestor.</li><li style="box-sizing: border-box;">Variable <span style="box-sizing: border-box; font-weight: bolder;">candidates</span> stores the cloeset coprime ancestor for each possible number, it's maximum length is 50.</li></ul></div><p>X. Videos</p><p class="blockParagraph-544a408c" data-key="b38e4d9f2edd460c9fd4b21447cae60b"><span class="text-4505230f--TextH400-3033861f--textContentFamily-49a318e1"><a class="link-a079aa82--primary-53a25e66--link-faf6c434" data-key="63a7c9ec3a1e404eb58458c7d231c5a1" href="https://youtu.be/l4sZZ0NJiCE" rel="noopener noreferrer" style="color: #3884ff;" target="_blank"><span data-key="79e128a6038840c2b511935703e1fdcd"><span data-offset-key="79e128a6038840c2b511935703e1fdcd:0">English Version in Youtube</span></span></a><span data-key="15453d3aaa4d49dfb5e9a59fd2cdcf96"><span data-offset-key="15453d3aaa4d49dfb5e9a59fd2cdcf96:0"><span data-slate-zero-width="z"></span></span></span></span></p><p class="blockParagraph-544a408c" data-key="6822511659064ea2835bb14caa532176" data-slate-fragment="JTdCJTIyb2JqZWN0JTIyJTNBJTIyZG9jdW1lbnQlMjIlMkMlMjJkYXRhJTIyJTNBJTdCJTdEJTJDJTIybm9kZXMlMjIlM0ElNUIlN0IlMjJvYmplY3QlMjIlM0ElMjJibG9jayUyMiUyQyUyMnR5cGUlMjIlM0ElMjJwYXJhZ3JhcGglMjIlMkMlMjJpc1ZvaWQlMjIlM0FmYWxzZSUyQyUyMmRhdGElMjIlM0ElN0IlN0QlMkMlMjJub2RlcyUyMiUzQSU1QiU3QiUyMm9iamVjdCUyMiUzQSUyMmlubGluZSUyMiUyQyUyMnR5cGUlMjIlM0ElMjJsaW5rJTIyJTJDJTIyaXNWb2lkJTIyJTNBZmFsc2UlMkMlMjJkYXRhJTIyJTNBJTdCJTIyaHJlZiUyMiUzQSUyMmh0dHBzJTNBJTJGJTJGeW91dHUuYmUlMkZsNHNaWjBOSmlDRSUyMiU3RCUyQyUyMm5vZGVzJTIyJTNBJTVCJTdCJTIyb2JqZWN0JTIyJTNBJTIydGV4dCUyMiUyQyUyMmxlYXZlcyUyMiUzQSU1QiU3QiUyMm9iamVjdCUyMiUzQSUyMmxlYWYlMjIlMkMlMjJ0ZXh0JTIyJTNBJTIyRW5nbGlzaCUyMFZlcnNpb24lMjBpbiUyMFlvdXR1YmUlMjIlMkMlMjJtYXJrcyUyMiUzQSU1QiU1RCU3RCU1RCU3RCU1RCU3RCUyQyU3QiUyMm9iamVjdCUyMiUzQSUyMnRleHQlMjIlMkMlMjJsZWF2ZXMlMjIlM0ElNUIlN0IlMjJvYmplY3QlMjIlM0ElMjJsZWFmJTIyJTJDJTIydGV4dCUyMiUzQSUyMiUyMiUyQyUyMm1hcmtzJTIyJTNBJTVCJTVEJTdEJTVEJTdEJTVEJTdEJTJDJTdCJTIyb2JqZWN0JTIyJTNBJTIyYmxvY2slMjIlMkMlMjJ0eXBlJTIyJTNBJTIycGFyYWdyYXBoJTIyJTJDJTIyaXNWb2lkJTIyJTNBZmFsc2UlMkMlMjJkYXRhJTIyJTNBJTdCJTdEJTJDJTIybm9kZXMlMjIlM0ElNUIlN0IlMjJvYmplY3QlMjIlM0ElMjJ0ZXh0JTIyJTJDJTIybGVhdmVzJTIyJTNBJTVCJTdCJTIyb2JqZWN0JTIyJTNBJTIybGVhZiUyMiUyQyUyMnRleHQlMjIlM0ElMjIlMjIlMkMlMjJtYXJrcyUyMiUzQSU1QiU1RCU3RCU1RCU3RCUyQyU3QiUyMm9iamVjdCUyMiUzQSUyMmlubGluZSUyMiUyQyUyMnR5cGUlMjIlM0ElMjJsaW5rJTIyJTJDJTIyaXNWb2lkJTIyJTNBZmFsc2UlMkMlMjJkYXRhJTIyJTNBJTdCJTIyaHJlZiUyMiUzQSUyMmh0dHBzJTNBJTJGJTJGeW91dHUuYmUlMkZyaUxvVkozUk9FTSUyMiU3RCUyQyUyMm5vZGVzJTIyJTNBJTVCJTdCJTIyb2JqZWN0JTIyJTNBJTIydGV4dCUyMiUyQyUyMmxlYXZlcyUyMiUzQSU1QiU3QiUyMm9iamVjdCUyMiUzQSUyMmxlYWYlMjIlMkMlMjJ0ZXh0JTIyJTNBJTIyJUU0JUI4JUFEJUU2JTk2JTg3JUU3JTg5JTg4JUU4JUE3JUEzJUU3JUFEJTk0WW91dHViZSUyMExpbmslMjIlMkMlMjJtYXJrcyUyMiUzQSU1QiU1RCU3RCU1RCU3RCU1RCU3RCU1RCU3RCU1RCU3RA=="><span class="text-4505230f--TextH400-3033861f--textContentFamily-49a318e1"><span data-key="b3dac319d1c34099a60922e56bc2a7fd"><span data-offset-key="b3dac319d1c34099a60922e56bc2a7fd:0"><span data-slate-zero-width="z"></span></span></span><a class="link-a079aa82--primary-53a25e66--link-faf6c434" data-key="13d5a5f8859e455cb5a53cf17fc88c16" href="https://youtu.be/riLoVJ3ROEM" rel="noopener noreferrer" style="color: #3884ff;" target="_blank"><span data-key="b893c1c4d3c440d7b446e9bb240bdb43"><span data-offset-key="b893c1c4d3c440d7b446e9bb240bdb43:0">中文版解答Youtube Link</span></span></a></span></p><p><br /></p>Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-87949866151181603812021-04-08T17:27:00.004-07:002021-04-08T17:27:34.251-07:00LeetCode 1768 - Merge Strings Alternately<p><a href="http://code.minggr.net/leetcode-1768/" target="_blank">http://code.minggr.net/leetcode-1768/</a></p><p style="background-color: white; border: 0px; color: #555555; font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 14px; line-height: 1.6em; margin: 1.6em 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><a href="https://leetcode.com/problems/merge-strings-alternately/" rel="noopener" style="border: 0px; color: #258fb8; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; text-decoration-line: none; vertical-align: baseline;" target="_blank">https://leetcode.com/problems/merge-strings-alternately/</a></p><p style="background-color: white; border: 0px; color: #555555; font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 14px; line-height: 1.6em; margin: 1.6em 0px; outline: 0px; padding: 0px; vertical-align: baseline;">You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.</p><p style="background-color: white; border: 0px; color: #555555; font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 14px; line-height: 1.6em; margin: 1.6em 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Return the merged string.</p><p style="background-color: white; border: 0px; color: #555555; font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 14px; line-height: 1.6em; margin: 1.6em 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Example 1:</p><p style="background-color: white; border: 0px; color: #555555; font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 14px; line-height: 1.6em; margin: 1.6em 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Input: word1 = “abc”, word2 = “pqr”<br />Output: “apbqcr”<br />Explanation: The merged string will be merged as so:<br />word1: a b c<br />word2: p q r<br />merged: a p b q c r<br />Example 2:</p><p style="background-color: white; border: 0px; color: #555555; font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 14px; line-height: 1.6em; margin: 1.6em 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Input: word1 = “ab”, word2 = “pqrs”<br />Output: “apbqrs”<br />Explanation: Notice that as word2 is longer, “rs” is appended to the end.<br />word1: a b<br />word2: p q r s<br />merged: a p b q r s<br />Example 3:</p><p style="background-color: white; border: 0px; color: #555555; font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 14px; line-height: 1.6em; margin: 1.6em 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Input: word1 = “abcd”, word2 = “pq”<br />Output: “apbqcd”<br />Explanation: Notice that as word1 is longer, “cd” is appended to the end.<br />word1: a b c d<br />word2: p q<br />merged: a p b q c d</p><p style="background-color: white; border: 0px; color: #555555; font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 14px; line-height: 1.6em; margin: 1.6em 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Constraints:</p><p style="background-color: white; border: 0px; color: #555555; font-family: -apple-system, system-ui, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 14px; line-height: 1.6em; margin: 1.6em 0px; outline: 0px; padding: 0px; vertical-align: baseline;">1 <= word1.length, word2.length <= 100<br />word1 and word2 consist of lowercase English letters.</p><pre style="background: rgb(45, 45, 45); border: none; color: #cccccc; font-family: "Source Code Pro", Consolas, Monaco, Menlo, Consolas, monospace; font-size: 14px; line-height: 22.4px; margin-bottom: 0px; margin-top: 0px; outline: 0px; overflow: auto; padding: 0px; vertical-align: baseline;"><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> <span class="function" style="border: 0px; color: #6699cc; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><span class="built_in" style="border: 0px; color: #f99157; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">string</span> <span class="title" style="border: 0px; color: #999999; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">mergeAlternately</span><span class="params" style="border: 0px; color: #f99157; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(<span class="built_in" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">string</span> word1, <span class="built_in" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">string</span> word2)</span> </span>{</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> <span class="keyword" style="border: 0px; color: #cc99cc; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">int</span> i = <span class="number" style="border: 0px; color: #f99157; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">0</span>, j = <span class="number" style="border: 0px; color: #f99157; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">0</span>;</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> <span class="built_in" style="border: 0px; color: #f99157; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">string</span> ans;</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> <span class="keyword" style="border: 0px; color: #cc99cc; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">while</span> (i < word1.size() && j < word2.size()) {</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> <span class="keyword" style="border: 0px; color: #cc99cc; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">if</span> ((i+j) % <span class="number" style="border: 0px; color: #f99157; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">2</span> == <span class="number" style="border: 0px; color: #f99157; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">0</span>)</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> ans.push_back(word1[i++]);</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> <span class="keyword" style="border: 0px; color: #cc99cc; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">else</span></span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> ans.push_back(word2[j++]);</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> }</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> <span class="keyword" style="border: 0px; color: #cc99cc; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">if</span> (i < word1.size())</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> ans += word1.substr(i);</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> <span class="keyword" style="border: 0px; color: #cc99cc; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">if</span> (j < word2.size())</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> ans += word2.substr(j);</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> <span class="keyword" style="border: 0px; color: #cc99cc; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">return</span> ans;</span><br /><span class="line" style="border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; height: 22.4px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> }</span><br /></pre><p><a href="https://leetcode.com/problems/merge-strings-alternately/discuss/1075534/JavaC%2B%2BPython-Straignt-Forward-Solutions">https://leetcode.com/problems/merge-strings-alternately/discuss/1075534/JavaC%2B%2BPython-Straignt-Forward-Solutions</a></p><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">Alternatively append the character from <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">w1</code> and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">w2</code> to <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">res</code>.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /></p><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;"></p><h1 style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: unset; font-weight: 500; margin: 0px 0px 0.5em;"><span style="box-sizing: border-box; font-weight: bolder;">Complexity</span></h1><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;"></p><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">Time <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(n + m)</code><br style="box-sizing: border-box;" />Space <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(n + m)</code><br style="box-sizing: border-box;" /></p><p>X. Two Pointers</p><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> String <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">mergeAlternately</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(String w1, String w2)</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> n = w1.length(), m = w2.length(), i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, j = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
StringBuilder res = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> StringBuilder();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">while</span> (i < n || j < m) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (i < w1.length())
res.append(w1.charAt(i++));
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (j < w2.length())
res.append(w2.charAt(j++));
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> res.toString();
}</code></pre><p>One Pointer</p><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> String <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">mergeAlternately</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(String w1, String w2)</span> </span>{
StringBuilder res = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> StringBuilder();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < w1.length() || i < w2.length(); ++i) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (i < w1.length())
res.append(w1.charAt(i));
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (i < w2.length())
res.append(w2.charAt(i));
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> res.toString();
}</code></pre><p><a href="https://walkccc.me/LeetCode/problems/1768/">https://walkccc.me/LeetCode/problems/1768/</a></p><p><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">string</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">mergeAlternately</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">(</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">string</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">word1</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">,</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">string</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">word2</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">)</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">{</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">
</span><span class="k" style="box-sizing: inherit; color: var(--md-code-hl-keyword-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">const</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="kt" style="box-sizing: inherit; color: var(--md-code-hl-keyword-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">int</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">n</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="o" style="box-sizing: inherit; color: var(--md-code-hl-operator-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">=</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">min</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">(</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">word1</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">.</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">length</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">(),</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">word2</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">.</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">length</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">());</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">
</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">string</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">prefix</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">;</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">
</span><span class="k" style="box-sizing: inherit; color: var(--md-code-hl-keyword-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">for</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">(</span><span class="kt" style="box-sizing: inherit; color: var(--md-code-hl-keyword-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">int</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">i</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="o" style="box-sizing: inherit; color: var(--md-code-hl-operator-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">=</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="mi" style="box-sizing: inherit; color: var(--md-code-hl-number-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">0</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">;</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">i</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="o" style="box-sizing: inherit; color: var(--md-code-hl-operator-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"><</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">n</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">;</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="o" style="box-sizing: inherit; color: var(--md-code-hl-operator-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">++</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">i</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">)</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">{</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">
</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">prefix</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="o" style="box-sizing: inherit; color: var(--md-code-hl-operator-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">+=</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">word1</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">[</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">i</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">];</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">
</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">prefix</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="o" style="box-sizing: inherit; color: var(--md-code-hl-operator-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">+=</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">word2</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">[</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">i</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">];</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">
</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">}</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">
</span><span class="k" style="box-sizing: inherit; color: var(--md-code-hl-keyword-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">return</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">prefix</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="o" style="box-sizing: inherit; color: var(--md-code-hl-operator-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">+</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">word1</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">.</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">substr</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">(</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">n</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">)</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="o" style="box-sizing: inherit; color: var(--md-code-hl-operator-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">+</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;"> </span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">word2</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">.</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">substr</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">(</span><span class="n" style="box-sizing: inherit; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">n</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">);</span><span style="background-color: whitesmoke; color: #36464e; font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">
</span><span class="p" style="box-sizing: inherit; color: var(--md-code-hl-punctuation-color); font-family: "Roboto Mono", SFMono-Regular, Consolas, Menlo, monospace; font-size: 14.96px; white-space: pre;">}</span></p><p><br /></p>Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-17791913668852530632021-04-08T17:22:00.003-07:002021-04-08T17:22:26.423-07:00LeetCode 1769 - Minimum Number of Operations to Move All Balls to Each Box<p><a href="https://chowdera.com/2021/03/20210326160903226c.html">https://chowdera.com/2021/03/20210326160903226c.html</a></p><p style="color: #282d36; font-family: "Microsoft YaHei", Arial, Helvetica, sans-serif; font-size: 15px; line-height: inherit; margin: 10px 0px; padding: 0px;">Give you a length of n Binary string of boxes , among boxes[i] The value of is '0' It means the first one i A box is empty Of , and boxes[i] The value of is '1' There is... In the box One Pellet .</p><p style="color: #282d36; font-family: "Microsoft YaHei", Arial, Helvetica, sans-serif; font-size: 15px; line-height: inherit; margin: 10px 0px; padding: 0px;">In one step , You can take One The ball moves from a box to an adjacent box . The first i A box and j Two adjacent boxes need to satisfy abs(i - j) == 1 . Be careful , After operation , There may be more than one ball in some boxes .</p><p style="color: #282d36; font-family: "Microsoft YaHei", Arial, Helvetica, sans-serif; font-size: 15px; line-height: inherit; margin: 10px 0px; padding: 0px;">Returns a length of n Array of answer , among answer[i] It's moving all the balls to the second i What a box needs Minimum Operands .</p><p style="color: #282d36; font-family: "Microsoft YaHei", Arial, Helvetica, sans-serif; font-size: 15px; line-height: inherit; margin: 10px 0px; padding: 0px;">Every answer[i] It all depends on the box The initial state Calculate .</p><div><a href="https://leetcode-cn.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/">https://leetcode-cn.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/</a></div><div><div>有 n 个盒子。给你一个长度为 n 的二进制字符串 boxes ,其中 boxes[i] 的值为 '0' 表示第 i 个盒子是 空 的,而 boxes[i] 的值为 '1' 表示盒子里有 一个 小球。</div><div><br /></div><div>在一步操作中,你可以将 一个 小球从某个盒子移动到一个与之相邻的盒子中。第 i 个盒子和第 j 个盒子相邻需满足 abs(i - j) == 1 。注意,操作执行后,某些盒子中可能会存在不止一个小球。</div><div><br /></div><div>返回一个长度为 n 的数组 answer ,其中 answer[i] 是将所有小球移动到第 i 个盒子所需的 最小 操作数。</div><div><br /></div><div>每个 answer[i] 都需要根据盒子的 初始状态 进行计算。</div><div><br /></div></div><div><div>输入:boxes = "110"</div><div>输出:[1,1,3]</div><div>解释:每个盒子对应的最小操作数如下:</div><div>1) 第 1 个盒子:将一个小球从第 2 个盒子移动到第 1 个盒子,需要 1 步操作。</div><div>2) 第 2 个盒子:将一个小球从第 1 个盒子移动到第 2 个盒子,需要 1 步操作。</div><div>3) 第 3 个盒子:将一个小球从第 1 个盒子移动到第 3 个盒子,需要 2 步操作。将一个小球从第 2 个盒子移动到第 3 个盒子,需要 1 步操作。共计 3 步操作。</div><div>示例 2:</div><div><br /></div><div>输入:boxes = "001011"</div><div>输出:[11,8,5,4,3,4]</div><div><br /></div></div><div>X. O(n) - left sum and right sum</div><div><a href="https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/discuss/1075595/Java%3A-O(n)-left-sum-and-right-sum">https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/discuss/1075595/Java%3A-O(n)-left-sum-and-right-sum</a></div><div><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] minOperations(String boxes) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> n = boxes.length();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] left = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[n];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] right = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[n];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] ans = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[n];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> count = boxes.charAt(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'0'</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> ; i < n ; i++){
left[i] = left[i - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>] + count;
count += boxes.charAt(i) - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'0'</span>;
}
count = boxes.charAt(n - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'0'</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = n - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span> ; i >=<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> ; i--){
right[i] = right[i + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>] + count;
count += boxes.charAt(i) - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'0'</span>;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> ; i < n ; i++) {
ans[i] = left[i] + right[i];
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans;
}</code></pre></div><div><span style="background-color: white; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px;">You can just get rid of the temp arrays. As problem asks us to return a new </span><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">answer</code><span style="background-color: white; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px;"> array, we won't consider that space in complexity analysis and hence space complexity is constant.</span></div><div><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>[] <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">minOperations</span>(<span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">String boxes</span>)</span> {
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>[] ans = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>[boxes.length()];
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> cnt = boxes.charAt(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'0'</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> i=<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>;i<boxes.length(); i++) {
ans[i] = ans[i<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>] + cnt;
cnt += boxes.charAt(i) - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'0'</span>;
}
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> runner = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
cnt = boxes.charAt(boxes.length()<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>) - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'0'</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> i=boxes.length()<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-2</span>;i>=<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i--) {
runner += cnt;
ans[i] += runner;
cnt += boxes.charAt(i) - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'0'</span>;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans;
}</code></pre></div><div><a href="https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/discuss/1077682/Java-oror-DP-oror-beats-100-oror-2ms-oror-O(boxes.length)-O(1)">https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/discuss/1077682/Java-oror-DP-oror-beats-100-oror-2ms-oror-O(boxes.length)-O(1)</a></div><div><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;">public <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] minOperations(String boxes) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> left = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, right = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, lsum = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, rsum = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">len</span> = boxes.length();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">len</span>; i++) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (boxes.charAt(i) == <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'1'</span>) {
right++;
rsum += i;
}
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] ans = <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">len</span>];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">len</span>; i++) {
ans[i] = lsum + rsum;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (boxes.charAt(i) == <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'1'</span>) {
left++;
right--;
}
lsum = lsum + left;
rsum = rsum - right;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans;
}</code></pre></div><div><br /></div><div><a href="https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/discuss/1075474/C%2B%2BJava-O(n)-LTR-%2B-RTL">https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/discuss/1075474/C%2B%2BJava-O(n)-LTR-%2B-RTL</a></div><div><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">We first "move" balls from left to right, and track how many ops it takes for each box.</p><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;"></p><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">For that, we count how many balls we got so far in <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">cnt</code>, and accumulate it in <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">ops</code>.</p><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;"></p><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">Then, we do the same from right to left.</p><p style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;"></p></div><div><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] minOperations(String boxes) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] res = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[boxes.length()];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, ops = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, cnt = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < boxes.length(); ++i) {
res[i] += ops;
cnt += boxes.charAt(i) == <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'1'</span> ? <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> : <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
ops += cnt;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = boxes.length() - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, ops = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, cnt = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i >= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; --i) {
res[i] += ops;
cnt += boxes.charAt(i) == <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'1'</span> ? <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> : <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
ops += cnt;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> res;
}</code></pre></div><div><a href="https://chowdera.com/2021/03/20210326160903226c.html" target="_blank">https://chowdera.com/2021/03/20210326160903226c.html</a><br /></div><p style="background-color: white; color: #282d36; font-family: "Microsoft YaHei", Arial, Helvetica, sans-serif; font-size: 15px; line-height: inherit; margin: 22px 0px; padding: 0px;">What needs to be considered is , How to move to by all <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">x</code> The number of operations <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">answer[x]</code> Find all move to <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">x+1</code> The number of operations <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">answer[x+1]</code>. about <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">x+1</code> The ball on the left , Each ball will be operated once more , about <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">x</code> The ball on the right , Each will operate less than once . So , You just have to figure out <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">x+1</code> The number of balls on the left side <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">left(x+1)</code> and <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">x</code> The number of balls on the right <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">right(x)</code> that will do . That is to say <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">answer[x+1]=answer[x]+left(x+1)-right(x)</code>.</p><p style="background-color: white; color: #282d36; font-family: "Microsoft YaHei", Arial, Helvetica, sans-serif; font-size: 15px; line-height: inherit; margin: 22px 0px; padding: 0px;">At present, we don't have to ask for it every time <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">left</code> and <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">right</code>, Save the position of the ball in line , Compare the current position and <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">x</code>,<code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">x+1</code> The relationship between , It can be calculated directly <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">left</code> and <code style="background-color: #fff5f5; border-radius: 2px; color: #ff502c; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 0.87em; margin: 0px; overflow-x: auto; padding: 0.065em 0.4em; word-break: break-word;">right</code> The change of , But this code is easier to understand . You can try to optimize it yourself .</p><p style="background-color: white; color: #282d36; font-family: "Microsoft YaHei", Arial, Helvetica, sans-serif; font-size: 15px; line-height: inherit; margin: 22px 0px; padding: 0px;"><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> </span><span class="hljs-function" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;"><span class="hljs-built_in" style="margin: 0px; padding: 0px;">vector</span><<span class="hljs-keyword" style="margin: 0px; padding: 0px;">int</span>> <span class="hljs-title" style="margin: 0px; padding: 0px;">minOperations</span><span class="hljs-params" style="margin: 0px; padding: 0px;">(<span class="hljs-built_in" style="margin: 0px; padding: 0px;">string</span> boxes)</span> </span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">{
</span><span class="hljs-built_in" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">vector</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"><</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">int</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">> positions;
</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">int</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> n = boxes.size();
</span><span class="hljs-function" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;"><span class="hljs-built_in" style="margin: 0px; padding: 0px;">vector</span><<span class="hljs-keyword" style="margin: 0px; padding: 0px;">int</span>> <span class="hljs-title" style="margin: 0px; padding: 0px;">answer</span><span class="hljs-params" style="margin: 0px; padding: 0px;">(n, <span class="hljs-number" style="margin: 0px; padding: 0px;">0</span>)</span></span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">;
</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">for</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> (</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">int</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> i = </span><span class="hljs-number" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">0</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">; i < n; i++) {
</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">if</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> (boxes[i] == </span><span class="hljs-string" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">'1'</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">) {
positions.push_back(i);
</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">if</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> (i) answer[</span><span class="hljs-number" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">0</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">] += i;
}
}
</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">for</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> (</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">int</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> i = </span><span class="hljs-number" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">1</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">; i < n; i++) {
</span><span class="hljs-comment" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">// answer[i]=answer[i-1]+left(i)-right(i-1)</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">
</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">int</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> left = lower_bound(positions.begin(), positions.end(), i) - positions.begin();
</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">int</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> right = positions.end() - upper_bound(positions.begin(), positions.end(), i - </span><span class="hljs-number" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">1</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">);
answer[i] = answer[i - </span><span class="hljs-number" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">1</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;">] + left - right;
}
</span><span class="hljs-keyword" style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; margin: 0px; padding: 0px; white-space: pre;">return</span><span style="color: #262626; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px; white-space: pre;"> answer;
}</span></p><p style="background-color: white; line-height: inherit; margin: 22px 0px; padding: 0px;"><a href="https://www.taodudu.cc/news/show-2114813.html">https://www.taodudu.cc/news/show-2114813.html</a></p><p style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", 微软雅黑; font-size: 16px; margin: 0px 0px 16px; overflow-wrap: break-word; padding: 0px;">这个思路是:我们先找到把所有的 1 移到下标为 0 的起始位置需要的最小操作数为 <span class="katex--inline" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-mathml" style="box-sizing: border-box; margin: 0px; padding: 0px;">r e s [ 0 ] res[0] </span><span class="katex-html" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.02778em 0px 0px; padding: 0px;">r</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">e</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">s</span><span class="mopen" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span class="mord" style="box-sizing: border-box; margin: 0px; padding: 0px;">0</span><span class="mclose" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span></span></span></span></span>。然后其余<strong style="box-sizing: border-box; margin: 0px; padding: 0px;">把所有 1 移到 <span class="katex--inline" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-mathml" style="box-sizing: border-box; margin: 0px; padding: 0px;">i i </span><span class="katex-html" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 0.65952em; margin: 0px; padding: 0px; vertical-align: 0em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span></span></span></span></span> 位置的操作数 = 把所有 1 移到 <span class="katex--inline" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-mathml" style="box-sizing: border-box; margin: 0px; padding: 0px;">i − 1 i - 1 </span><span class="katex-html" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 0.74285em; margin: 0px; padding: 0px; vertical-align: -0.08333em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span><span class="mbin" style="box-sizing: border-box; margin: 0px; padding: 0px;">−</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span></span><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 0.64444em; margin: 0px; padding: 0px; vertical-align: 0em;"></span><span class="mord" style="box-sizing: border-box; margin: 0px; padding: 0px;">1</span></span></span></span></span> 位置的的操作数 -(右边的所有 1) + (左边的所有 1)</strong>,即转移方程是:</p><p style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", 微软雅黑; font-size: 16px; margin: 0px 0px 16px; overflow-wrap: break-word; padding: 0px;"><span class="katex--display" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-display" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-mathml" style="box-sizing: border-box; margin: 0px; padding: 0px;">r e s [ i ] = r e s [ i − 1 ] − ( o n e s − v i s i t e d ) + v i s i t e d res[i] = res[i - 1] - (ones - visited) + visited </span><span class="katex-html" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.02778em 0px 0px; padding: 0px;">r</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">e</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">s</span><span class="mopen" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span><span class="mclose" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.277778em 0px 0px; padding: 0px;"></span><span class="mrel" style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.277778em 0px 0px; padding: 0px;"></span></span><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.02778em 0px 0px; padding: 0px;">r</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">e</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">s</span><span class="mopen" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span><span class="mbin" style="box-sizing: border-box; margin: 0px; padding: 0px;">−</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span></span><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord" style="box-sizing: border-box; margin: 0px; padding: 0px;">1</span><span class="mclose" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span><span class="mbin" style="box-sizing: border-box; margin: 0px; padding: 0px;">−</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span></span><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mopen" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">o</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">n</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">e</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">s</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span><span class="mbin" style="box-sizing: border-box; margin: 0px; padding: 0px;">−</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span></span><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.03588em 0px 0px; padding: 0px;">v</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">s</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">t</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">e</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">d</span><span class="mclose" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span><span class="mbin" style="box-sizing: border-box; margin: 0px; padding: 0px;">+</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span></span><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 0.69444em; margin: 0px; padding: 0px; vertical-align: 0em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.03588em 0px 0px; padding: 0px;">v</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">s</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">t</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">e</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">d</span></span></span></span></span></span></p><p style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", 微软雅黑; font-size: 16px; margin: 0px 0px 16px; overflow-wrap: break-word; padding: 0px;">原理是什么呢?因为把所有位置移动到 <span class="katex--inline" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-mathml" style="box-sizing: border-box; margin: 0px; padding: 0px;">r e s [ i ] res[i] </span><span class="katex-html" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.02778em 0px 0px; padding: 0px;">r</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">e</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">s</span><span class="mopen" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span><span class="mclose" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span></span></span></span></span> 位置相对于 <span class="katex--inline" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-mathml" style="box-sizing: border-box; margin: 0px; padding: 0px;">r e s [ i − 1 ] res[i - 1] </span><span class="katex-html" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.02778em 0px 0px; padding: 0px;">r</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">e</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">s</span><span class="mopen" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px; padding: 0px;">i</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span><span class="mbin" style="box-sizing: border-box; margin: 0px; padding: 0px;">−</span><span class="mspace" style="box-sizing: border-box; margin: 0px 0.222222em 0px 0px; padding: 0px;"></span></span><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord" style="box-sizing: border-box; margin: 0px; padding: 0px;">1</span><span class="mclose" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span></span></span></span></span> 位置来说,所有它右边位置的 1 少移动了一次,它左边位置的 1 多移动了一次。</p><p style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", 微软雅黑; font-size: 16px; margin: 0px 0px 16px; overflow-wrap: break-word; padding: 0px;">时间复杂度:<span class="katex--inline" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-mathml" style="box-sizing: border-box; margin: 0px; padding: 0px;"> O ( N ) O(N) </span><span class="katex-html" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.02778em 0px 0px; padding: 0px;">O</span><span class="mopen" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.10903em 0px 0px; padding: 0px;">N</span><span class="mclose" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span></span></span></span></span>,空间复杂度:<span class="katex--inline" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-mathml" style="box-sizing: border-box; margin: 0px; padding: 0px;"> O ( 1 ) O(1) </span><span class="katex-html" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.02778em 0px 0px; padding: 0px;">O</span><span class="mopen" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span><span class="mord" style="box-sizing: border-box; margin: 0px; padding: 0px;">1</span><span class="mclose" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span></span></span></span></span>.</p><pre style="background: rgb(240, 240, 240); border-radius: 3px; box-sizing: border-box; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; margin-bottom: 15px; margin-top: 15px; overflow-wrap: normal; overflow: auto; padding: 10px 5px 10px 10px;"></pre><p style="background-color: white; line-height: inherit; margin: 22px 0px; padding: 0px;"><span style="color: #282d36; font-family: Microsoft YaHei, Arial, Helvetica, sans-serif;"><a href="https://shareablecode.com/snippets/minimum-number-of-operations-to-move-all-balls-to-each-box-c-solution-leetcode-u8Vu-zhvF">https://shareablecode.com/snippets/minimum-number-of-operations-to-move-all-balls-to-each-box-c-solution-leetcode-u8Vu-zhvF</a></span></p><pre class="snippet-pre language-cpp" style="background: rgb(245, 242, 240); border-top: 1px solid rgb(230, 229, 225); box-sizing: inherit; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-size: 15px; hyphens: none; line-height: 1.5; margin-bottom: 0.5em; margin-top: 0px; overflow-wrap: normal; overflow: auto; padding: 1em; tab-size: 4; text-shadow: rgb(255, 255, 255) 0px 1px; word-break: normal;"><code class=" language-cpp" style="background: 0px 0px; box-sizing: inherit; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-size: 1em; hyphens: none; line-height: 1.5; overflow-wrap: normal; tab-size: 4; text-shadow: rgb(255, 255, 255) 0px 1px; word-break: normal; word-spacing: normal;"> vector<span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;"><</span><span class="token keyword" style="box-sizing: inherit; color: #0077aa;">int</span><span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">></span> <span class="token function" style="box-sizing: inherit; color: #dd4a68;">minOperations</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">(</span>string boxes<span class="token punctuation" style="box-sizing: inherit; color: #999999;">)</span> <span class="token punctuation" style="box-sizing: inherit; color: #999999;">{</span>
vector<span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;"><</span><span class="token keyword" style="box-sizing: inherit; color: #0077aa;">int</span><span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">></span> <span class="token function" style="box-sizing: inherit; color: #dd4a68;">result</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">(</span><span class="token function" style="box-sizing: inherit; color: #dd4a68;">size</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">(</span>boxes<span class="token punctuation" style="box-sizing: inherit; color: #999999;">)</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">)</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span>
<span class="token keyword" style="box-sizing: inherit; color: #0077aa;">for</span> <span class="token punctuation" style="box-sizing: inherit; color: #999999;">(</span><span class="token keyword" style="box-sizing: inherit; color: #0077aa;">int</span> i <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">=</span> <span class="token number" style="box-sizing: inherit; color: #990055;">0</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">,</span> accu <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">=</span> <span class="token number" style="box-sizing: inherit; color: #990055;">0</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">,</span> cnt <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">=</span> <span class="token number" style="box-sizing: inherit; color: #990055;">0</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span> i <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;"><</span> <span class="token function" style="box-sizing: inherit; color: #dd4a68;">size</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">(</span>boxes<span class="token punctuation" style="box-sizing: inherit; color: #999999;">)</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">++</span>i<span class="token punctuation" style="box-sizing: inherit; color: #999999;">)</span> <span class="token punctuation" style="box-sizing: inherit; color: #999999;">{</span>
result<span class="token punctuation" style="box-sizing: inherit; color: #999999;">[</span>i<span class="token punctuation" style="box-sizing: inherit; color: #999999;">]</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">+=</span> accu<span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span>
cnt <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">+=</span> <span class="token punctuation" style="box-sizing: inherit; color: #999999;">(</span>boxes<span class="token punctuation" style="box-sizing: inherit; color: #999999;">[</span>i<span class="token punctuation" style="box-sizing: inherit; color: #999999;">]</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">==</span> <span class="token string" style="box-sizing: inherit; color: #669900;">'1'</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">)</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">?</span> <span class="token number" style="box-sizing: inherit; color: #990055;">1</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">:</span> <span class="token number" style="box-sizing: inherit; color: #990055;">0</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span>
accu <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">+=</span> cnt<span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span>
<span class="token punctuation" style="box-sizing: inherit; color: #999999;">}</span>
<span class="token keyword" style="box-sizing: inherit; color: #0077aa;">for</span> <span class="token punctuation" style="box-sizing: inherit; color: #999999;">(</span><span class="token keyword" style="box-sizing: inherit; color: #0077aa;">int</span> i <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">=</span> <span class="token function" style="box-sizing: inherit; color: #dd4a68;">size</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">(</span>boxes<span class="token punctuation" style="box-sizing: inherit; color: #999999;">)</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">-</span> <span class="token number" style="box-sizing: inherit; color: #990055;">1</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">,</span> accu <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">=</span> <span class="token number" style="box-sizing: inherit; color: #990055;">0</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">,</span> cnt <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">=</span> <span class="token number" style="box-sizing: inherit; color: #990055;">0</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span> i <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">>=</span> <span class="token number" style="box-sizing: inherit; color: #990055;">0</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">--</span>i<span class="token punctuation" style="box-sizing: inherit; color: #999999;">)</span> <span class="token punctuation" style="box-sizing: inherit; color: #999999;">{</span>
result<span class="token punctuation" style="box-sizing: inherit; color: #999999;">[</span>i<span class="token punctuation" style="box-sizing: inherit; color: #999999;">]</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">+=</span> accu<span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span>
cnt <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">+=</span> <span class="token punctuation" style="box-sizing: inherit; color: #999999;">(</span>boxes<span class="token punctuation" style="box-sizing: inherit; color: #999999;">[</span>i<span class="token punctuation" style="box-sizing: inherit; color: #999999;">]</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">==</span> <span class="token string" style="box-sizing: inherit; color: #669900;">'1'</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">)</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">?</span> <span class="token number" style="box-sizing: inherit; color: #990055;">1</span> <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">:</span> <span class="token number" style="box-sizing: inherit; color: #990055;">0</span><span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span>
accu <span class="token operator" style="background: rgba(255, 255, 255, 0.5); box-sizing: inherit; color: #9a6e3a;">+=</span> cnt<span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span>
<span class="token punctuation" style="box-sizing: inherit; color: #999999;">}</span>
<span class="token keyword" style="box-sizing: inherit; color: #0077aa;">return</span> result<span class="token punctuation" style="box-sizing: inherit; color: #999999;">;</span>
<span class="token punctuation" style="box-sizing: inherit; color: #999999;">}</span>
</code></pre><div><br /></div><p style="background-color: white; line-height: inherit; margin: 22px 0px; padding: 0px;"><span style="color: #282d36; font-family: Microsoft YaHei, Arial, Helvetica, sans-serif;"><span style="font-size: 15px;">X.</span></span></p><p style="background-color: white; box-sizing: border-box; margin: 0px 0px 16px; overflow-wrap: break-word; padding: 0px;"><span style="color: #24292e; font-family: -apple-system, system-ui, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, 微软雅黑;"><a href="https://www.taodudu.cc/news/show-2114813.html">https://www.taodudu.cc/news/show-2114813.html</a></span></p><p style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", 微软雅黑; font-size: 16px; margin: 0px 0px 16px; overflow-wrap: break-word; padding: 0px;">本题的数据规模为 2000,用 <span class="katex--inline" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="katex-mathml" style="box-sizing: border-box; margin: 0px; padding: 0px;">O ( N 2 ) O(N^2) </span><span class="katex-html" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="base" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="strut" style="box-sizing: border-box; height: 1.06411em; margin: 0px; padding: 0px; vertical-align: -0.25em;"></span><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.02778em 0px 0px; padding: 0px;">O</span><span class="mopen" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span><span class="mord" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="mord mathdefault" style="box-sizing: border-box; margin: 0px 0.10903em 0px 0px; padding: 0px;">N</span><span class="msupsub" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="vlist-t" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="vlist-r" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="vlist" style="box-sizing: border-box; height: 0.814108em; margin: 0px; padding: 0px;"><span class="" style="box-sizing: border-box; margin: 0px 0.05em 0px 0px; padding: 0px; top: -3.063em;"><span class="pstrut" style="box-sizing: border-box; height: 2.7em; margin: 0px; padding: 0px;"></span><span class="sizing reset-size6 size3 mtight" style="box-sizing: border-box; margin: 0px; padding: 0px;"><span class="mord mtight" style="box-sizing: border-box; margin: 0px; padding: 0px;">2</span></span></span></span></span></span></span></span><span class="mclose" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span></span></span></span></span> 的解法竟然能过。这个方法比较简单,直接对每个位置向左右两边统计具体各个 1 的位置之和。</p><div><pre style="background: rgb(240, 240, 240); border-radius: 3px; box-sizing: border-box; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; margin-bottom: 15px; margin-top: 15px; overflow-wrap: normal; overflow: auto; padding: 10px 5px 10px 10px;"><code class="prism language-python" style="background: none rgb(45, 45, 45); border-radius: 4px; box-sizing: border-box; color: #cccccc; display: block; font-family: Consolas, Inconsolata, Courier, monospace; line-height: 22px; margin: 0px; min-width: 94%; overflow-wrap: normal; overflow-x: auto; padding: 0px !important; word-break: normal;"> <span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">def</span> <span class="token function" style="box-sizing: border-box; margin: 0px; padding: 0px;">minOperations</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span>self<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">,</span> boxes<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span> <span class="token builtin" style="box-sizing: border-box; margin: 0px; padding: 0px;">str</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span> <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span><span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">></span> List<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span class="token builtin" style="box-sizing: border-box; margin: 0px; padding: 0px;">int</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span>
N <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span> <span class="token builtin" style="box-sizing: border-box; margin: 0px; padding: 0px;">len</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span>boxes<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span>
res <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span> <span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span><span class="token number" style="box-sizing: border-box; margin: 0px; padding: 0px;">0</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span> <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">*</span> N
<span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">for</span> i <span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">in</span> <span class="token builtin" style="box-sizing: border-box; margin: 0px; padding: 0px;">range</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span>N<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">:</span>
left <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span> <span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span>i <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span> j <span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">for</span> j <span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">in</span> <span class="token builtin" style="box-sizing: border-box; margin: 0px; padding: 0px;">range</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span>i<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span> <span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">if</span> boxes<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span>j<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span> <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">==</span> <span class="token string" style="box-sizing: border-box; margin: 0px; padding: 0px;">"1"</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span>
right <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span> <span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span>j <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">-</span> i <span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">for</span> j <span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">in</span> <span class="token builtin" style="box-sizing: border-box; margin: 0px; padding: 0px;">range</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span>i <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">+</span> <span class="token number" style="box-sizing: border-box; margin: 0px; padding: 0px;">1</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">,</span> N<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span> <span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">if</span> boxes<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span>j<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span> <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">==</span> <span class="token string" style="box-sizing: border-box; margin: 0px; padding: 0px;">"1"</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span>
res<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">[</span>i<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">]</span> <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">=</span> <span class="token builtin" style="box-sizing: border-box; margin: 0px; padding: 0px;">sum</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span>left<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span> <span class="token operator" style="box-sizing: border-box; margin: 0px; padding: 0px;">+</span> <span class="token builtin" style="box-sizing: border-box; margin: 0px; padding: 0px;">sum</span><span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">(</span>right<span class="token punctuation" style="box-sizing: border-box; margin: 0px; padding: 0px;">)</span>
<span class="token keyword" style="box-sizing: border-box; margin: 0px; padding: 0px;">return</span> res</code></pre></div><p style="background-color: white; line-height: inherit; margin: 22px 0px; padding: 0px;"><span style="color: #282d36; font-family: Microsoft YaHei, Arial, Helvetica, sans-serif;"><span style="font-size: 15px;"><a href="https://kickstart.best/1769-minimum-number-of-operations-to-move-all-balls-to-each-box/">https://kickstart.best/1769-minimum-number-of-operations-to-move-all-balls-to-each-box/</a></span></span></p><div class="" style="background-color: #fffcd3; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #444444; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> vector</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"><</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">int</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">></span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-m0" style="box-sizing: inherit; color: #0086b3; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">minOperations</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">(</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">string boxes</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">)</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">{</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"></span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> int n = boxes.</span><span class="enlighter-m3" style="box-sizing: inherit; color: #0086b3; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">size</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">()</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">;</span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> vector</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"><</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">int</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">></span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-m0" style="box-sizing: inherit; color: #0086b3; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">ret</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">(</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">n, </span><span class="enlighter-n1" style="box-sizing: inherit; color: #009999; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">0</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">)</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">;</span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"></span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-k1" style="box-sizing: inherit; color: #286491; font-family: inherit; font-size: 1em; font-weight: 700; line-height: inherit; margin: 0px; padding: 0px;">for</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">(</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">int i = </span><span class="enlighter-n1" style="box-sizing: inherit; color: #009999; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">0</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">; i </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"><</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> n; ++i</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">)</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">{</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"></span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-k1" style="box-sizing: inherit; color: #286491; font-family: inherit; font-size: 1em; font-weight: 700; line-height: inherit; margin: 0px; padding: 0px;">for</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">(</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">int j = </span><span class="enlighter-n1" style="box-sizing: inherit; color: #009999; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">0</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">; j </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"><</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> n; ++j</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">)</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">{</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"></span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-k1" style="box-sizing: inherit; color: #286491; font-family: inherit; font-size: 1em; font-weight: 700; line-height: inherit; margin: 0px; padding: 0px;">if</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">(</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">boxes</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">[</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">j</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">]</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> == </span><span class="enlighter-s0" style="box-sizing: inherit; color: #dd1144; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">'1'</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">)</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">{</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"></span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> ret</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">[</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">i</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">]</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> += </span><span class="enlighter-m0" style="box-sizing: inherit; color: #0086b3; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">abs</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">(</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">i - j</span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">)</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">;</span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">}</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"></span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">}</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"></span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">}</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"></span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"></span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-k1" style="box-sizing: inherit; color: #286491; font-family: inherit; font-size: 1em; font-weight: 700; line-height: inherit; margin: 0px; padding: 0px;">return</span><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> ret;</span></div></div><div class="" style="background-color: #f4f4f4; border: 0px solid rgb(255, 255, 255); box-sizing: inherit; color: #aaaaaa; display: table-row; font-family: "Source Code Pro", "Liberation Mono", "Courier New", Courier, monospace; font-size: 12px; line-height: 1.35em; list-style: none; margin: 0px; min-height: 14px; white-space: pre-wrap;"><div style="box-sizing: inherit; display: table-cell; padding-left: 10px;"><span class="enlighter-text" style="box-sizing: inherit; color: black; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;"> </span><span class="enlighter-g1" style="box-sizing: inherit; color: #777777; font-family: inherit; font-size: 1em; line-height: inherit; margin: 0px; padding: 0px;">}</span></div></div><p style="background-color: white; color: #282d36; font-family: "Microsoft YaHei", Arial, Helvetica, sans-serif; font-size: 15px; line-height: inherit; margin: 22px 0px; padding: 0px;"><br /></p>Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-84104758567405597582021-01-28T20:33:00.000-08:002019-07-02T00:32:22.050-07:00Archives<script>function LoadTheArchive(e){var a=new Array,r=new Array,t=new Array,i=new Array,n=new Array;if("entry"in e.feed)for(var s=e.feed.entry.length,p=0;p<s;p++){var o=e.feed.entry[p];a.push(o.title.$t),t.push(o.published.$t.substring(0,4)),i.push(o.published.$t.substring(5,7)),n.push(o.published.$t.substring(8,10));for(var u,l=0;l<o.link.length;l++)if("alternate"==o.link[l].rel){u=o.link[l].href;break}r.push(u)}DisplaytheTOC(a,r,t,i,n)}function DisplaytheTOC(e,a,r,t,i){for(var n=["January","February","March","April","May","June","July","August","September","October","November","December"],s=e.length,p="",o="",u=0;u<s;u++){NameOfMonth=n[parseInt(t[u],10)-1],p==NameOfMonth&&o==r[u]||(p=NameOfMonth,o=r[u],document.write("<br><div class='dateStyle'>"+p+" "+o+"</div>"));parseInt(i[u],10)>9?""+parseInt(i[u],10):"0"+parseInt(i[u],10);document.write("<a href='"+a[u]+"'>"+e[u]+"</a><br>")}}function writeNavigation(){document.write('<a href="javascript:goto(--page)">Previous</a> ');for(var e=1;(e-1)*pageSize<maxPostSize;e++)document.write('<a href="javascript:goto(',e,')">',e+"</a> ");document.write(' <a href="javascript:goto(++page)">Next</a>')}function goto(e){e<1&&(e=1),e>Math.ceil(maxPostSize/e)&&(e=e>Math.ceil(maxPostSize/e)),urlParams.set("page",e),urlParams.set("pageSize",pageSize),window.location.search=urlParams.toString()}var maxPostSize=5400,urlParams=new URLSearchParams(window.location.search),page=urlParams.get("page");(null==page||page<1)&&(page=1);var pageSize=urlParams.get("pageSize");null==pageSize&&(pageSize=null!=urlParams.get("m")&&1==urlParams.get("m")?50:500);var startIndex=(page-1)*pageSize+1;writeNavigation(),document.write("</br>"),document.write('<script src="/feeds/posts/summary?alt=json-in-script&max-results=',pageSize,"&start-index=",startIndex,'&callback=LoadTheArchive" type="text/javascript">',"<\/script>"),document.write("</br>"),writeNavigation()</script><br>
Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-30823817233330694022021-01-27T12:40:00.000-08:002019-07-07T08:36:39.884-07:00Algorithm Interview<script type="text/javascript">
//<![CDATA[
var totalposts = 10000;
var standardstyling = true;
function showrecentposts(json) {
for (var a = 0; a < totalposts; a++) {
var entry = json.feed.entry[a];
if(entry ==null) break;
var posttitle = entry.title.$t;
var posturl;
if (a == json.feed.entry.length) break;
for (var b = 0; b < entry.link.length; b++) {
if (entry.link[b].rel == 'alternate') {
posturl = entry.link[b].href;
break;
}
}
if(posttitle == "Algorithm Interview") continue;
posttitle = posttitle.link(posturl);
if (standardstyling) document.write('<li>');
document.write(posttitle);
if (standardstyling) document.write('</li>');
}
}
//]]>
</script>
<div class='cse'>
<form action='#' id='cse-search-box'>
<table>
<tr>
<td><input id="searchInput" type='text' name='q' size='80' /></td>
<td><input type="submit" name="sa" value="Search" /></td>
</tr>
</table>
<input name='cx' type='hidden' value='000071145641082027410:f_f4lunhrg8'/>
<input name='ie' type='hidden' value='ISO-8859-1'/>
</form>
<script type="text/javascript" src="https://www.google.com/cse/brand?form=cse-search-box&lang=en"></script>
</div>
<script async src="https://cse.google.com/cse.js?cx=000071145641082027410:f_f4lunhrg8"></script>
<div class="gcse-searchresults-only"></div>
<script>
var urlParams = new URLSearchParams(window.location.search);
var q = urlParams.get('q');
if(q) {
document.getElementById("searchInput").value = q;
}
</script>
<h3>
<a href="/2019/06/archives.html">Archives</a></h3>
<li><a href="/2019/06/archives.html">Archives</a></li>
<br/>
<h3>New/Updated Posts</h3>
<script src="https://massivealgorithms.blogspot.com/feeds/posts/default/?orderby=published&alt=json-in-script&callback=showrecentposts&max-results=21"></script>
<br/>
<h3>Algorithm Questions</h3>
<script src="https://massivealgorithms.blogspot.com/feeds/posts/default/-/LeetCode - Review?orderby=updated&alt=json-in-script&callback=showrecentposts&max-results=9999"></script>Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-34395970714800116362021-01-06T23:38:00.000-08:002019-07-07T08:32:50.261-07:00Page Not Found<script type="text/javascript">
//<![CDATA[
var urlParams = new URLSearchParams(window.location.search);
var redirectPath = urlParams.get("redirectPath")
function redirectIfNeeded() {
var redirectMap = new Map();
redirectMap.set("/2014/07/given-array-of-integers-sort-into-wave.html", "/2014/07/leetcode-280-wiggle-sort.html");
if (redirectPath) {
var redirect = redirectMap.get(redirectPath);
if (redirect) {
window.location = redirect;
return true;
}
}
return false;
}
var redirected = redirectIfNeeded();
// if (!redirected) {
// var fileName = redirectPath.split("/").slice(-1)[0];
// if(fileName.endsWith(".html"))
// {
// fileName = fileName.substr(0, fileName.length-".html".length)
// }
// var url = "http://www.google.com/search?as_sitesearch=massivealgorithms.blogspot.com&as_oq=" + fileName.split('-').join(" ")
// newTab = window.open();
// newTab.location.href = url;
// // setTimeout(() => window.open(url, '_blank'), 3000);
// }
function googleSearch() {
var fileName = redirectPath.split("/").slice(-1)[0];
if (fileName.endsWith(".html")) {
fileName = fileName.substr(0, fileName.length - ".html".length)
}
var googleUrl = "http://www.google.com/search?as_sitesearch=massivealgorithms.blogspot.com&as_oq=" + fileName.split('-').join(" ")
window.open(googleUrl, '_blank');
return false;
}
var totalposts = 10000;
var standardstyling = true;
function showrecentposts(json) {
for (var a = 0; a < totalposts; a++) {
var entry = json.feed.entry[a];
var posttitle = entry.title.$t;
var posturl;
if (a == json.feed.entry.length) break;
for (var b = 0; b < entry.link.length; b++) {
if (entry.link[b].rel == 'alternate') {
posturl = entry.link[b].href;
break;
}
}
if (posttitle == "Algorithm Interview") continue;
posttitle = posttitle.link(posturl);
if (standardstyling) document.write('<li>');
document.write(posttitle);
if (standardstyling) document.write('</li>');
}
}
setTimeout(function () {
window.location = '/2019/06/archives.html'
}, 10000);
//]]>
</script>
<br />
<br />
<h3>
Sorry, the page you were looking for in this blog does not exist. </h3>
<h3><a id="searchLink" href="#" target="_blank" onclick="googleSearch()">Click Here to Find the Lost Page in Google</a></h3>
<br>
<h3>
Will jump to <a href="/2019/06/archives.html">archives</a> after 10 seconds...</h3>
<br />
<h3>Start exploring here:</h3>
<h3>
<a href="https://www.blogger.com/p/archives.html">Archives</a></h3>
<li><a href="https://www.blogger.com/p/archives.html">Archives</a></li>
<br />
<h3>
New/Updated Posts</h3>
<script src="https://massivealgorithms.blogspot.com/feeds/posts/default/?orderby=published&alt=json-in-script&callback=showrecentposts&max-results=21"></script>
<br />
<h3>
Algorithm Questions</h3>
<script src="https://massivealgorithms.blogspot.com/feeds/posts/default/-/LeetCode - Review?orderby=updated&alt=json-in-script&callback=showrecentposts&max-results=9999"></script>Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-64161392767565598712020-02-04T15:09:00.000-08:002020-02-04T15:09:55.566-08:00Google | Onsite | Timer<a href="https://leetcode.com/discuss/interview-question/350166/google-onsite-timer">https://leetcode.com/discuss/interview-question/350166/google-onsite-timer</a><br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Given the following class with 2 methods</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: #5c2699;">MySystem</span> {</span>
<span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">/**
* Waits durationMillis milliseconds then runs the callback.
*
* Only one timer can be pending at one time. If called again before the
* duration expires, the original timer is overwritten without running
* callback.
*/</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">static</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">setTimer</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">long</span> durationMillis, Runnable callback)</span></span>;
<span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">/** Returns the current time in milliseconds since system boot. */</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">static</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">long</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">getCurrentTimeMillis</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">()</span></span>;
}
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Use these methods to implement the 3rd method to support multiple timers:</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">/**
* Waits durationMillis milliseconds then runs the callback.
*
* Supports multiple concurrent timers. Calling addTimer will not break
* any previously started timers.
*/</span>
void add<span class="hljs-constructor" style="box-sizing: border-box;">Timer(<span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">long</span> <span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">durationMillis</span>, Runnable <span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">callback</span>)</span>;
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Usage is as follows:</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;">MySystem sys = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-constructor" style="box-sizing: border-box;">MySystem()</span>;
sys.add<span class="hljs-constructor" style="box-sizing: border-box;">Timer(100, <span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">runnable1</span>)</span>;
<span class="hljs-module-access" style="box-sizing: border-box;"><span class="hljs-module" style="box-sizing: border-box;"><span class="hljs-identifier" style="box-sizing: border-box;">Thread</span>.</span></span>sleep(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">50</span>);
sys.add<span class="hljs-constructor" style="box-sizing: border-box;">Timer(100, <span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">runnable2</span>)</span>;</code></pre>
<br />
You can use a priority queue to keep all the timers with minimum time. You can maintain your own Runnable which will get's triggered when the timer goes off. At that time you can pop the next time and call the setTime api. NOTE : You need to subtract the time you have already elapsed when you trigger next alarm.<br />
<br />
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">interface</span> <span class="hljs-title" style="box-sizing: border-box; color: #5c2699;">MySystem</span> </span>{
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">setTimer</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">long</span> durationMillis, Runnable callback)</span></span>;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">long</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">getCurrentTimeMillis</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">()</span></span>;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">addTimer</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">long</span> durationMillis, Runnable callback)</span></span>;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">abstract</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: #5c2699;">AbstractMySystem</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: #5c2699;">MySystem</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">private</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">final</span> NavigableMap<Long, List<Runnable>>
callbacks = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> TreeMap<>();
<span class="hljs-meta" style="box-sizing: border-box; color: #1c00cf;">@Override</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">addTimer</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">long</span> durationMillis, Runnable callback)</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">synchronized</span> (callbacks) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">assert</span> durationMillis >= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">long</span> scheduledTime = getCurrentTimeMillis() + durationMillis;
callbacks.computeIfAbsent(scheduledTime, t -> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> ArrayList<>()).add(callback);
reschedule();
}
}
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">private</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">reschedule</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">()</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">synchronized</span> (callbacks) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">final</span> Long next = callbacks.firstKey();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (next != <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">null</span>) {
setTimer(Math.max(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, next - getCurrentTimeMillis()), () -> {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">synchronized</span> (callbacks) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (Runnable runnable : callbacks.get(next)) {
setTimer(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, runnable);
}
callbacks.remove(next);
reschedule();
}
});
}
}
}
}</code></pre>
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-78010781664532347012020-01-12T14:28:00.004-08:002020-01-12T14:28:59.788-08:00Google Interview Questions<a href="https://leetcode.com/discuss/interview-question/364396/">https://leetcode.com/discuss/interview-question/364396/</a><br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Design a data structure to calculate the moving product of all elements in a sliding window of size <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">k</code>.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: #5c2699;">SlidingWindow</span> {</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">SlidingWindow</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> k)</span> </span>{
}
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">add</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> val)</span> </span>{
}
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">getProduct</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">()</span> </span>{
}
}
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
All methods should work in O(1) time.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example:</span></div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;">SlidingWindow <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">window</span> = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> SlidingWindow(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">3</span>);
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">window</span>.add(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>); <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// [1]</span>
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">window</span>.add(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>); <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// [1, 2]</span>
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">window</span>.getProduct(); <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// 2</span>
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">window</span>.add(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">3</span>); <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// [1, 2, 3]</span>
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">window</span>.getProduct(); <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// 6</span>
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">window</span>.add(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">4</span>); <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// [2, 3, 4]</span>
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">window</span>.getProduct(); <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// 24</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Follow-up:</span><br style="box-sizing: border-box;" />What if <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">k</code> is a method argument instead of constructor?</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">getProduct</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> k)</span> </span>{
}
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px;">
You can assume that a product will fit into a single 32-bit integer without overflowing</div>
<br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Follow-up</span><br style="box-sizing: border-box;" />Solution with additional space complexity of O(N) where N = number of elements added to the SlidingWindow so far</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<em style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: bolder;">Intuition 1:</span></em> All elements have to be retained as k can vary dynamically.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<em style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: bolder;">Intuition 2:</span></em> If all elements were > 0, then we could say:</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">Product</span> of <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">Last</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">K</span> elements = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">Product</span> of all elements till <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">N</span> / <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">Product</span> of elements uptil <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">N</span> - <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">K</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
So, If elements added were [ 1, 4, 7, 2, 5] and K = 2<br style="box-sizing: border-box;" />Then Product of last 2 elements = 2 * 5 = ( 1 * 4 * 7 * 2 * 5 ) / ( 1 * 4 * 7 * 2 * 5)</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
What happens if 0 is allowed? Product after encountering it becomes 0 and the above formula cannot be used any longer. Allowance for 0 has to be made</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<em style="box-sizing: border-box;"><span style="box-sizing: border-box; font-weight: bolder;">Intuition 3:</span></em> If 0 exists in last k elements, then we can directly return 0.<br style="box-sizing: border-box;" />We need to keep track of the last occurrence of 0. If the index of the last occurrence of 0 is within range N to N - K then return 0</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Using the above,<br style="box-sizing: border-box;" />let leftProduct[i] = product of all non-zero elements from 1..i</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">add(val)</span></div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(val == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) {
lastIndexOfZero = currentIndex
leftProduct[currentIndex] = leftProduct[currentIndex - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>]
} <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span> {
leftProduct[currentIndex] = leftProduct[currentIndex - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>] * val
}
currentIndex++
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">getProduct(k)</span></div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (lastIndexOfZero >= N - k) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>
} <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span> {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> leftProduct[N<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>] / leftProduct[N-K]
}</code></pre>
Unknownnoreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-79663825152875702012020-01-02T23:09:00.003-08:002020-01-02T23:09:44.678-08:00LeetCode 1246 - Palindrome Removal<a href="https://algorithm-notes-allinone.blogspot.com/2019/11/leetcode-1246-palindrome-removal.html">https://algorithm-notes-allinone.blogspot.com/2019/11/leetcode-1246-palindrome-removal.html</a><br />
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
Given an integer array <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">arr</code>, in one move you can select a <span style="box-sizing: border-box; font-weight: 700;">palindromic</span> subarray <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">arr[i], arr[i+1], ..., arr[j]</code> where <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">i <= j</code>, and remove that subarray from the given array. Note that after removing a subarray, the elements on the left and on the right of that subarray move to fill the gap left by the removal.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
Return the minimum number of moves needed to remove all numbers from the array.</div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
<span style="box-sizing: border-box; font-weight: 700;">Example 1:</span></div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow-wrap: break-word; overflow: auto; padding: 9.5px; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 700;">Input:</span> arr = [1,2]
<span style="box-sizing: border-box; font-weight: 700;">Output:</span> 2
</pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
<span style="box-sizing: border-box; font-weight: 700;">Example 2:</span></div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow-wrap: break-word; overflow: auto; padding: 9.5px; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 700;">Input:</span> arr = [1,3,4,1,5]
<span style="box-sizing: border-box; font-weight: 700;">Output:</span> 3
<span style="box-sizing: border-box; font-weight: 700;">Explanation: </span>Remove [4] then remove [1,3,1] then remove [5].
</pre>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
<span style="box-sizing: border-box; font-weight: 700;">Constraints:</span></div>
<ul style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.4; margin: 0px 0px 10px; padding: 0px 2.5em;">
<li style="box-sizing: border-box; margin: 0px 0px 0.25em; padding: 0px;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">1 <= arr.length <= 100</code></li>
<li style="box-sizing: border-box; margin: 0px 0px 0.25em; padding: 0px;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">1 <= arr[i] <= 20</code></li>
</ul>
<span style="background-color: #fff9ee; color: #222222; font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15.4px;">This question can be solved by dp. So the key is find the key logic, or state transfer equation.</span><br />
<br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" />
<span style="background-color: #fff9ee; color: #222222; font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15.4px;">Let use dp[i][j] represents the minimum number of moves needed to remove all the numbers from the array from i to j inclusive. Then,</span><br />
<br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" />
<span style="background-color: #fff9ee; color: #222222; font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15.4px;">(1) dp[i][j] = 1 + dp[i+1][j] //since it can ways be done by removing one by one;</span><br />
<br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" />
<span style="background-color: #fff9ee; color: #222222; font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15.4px;">(2) if(dp[i] == dp[i+1]), dp[i][j] = min(dp[i][j], 1 + dp[i+2][j]);</span><br />
<br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" />
<span style="background-color: #fff9ee; color: #222222; font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15.4px;">(3) if(dp[i] == dp[j]), dp[i][j] = min(dp[i][j], dp[i+1][j-1])//both ends can be removed along with previous palindromes;</span><br />
<br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" />
<span style="background-color: #fff9ee; color: #222222; font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15.4px;">(4) if(dp[i] == dp[k]) dp[i][j] = min(dp[i][j], dp[i][k-1] + dp[k+1][j]) for k = i + 2, to j - 1;</span><br />
<br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" />
<span style="background-color: #fff9ee; color: #222222; font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15.4px;">Pay attention to the boundary conditions. The above (2) and (3) actually are the left and right boundaries of case (4).</span><br />
<br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" />
<span style="background-color: #fff9ee; color: #222222; font-family: "georgia" , "utopia" , "palatino linotype" , "palatino" , serif; font-size: 15.4px;">Also pay attention to the for loops. The time complexity is O(N^3), and the dp is in a bottom-up building fashion. So we need to calculate each subarr with a smaller size or len. Then gradually build up the final dp[0][len-1].</span><br />
<ol class="linenums" style="color: #222222; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; margin-bottom: 0px; margin-top: 0px; white-space: pre;">
<li class="L2" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> minimumMoves</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">vector</span><span class="str" style="color: #008800;"><int></span><span class="pun" style="color: #666600;">&</span><span class="pln" style="color: black;"> arr</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L3" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> n </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> arr</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">size</span><span class="pun" style="color: #666600;">();</span></li>
<li class="L4" style="margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> vector</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">vector</span><span class="str" style="color: #008800;"><int></span><span class="pun" style="color: #666600;">></span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">n</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> vector</span><span class="str" style="color: #008800;"><int></span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">n</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">));</span></li>
<li class="L5" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">for</span><span class="pun" style="color: #666600;">(</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> len </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> len </span><span class="pun" style="color: #666600;"><=</span><span class="pln" style="color: black;"> n</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">++</span><span class="pln" style="color: black;">len</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L6" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">for</span><span class="pun" style="color: #666600;">(</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> i</span><span class="pun" style="color: #666600;">=</span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> j </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> len</span><span class="pun" style="color: #666600;">-</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> j</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">n</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">++</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">++</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L7" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">if</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">len </span><span class="pun" style="color: #666600;">==</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span><span class="pln" style="color: black;">dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">continue</span><span class="pun" style="color: #666600;">;}</span></li>
<li class="L8" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">+</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">];</span><span class="com" style="color: #880000;">//can always remove the first one;</span></li>
<li class="L9" style="background: rgb(238, 238, 238); margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">if</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">arr</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">==</span><span class="pln" style="color: black;"> arr</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">])</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> min</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">],</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">+</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]);</span></li>
<li class="L0" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">if</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">arr</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">==</span><span class="pln" style="color: black;"> arr</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">&&</span><span class="pln" style="color: black;"> j</span><span class="pun" style="color: #666600;">></span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> min</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">],</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">-</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">]);</span></li>
<li class="L1" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">for</span><span class="pun" style="color: #666600;">(</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> k</span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> k</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">++</span><span class="pln" style="color: black;">k</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L2" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">if</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">arr</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">==</span><span class="pln" style="color: black;"> arr</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">k</span><span class="pun" style="color: #666600;">])</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> min</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">],</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">k</span><span class="pun" style="color: #666600;">-</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">+</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">k</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]);</span></li>
<li class="L3" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L4" style="margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L5" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L6" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">return</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">n</span><span class="pun" style="color: #666600;">-</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">];</span></li>
<li class="L7" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
</ol>
<a href="https://leetcode.jp/leetcode-1246-palindrome-removal-%E8%A7%A3%E9%A2%98%E6%80%9D%E8%B7%AF%E5%88%86%E6%9E%90/">https://leetcode.jp/leetcode-1246-palindrome-removal-%E8%A7%A3%E9%A2%98%E6%80%9D%E8%B7%AF%E5%88%86%E6%9E%90/</a><br />
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
这是一道动态规划的题目,难度级别为Hard。我在之前的文章说过,所有动态规划(DP)题目都可以转化为递归加记忆数组来实现,本人更青睐于递归,因此本题仍然使用递归来解题。</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
我们先定义一个递归方法help,help(from, to)的返回值代表,删除区间[from, to]内所有数字所需要的次数。我们从from+1开始循环到to,用每一个数i与from作比较,如果两数(当前数i和from)相同,分两种情况考虑,若当前数i与from相邻,说明这两个数可以组成回文,因此,可以使用一次操作删掉这两个数字,即:</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">公式1:help(from, to) = 1 + help(i+1, to)。</strong></div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
若当前数i与from不相邻,那么<strong style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">当前数与from</strong>是可以融合到区间[from+1, i-1]之内,也就是说, <strong style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">help(from, i) == help(from+1, to-1)</strong>。这里解释一下原因,举个例子,比如区间[from, i]为:</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin: 0px; padding: 0px; vertical-align: baseline;">
<div class="syntaxhighlighter java" id="highlighter_766411" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 1em !important; margin: 1em 0px !important; overflow: auto hidden !important; padding: 0px; position: relative !important; vertical-align: baseline; width: 640px;">
<table border="0" cellpadding="0" cellspacing="0" style="background: none !important; border-collapse: collapse; border-radius: 0px !important; border-spacing: 0px; border: 1px solid rgb(231, 231, 231); bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px -1px 24px 0px; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; table-layout: auto !important; top: auto !important; vertical-align: baseline !important; width: 640px;"><caption style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0.5em 0px 0.5em 1em !important; position: static !important; right: auto !important; text-align: left !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">Code</caption><tbody style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<tr style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><td class="gutter" style="background: none !important; border-bottom: 0px !important; border-image: initial !important; border-left: 0px !important; border-radius: 0px !important; border-right: 0px !important; border-top: 1px solid rgb(231, 231, 231); bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(120, 120, 120) !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 6px 24px; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-bottom-color: initial !important; border-bottom-style: initial !important; border-image: initial !important; border-left-color: initial !important; border-left-style: initial !important; border-radius: 0px !important; border-right-color: rgb(212, 208, 200) !important; border-right-style: solid !important; border-top-color: initial !important; border-top-style: initial !important; border-width: 0px 3px 0px 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
1</div>
</td><td class="code" style="background: none !important; border-bottom: 0px !important; border-image: initial !important; border-left: 0px !important; border-radius: 0px !important; border-right: 0px !important; border-top: 1px solid rgb(231, 231, 231); bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 6px 24px; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: 603px;"><div class="container" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: relative !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">,</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">2</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">,</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">3</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">,</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">] </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 等于[2,3]</code></div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
将1,2,3,1全部删除需要2步:</div>
<ol style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 24px 1.5em; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">删除2,变为1,3,1</li>
<li style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">此时1,3,1为回文,可一次删除</li>
</ol>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
也就是说,除去区间首尾相同的元素之外,中间部分经过一系列删除之后,一定能剩下1个单独的数字或者一个单独的回文,不论是哪种情况,中间最后剩余的部分都能与区间首尾共同组成一个回文,因此首尾部分可以与中间剩余部分一次性删除掉。结论: 如果两数(当前数i和from)相同,并且当前数i与from不相邻时,</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">公式2:</strong> <strong style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">help(from, to) = help(from+1, i-1) + help(i+1, to)</strong></div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
当循环完区间内所有元素(从from+1循环到to)之后,我们还有另一种选择,即将首元素单独删除,剩下的部分交给递归子问题去解决。即:</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">公式3:</strong> <strong style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">help(from, to) = 1 + help(from+1, to)</strong></div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
有了上述3个公式,本题即可完美解决。另外别忘记添加记忆数组,以防止重复计算(相当于动态规划使用到的dp数组)。我们定义一个二维数组memo[][],memo[i][j]代表区间[i,j]。</div>
<div>
<div class="line number1 index0 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[][] memo; </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 记忆数组</code></div>
<div class="line number2 index1 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">public</code> <code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">minimumMoves(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[] arr) {</code></div>
<div class="line number3 index2 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 初始化记忆数组</code></div>
<div class="line number4 index3 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">memo = </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">new</code> <code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[arr.length][arr.length];</code></div>
<div class="line number5 index4 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">help(arr, </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">, arr.length - </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">);</code></div>
<div class="line number6 index5 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number7 index6 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
</div>
<div class="line number8 index7 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">help(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[] arr, </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">from, </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">to) {</code></div>
<div class="line number9 index8 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 区间范围不合理,返回0</code></div>
<div class="line number10 index9 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(to < from) </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;</code></div>
<div class="line number11 index10 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 区间长度为1,返回1</code></div>
<div class="line number12 index11 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(to == from) </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;</code></div>
<div class="line number13 index12 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 如果该区间曾经计算过,返回之前的计算结果</code></div>
<div class="line number14 index13 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(memo[from][to] != </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">) </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">memo[from][to];</code></div>
<div class="line number15 index14 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 返回值</code></div>
<div class="line number16 index15 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">res = Integer.MAX_VALUE;</code></div>
<div class="line number17 index16 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 区间首元素的值</code></div>
<div class="line number18 index17 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">fromValue=arr[from];</code></div>
<div class="line number19 index18 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 从区间第二个元素循环到区间尾部</code></div>
<div class="line number20 index19 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">i = from + </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">; i <= to; i++) {</code></div>
<div class="line number21 index20 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 如果当前元素等于区间首元素</code></div>
<div class="line number22 index21 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(fromValue == arr[i]) {</code></div>
<div class="line number23 index22 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">result = </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;</code></div>
<div class="line number24 index23 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(i-from==</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">){ </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 当前元素与首元素相邻</code></div>
<div class="line number25 index24 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">result = </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">+ help(arr, i + </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">, to);</code></div>
<div class="line number26 index25 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">else</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">{ </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 当前元素与首元素不相邻</code></div>
<div class="line number27 index26 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">result = help(arr, from + </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">, i - </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">)</code></div>
<div class="line number28 index27 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">+ help(arr, i + </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">, to);</code></div>
<div class="line number29 index28 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number30 index29 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 将最小值更新到返回结果</code></div>
<div class="line number31 index30 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">res = Math.min(res, result);</code></div>
<div class="line number32 index31 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number33 index32 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number34 index33 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 我们还可以选择将首字母单独删去后的情况</code></div>
<div class="line number35 index34 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">res = Math.min(res, </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">+ help(arr, from + </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">, to));</code></div>
<div class="line number36 index35 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 将本区间的计算结果保存至记忆数组,防止今后重复计算</code></div>
<div class="line number37 index36 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">memo[from][to] = res;</code></div>
<div class="line number38 index37 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">res;</code></div>
<div class="line number39 index38 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
</div>
<a href="https://nagato1208.github.io/2019/11/02/leetcode-1246-Palindrome-Removal/">https://nagato1208.github.io/2019/11/02/leetcode-1246-Palindrome-Removal/</a><br />
<div style="background-color: white; color: #555555; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 16px; margin-bottom: 20px; text-align: justify;">
性质题, 对于每一个数字都有两种选择: 1) 选一个和它一样的数字消去, 如果这两个数字的位置之间有其他数字, 当前消去的过程不增加remove的次数 2) 直接消去它自己, remove的次数+1.<br />
区间DP或者DFS+记忆</div>
<div style="background-color: white; color: #555555; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 16px; margin-bottom: 20px; text-align: justify;">
DP pattern:</div>
<br />
<figure class="highlight python" style="background: rgb(247, 247, 247); color: #4d4d4c; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 16px; line-height: 1.6; margin: 20px 0px; text-align: justify;"><div class="table-container" style="margin: 0px; overflow: auto;">
<table style="border-collapse: collapse; border-spacing: 0px; border: none; font-size: 0.875em; margin: 0px; width: auto;"><tbody>
<tr style="background-color: whitesmoke;"><td class="gutter" style="border: none; padding: 0px; user-select: none; vertical-align: middle;"><pre style="background: rgb(239, 242, 243); border: none; color: #869194; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 1em; line-height: 1.6; overflow: auto; padding: 10px; text-align: right;"><span class="line">1</span></pre>
</td><td class="code" style="border: none; padding: 0px; vertical-align: middle;"><pre style="background: rgb(247, 247, 247); border: none; color: #4d4d4c; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 1em; line-height: 1.6; overflow: auto; padding: 10px 0px 10px 10px; width: 609px;"><span class="line">dp[i][j] = min(dp[i][j], dp[i][k<span class="number" style="color: #f5871f;">-1</span>] + dp[k+<span class="number" style="color: #f5871f;">1</span>][j]), where arr[k]==arr[i]</span></pre>
</td></tr>
</tbody></table>
</div>
</figure><figure class="highlight python" style="background: rgb(247, 247, 247); color: #4d4d4c; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 16px; line-height: 1.6; margin: 20px 0px; text-align: justify;"><div class="table-container" style="margin: 0px; overflow: auto;">
<table style="border-collapse: collapse; border-spacing: 0px; border: none; font-size: 0.875em; margin: 0px; width: auto;"><tbody>
<tr style="background-color: whitesmoke;"><td class="code" style="border: none; padding: 0px; vertical-align: middle;"><pre style="background: rgb(247, 247, 247); border: none; color: #4d4d4c; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 1em; line-height: 1.6; overflow: auto; padding: 10px 0px 10px 10px; width: 558px;"><span class="line"> <span class="function" style="color: #4271ae;"><span class="keyword" style="color: #8959a8;">def</span> <span class="title" style="color: #3e999f;">minimumMoves</span><span class="params" style="color: #f5871f;">(self, arr)</span>:</span></span>
<span class="line"> <span class="string" style="color: #718c00;">"""</span></span>
<span class="line"><span class="string" style="color: #718c00;"> :type arr: List[int]</span></span>
<span class="line"><span class="string" style="color: #718c00;"> :rtype: int</span></span>
<span class="line"><span class="string" style="color: #718c00;"> """</span></span>
<span class="line"> memo = {}</span>
<span class="line"> d = collections.defaultdict(list)</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">for</span> i,c <span class="keyword" style="color: #8959a8;">in</span> enumerate(arr):</span>
<span class="line"> d[c].append(i)</span>
<span class="line"> </span>
<span class="line"> <span class="function" style="color: #4271ae;"><span class="keyword" style="color: #8959a8;">def</span> <span class="title" style="color: #3e999f;">dp</span><span class="params" style="color: #f5871f;">(i,j)</span>:</span></span>
<span class="line"> <span class="keyword" style="color: #8959a8;">if</span> i == j:</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">return</span> <span class="number" style="color: #f5871f;">1</span></span>
<span class="line"> <span class="keyword" style="color: #8959a8;">if</span> i > j:</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">return</span> <span class="number" style="color: #f5871f;">0</span></span>
<span class="line"> <span class="keyword" style="color: #8959a8;">if</span> (i,j) <span class="keyword" style="color: #8959a8;">not</span> <span class="keyword" style="color: #8959a8;">in</span> memo:</span>
<span class="line"> res = <span class="number" style="color: #f5871f;">1</span> + dp(i+<span class="number" style="color: #f5871f;">1</span>, j)</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">for</span> k <span class="keyword" style="color: #8959a8;">in</span> d[arr[i]]:</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">if</span> k == i + <span class="number" style="color: #f5871f;">1</span>:</span>
<span class="line"> res = min(res, <span class="number" style="color: #f5871f;">1</span> + dp(k+<span class="number" style="color: #f5871f;">1</span>, j))</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">elif</span> i + <span class="number" style="color: #f5871f;">1</span> < k <= j:</span>
<span class="line"> res = min(res, dp(i+<span class="number" style="color: #f5871f;">1</span>, k<span class="number" style="color: #f5871f;">-1</span>) + dp(k+<span class="number" style="color: #f5871f;">1</span>, j))</span>
<span class="line"> memo[i,j] = res</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">return</span> memo[i,j]</span>
<span class="line"> </span>
<span class="line"> <span class="keyword" style="color: #8959a8;">return</span> dp(<span class="number" style="color: #f5871f;">0</span>, len(arr)<span class="number" style="color: #f5871f;">-1</span>)</span></pre>
<div>
<span class="line"><br /></span></div>
</td></tr>
</tbody></table>
</div>
</figure><br />
<br />
<footer class="post-footer" style="-webkit-text-stroke-width: 0px; background-color: white; color: #555555; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></footer><br /><br />
<div class="post-body" itemprop="articleBody" style="-webkit-text-stroke-width: 0px; background-color: white; color: #555555; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; overflow-wrap: break-word; text-align: justify; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<br />
<figure class="highlight python" style="background: rgb(247, 247, 247); color: #4d4d4c; line-height: 1.6; margin: 20px 0px;"><div class="table-container" style="margin: 0px; overflow: auto;">
<table style="border-collapse: collapse; border-spacing: 0px; border: none; font-size: 0.875em; margin: 0px; width: auto;"><tbody>
<tr style="background-color: whitesmoke;"><td class="code" style="border: none; font-weight: normal; padding: 0px; text-align: left; vertical-align: middle;"><pre style="background: rgb(247, 247, 247); border: none; color: #4d4d4c; font-family: consolas, Menlo, "PingFang SC", "Microsoft YaHei", monospace; font-size: 1em; line-height: 1.6; margin: 0px; overflow: auto; padding: 10px 0px 10px 10px; width: 837px;"><span class="line"> <span class="function" style="color: #4271ae;"><span class="keyword" style="color: #8959a8;">def</span> <span class="title" style="color: #3e999f;">minimumMoves</span><span class="params" style="color: #f5871f;">(self, arr)</span>:</span></span>
<span class="line"> <span class="string" style="color: #718c00;">"""</span></span>
<span class="line"><span class="string" style="color: #718c00;"> :type arr: List[int]</span></span>
<span class="line"><span class="string" style="color: #718c00;"> :rtype: int</span></span>
<span class="line"><span class="string" style="color: #718c00;"> """</span></span>
<span class="line"> n = len(arr)</span>
<span class="line"> dp = [[<span class="number" style="color: #f5871f;">100</span>] * n <span class="keyword" style="color: #8959a8;">for</span> _ <span class="keyword" style="color: #8959a8;">in</span> range(n)]</span>
<span class="line"> </span>
<span class="line"> <span class="keyword" style="color: #8959a8;">for</span> i <span class="keyword" style="color: #8959a8;">in</span> range(n<span class="number" style="color: #f5871f;">-1</span>, <span class="number" style="color: #f5871f;">-1</span>, <span class="number" style="color: #f5871f;">-1</span>):</span>
<span class="line"> dp[i][i] = <span class="number" style="color: #f5871f;">1</span></span>
<span class="line"> <span class="keyword" style="color: #8959a8;">for</span> j <span class="keyword" style="color: #8959a8;">in</span> range(i+<span class="number" style="color: #f5871f;">1</span>, n):</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">if</span> j == n:</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">break</span></span>
<span class="line"> dp[i][j] = <span class="number" style="color: #f5871f;">1</span> + (dp[i+<span class="number" style="color: #f5871f;">1</span>][j] <span class="keyword" style="color: #8959a8;">if</span> i+<span class="number" style="color: #f5871f;">1</span><n <span class="keyword" style="color: #8959a8;">else</span> <span class="number" style="color: #f5871f;">0</span>)</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">for</span> k <span class="keyword" style="color: #8959a8;">in</span> range(i+<span class="number" style="color: #f5871f;">1</span>, j+<span class="number" style="color: #f5871f;">1</span>):</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">if</span> arr[i] == arr[k]:</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">if</span> k == i + <span class="number" style="color: #f5871f;">1</span>:</span>
<span class="line"> dp[i][j] = min(dp[i][j], <span class="number" style="color: #f5871f;">1</span> + (dp[k+<span class="number" style="color: #f5871f;">1</span>][j] <span class="keyword" style="color: #8959a8;">if</span> k+<span class="number" style="color: #f5871f;">1</span><=j <span class="keyword" style="color: #8959a8;">else</span> <span class="number" style="color: #f5871f;">0</span>))</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">else</span>:</span>
<span class="line"> dp[i][j] = min(dp[i][j], dp[i+<span class="number" style="color: #f5871f;">1</span>][k<span class="number" style="color: #f5871f;">-1</span>] + (dp[k+<span class="number" style="color: #f5871f;">1</span>][j] <span class="keyword" style="color: #8959a8;">if</span> k+<span class="number" style="color: #f5871f;">1</span><=j <span class="keyword" style="color: #8959a8;">else</span> <span class="number" style="color: #f5871f;">0</span>))</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">return</span> dp[<span class="number" style="color: #f5871f;">0</span>][n<span class="number" style="color: #f5871f;">-1</span>]</span></pre>
</td></tr>
</tbody></table>
</div>
</figure></div>
<a href="https://blog.csdn.net/yaoct/article/details/102881840">https://blog.csdn.net/yaoct/article/details/102881840</a><br />
<a href="https://www.acwing.com/solution/LeetCode/content/5795/">https://www.acwing.com/solution/LeetCode/content/5795/</a><br />
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(动态规划,区间模型) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>3</mn></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>3</mn></msup><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">设 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo></math></span></span> 表示删除区间 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">[</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">]</mo></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">[</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">]</mo></math></span></span> 所需要的最少操作次数。</li>
<li style="line-height: 1.5em;">初始时,<span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>i</mi><mo stretchy="false">)</mo><mo>=</mo><mn>1</mn></math>" id="MathJax-Element-4-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>i</mi><mo stretchy="false">)</mo><mo>=</mo><mn>1</mn></math></span></span>,<span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>i</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">)</mo><mo>=</mo><mn>0</mn></math>" id="MathJax-Element-5-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo>=</mo><mn>0</mn></math></span></span>。其余为正无穷。</li>
<li style="line-height: 1.5em;">转移时,可以将 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo></math>" id="MathJax-Element-6-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo></math></span></span> 单独考虑删除,则转移 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>=</mo><mi>m</mi><mi>i</mi><mi>n</mi><mo stretchy="false">(</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>,</mo><mn>1</mn><mo>+</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo></math>" id="MathJax-Element-7-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>=</mo><mi>m</mi><mi>i</mi><mi>n</mi><mo stretchy="false">(</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>,</mo><mn>1</mn><mo>+</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo></math></span></span>。如果 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo><mo>==</mo><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo stretchy="false">]</mo></math>" id="MathJax-Element-8-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo><mo>==</mo><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo stretchy="false">]</mo></math></span></span>,则这两个可以一起删除,转移 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>=</mo><mi>m</mi><mi>i</mi><mi>n</mi><mo stretchy="false">(</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>,</mo><mn>1</mn><mo>+</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>2</mn><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo></math>" id="MathJax-Element-9-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>=</mo><mi>m</mi><mi>i</mi><mi>n</mi><mo stretchy="false">(</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>,</mo><mn>1</mn><mo>+</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>2</mn><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo></math></span></span>。如果 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo><mo>==</mo><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>k</mi><mo stretchy="false">]</mo><mo>,</mo><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>2</mn><mo>&lt;=</mo><mi>k</mi><mo>&lt;=</mo><mi>j</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-10-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo><mo>==</mo><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>k</mi><mo stretchy="false">]</mo><mo>,</mo><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>2</mn><mo><=</mo><mi>k</mi><mo><=</mo><mi>j</mi><mo stretchy="false">)</mo></math></span></span>,则 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo></math>" id="MathJax-Element-11-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo></math></span></span> 和 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>k</mi><mo stretchy="false">]</mo></math>" id="MathJax-Element-12-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>k</mi><mo stretchy="false">]</mo></math></span></span> 可以随着区间 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">[</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo>,</mo><mi>k</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">]</mo></math>" id="MathJax-Element-13-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">[</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo>,</mo><mi>k</mi><mo>−</mo><mn>1</mn><mo stretchy="false">]</mo></math></span></span> 一起删除,转移 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>=</mo><mi>m</mi><mi>i</mi><mi>n</mi><mo stretchy="false">(</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>,</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo>,</mo><mi>k</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">)</mo><mo>+</mo><mi>f</mi><mo stretchy="false">(</mo><mi>k</mi><mo>+</mo><mn>1</mn><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo></math>" id="MathJax-Element-14-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>=</mo><mi>m</mi><mi>i</mi><mi>n</mi><mo stretchy="false">(</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo>,</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>+</mo><mn>1</mn><mo>,</mo><mi>k</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo>+</mo><mi>f</mi><mo stretchy="false">(</mo><mi>k</mi><mo>+</mo><mn>1</mn><mo>,</mo><mi>j</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo></math></span></span>。</li>
<li style="line-height: 1.5em;">最终答案为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mn>0</mn><mo>,</mo><mi>n</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">)</mo></math>" id="MathJax-Element-15-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mn>0</mn><mo>,</mo><mi>n</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></math></span></span>。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">状态数为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-16-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math></span></span> 个,转移需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-17-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的时间,故时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>3</mn></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-18-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>3</mn></msup><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">需要额外 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-19-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math></span></span> 的空间存储状态。</li>
</ul>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> <span class="hljs-title" style="display: initial !important;">minimumMoves</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>& arr)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">int</span> n = arr.size();
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>> f(n + <span class="hljs-number" style="display: initial !important;">1</span>, <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>(n + <span class="hljs-number" style="display: initial !important;">1</span>));
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i <= n; i++)
f[i][i] = <span class="hljs-number" style="display: initial !important;">1</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">1</span>; i <= n; i++)
f[i][i - <span class="hljs-number" style="display: initial !important;">1</span>] = <span class="hljs-number" style="display: initial !important;">0</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> len = <span class="hljs-number" style="display: initial !important;">2</span>; len <= n; len++)
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i < n - len + <span class="hljs-number" style="display: initial !important;">1</span>; i++) {
<span class="hljs-keyword" style="display: initial !important;">int</span> j = i + len - <span class="hljs-number" style="display: initial !important;">1</span>;
f[i][j] = <span class="hljs-number" style="display: initial !important;">1</span> + f[i + <span class="hljs-number" style="display: initial !important;">1</span>][j];
<span class="hljs-keyword" style="display: initial !important;">if</span> (arr[i] == arr[i + <span class="hljs-number" style="display: initial !important;">1</span>])
f[i][j] = min(f[i][j], <span class="hljs-number" style="display: initial !important;">1</span> + f[i + <span class="hljs-number" style="display: initial !important;">2</span>][j]);
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> k = i + <span class="hljs-number" style="display: initial !important;">2</span>; k <= j; k++)
<span class="hljs-keyword" style="display: initial !important;">if</span> (arr[i] == arr[k])
f[i][j] = min(f[i][j], f[i + <span class="hljs-number" style="display: initial !important;">1</span>][k - <span class="hljs-number" style="display: initial !important;">1</span>] + f[k + <span class="hljs-number" style="display: initial !important;">1</span>][j]);
}
<span class="hljs-keyword" style="display: initial !important;">return</span> f[<span class="hljs-number" style="display: initial !important;">0</span>][n - <span class="hljs-number" style="display: initial !important;">1</span>];
}
</code></pre>
<div>
<code style="display: initial !important;"><br /></code></div>
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-79802972139738955972020-01-02T23:03:00.002-08:002020-01-02T23:03:35.046-08:00LeetCode 1245 - Tree Diameter<a href="https://leetcode.jp/problemdetail.php?id=1245">https://leetcode.jp/problemdetail.php?id=1245</a><br />
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 16px;">
Given an undirected tree, return its diameter: the number of <span style="box-sizing: border-box; font-weight: 600;">edges</span> in a longest path in that tree.</div>
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 16px;">
The tree is given as an array of <code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">edges</code> where <code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">edges[i] = [u, v]</code> is a bidirectional edge between nodes <code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">u</code> and <code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">v</code>. Each node has labels in the set <code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">{0, 1, ..., edges.length}</code>.</div>
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 16px;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 16px;">
<span style="box-sizing: border-box; font-weight: 600;">Example 1:</span></div>
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 16px;">
<img alt="" src="http://kwantong.sakura.ne.jp/caiji/data/images/2c/f6/2c8ed2eb686a163f9588d214bf448bf6.png" style="border: 0px none; box-sizing: content-box; height: 233px; max-width: 100%; vertical-align: middle; width: 226px;" /></div>
<pre style="background-color: #f6f8fa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 16px; overflow-wrap: normal; overflow: auto; padding: 16px; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 600;">Input:</span> edges = [[0,1],[0,2]]
<span style="box-sizing: border-box; font-weight: 600;">Output:</span> 2
<span style="box-sizing: border-box; font-weight: 600;">Explanation: </span>
A longest path of the tree is the path 1 - 0 - 2.
</pre>
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 16px;">
<span style="box-sizing: border-box; font-weight: 600;">Example 2:</span></div>
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 16px;">
<img alt="" src="http://kwantong.sakura.ne.jp/caiji/data/images/c5/cd/c5495d870ac3ceae1a71c65ea22be0cd.png" style="border: 0px none; box-sizing: content-box; height: 316px; max-width: 100%; vertical-align: middle; width: 350px;" /></div>
<pre style="background-color: #f6f8fa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; line-height: 1.45; margin-bottom: 16px; overflow-wrap: normal; overflow: auto; padding: 16px; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 600;">Input:</span> edges = [[0,1],[1,2],[2,3],[1,4],[4,5]]
<span style="box-sizing: border-box; font-weight: 600;">Output:</span> 4
<span style="box-sizing: border-box; font-weight: 600;">Explanation: </span>
A longest path of the tree is the path 3 - 2 - 1 - 4 - 5.
</pre>
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 16px;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 16px;">
<span style="box-sizing: border-box; font-weight: 600;">Constraints:</span></div>
<br />
<div style="background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px;">
</div>
<br />
<ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 16px; margin-top: 0px; orphans: 2; padding-left: 2em; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<li style="box-sizing: border-box;"><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">0 <= edges.length < 10^4</code></li>
<li style="box-sizing: border-box; margin-top: 0.25em;"><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">edges[i][0] != edges[i][1]</code></li>
<li style="box-sizing: border-box; margin-top: 0.25em;"><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">0 <= edges[i][j] <= edges.length</code></li>
<li style="box-sizing: border-box; margin-top: 0.25em;">The given edges form an undirected tree.</li>
</ul>
<a href="https://www.acwing.com/solution/LeetCode/content/5794/">https://www.acwing.com/solution/LeetCode/content/5794/</a><br />
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(深度优先遍历) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">根据边集数组建邻接表表示树。</li>
<li style="line-height: 1.5em;">我们采用深度优先遍历的方式从 <code>0</code> 号点开始遍历。递归函数返回当前结点的最长路径。</li>
<li style="line-height: 1.5em;">在遍历当前点的子结点的过程中,我们找到他们递归函数返回值加 1 的<strong>最大值</strong>和<strong>次大值</strong>。</li>
<li style="line-height: 1.5em;">更新答案最大值加次大值,然后返回最大值。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">每个结点仅遍历一次,故时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">需要额外 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的空间存储邻接表。</li>
</ul>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-keyword" style="display: initial !important;">int</span> ans;
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>> graph;
<span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> <span class="hljs-title" style="display: initial !important;">dfs</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-keyword" style="display: initial !important;">int</span> x, <span class="hljs-keyword" style="display: initial !important;">int</span> fa)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">int</span> max1 = <span class="hljs-number" style="display: initial !important;">0</span>, max2 = <span class="hljs-number" style="display: initial !important;">0</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> v : graph[x])
<span class="hljs-keyword" style="display: initial !important;">if</span> (v != fa) {
<span class="hljs-keyword" style="display: initial !important;">int</span> t = dfs(v, x) + <span class="hljs-number" style="display: initial !important;">1</span>;
<span class="hljs-keyword" style="display: initial !important;">if</span> (max1 < t) {
max2 = max1;
max1 = t;
} <span class="hljs-keyword" style="display: initial !important;">else</span> <span class="hljs-keyword" style="display: initial !important;">if</span> (max2 < t) {
max2 = t;
}
}
ans = max(ans, (max1 + max2));
<span class="hljs-keyword" style="display: initial !important;">return</span> max1;
}
<span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> <span class="hljs-title" style="display: initial !important;">treeDiameter</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>>& edges)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">int</span> n = edges.size() + <span class="hljs-number" style="display: initial !important;">1</span>;
graph.resize(n);
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">auto</span> &e : edges) {
graph[e[<span class="hljs-number" style="display: initial !important;">0</span>]].push_back(e[<span class="hljs-number" style="display: initial !important;">1</span>]);
graph[e[<span class="hljs-number" style="display: initial !important;">1</span>]].push_back(e[<span class="hljs-number" style="display: initial !important;">0</span>]);
}
ans = <span class="hljs-number" style="display: initial !important;">0</span>;
dfs(<span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">-1</span>);
<span class="hljs-keyword" style="display: initial !important;">return</span> ans;
}
</code></pre>
<div>
<code style="display: initial !important;"><a href="https://dongweidotblog.wordpress.com/2019/11/17/1245-tree-diameter/">https://dongweidotblog.wordpress.com/2019/11/17/1245-tree-diameter/</a></code></div>
<div>
<div>
解题思路:本题是一道图上做有记忆的dfs搜索的题目,是无向图,给定的输入是图的各个边,题目中给出一个关键信息(Each node has labels in the set {0, 1, ..., edges.length})可知图中没有环,如果是有环的图这个题是无解的。最好的证明方法就是反正法,如果有环的话标号最大的节点必然小于边的数量,例如[0, 1], [1, 2], [0, 2], 三个节点且三条边,但编号最大的节点小于边的数量。</div>
<div>
<br /></div>
<div>
作为一个无向不联通图,节点主要分为两种类型,一种可以称为叶节点(这个并非是官方的名字,只是我自己叫着方便),即只有一个边与它连接,除来叶节点之外的节点则至少有两个边相连。</div>
<div>
<br /></div>
<div>
题目要求找出最长的一个path依次来定义这个tree的直径,很显然直径的选择是从一个叶节点到另外一个叶节点,在构建完图之后找到所有的叶节点,开始进行dfs的遍历,找出其中最长的一条路径来定义树的直径。</div>
<div>
<br /></div>
<div>
这个题目的模型与 568. Maximum Vacation Days 最大的不同是在计算过的值的记忆部分。</div>
<div>
<br /></div>
<div>
在568中每个节点的进与出的部分很清晰的分离,因此只要计算过一次,下一次递归不团从哪个节点进,都返回同样的答案。</div>
<div>
<br /></div>
<div>
本题则需要根据不同的输入返回不同的结果,所以memo的数据结构也不一样,对于每一个节点,需要用一个map来对应该节点到下一个节点的答案。这个地方需要使用map,而不是vector,因为需要使用到下一个节点的序号来索引。如果选择vector,无法将序号转换成下表。</div>
<div>
<br /></div>
<div>
时间复杂度:对于每一个节点而言,图需要计算各个出的值,复杂度为 O(e)</div>
<div>
<br /></div>
<div>
空间复杂度:存储每个节点到每一个方向的值 O(e)</div>
<div>
<br /></div>
<div>
class Solution {</div>
<div>
public:</div>
<div>
int recursive(vector<vector<int>>& graph, vector<map<int, int>>& memo, vector<bool>& visited, int cur){</div>
<div>
int len = 0;</div>
<div>
for(int i = 0; i < graph[cur].size(); ++i){</div>
<div>
int next = graph[cur][i];</div>
<div>
if(!visited[next]){</div>
<div>
visited[next] = true;</div>
<div>
if(memo[cur][next] == 0){</div>
<div>
memo[cur][next] = recursive(graph, memo, visited, next) + 1;</div>
<div>
}</div>
<div>
len = max(len, memo[cur][next]); </div>
<div>
}</div>
<div>
}</div>
<div>
return len;</div>
<div>
}</div>
<div>
</div>
<div>
int treeDiameter(vector<vector<int>>& edges) {</div>
<div>
const int nodeCnt = edges.size() + 1;</div>
<div>
vector<vector<int>> graph(nodeCnt, vector<int>());</div>
<div>
vector<map<int, int>> memo(nodeCnt, map<int, int>()); // map<int, int>: first: the index of next node, second: the value if jump to the node</div>
<div>
for(auto edge : edges){</div>
<div>
graph[edge[0]].push_back(edge[1]);</div>
<div>
graph[edge[1]].push_back(edge[0]);</div>
<div>
memo[edge[0]][edge[1]] = 0;</div>
<div>
memo[edge[1]][edge[0]] = 0;</div>
<div>
}</div>
<div>
int ans = 0;</div>
<div>
for(int i = 0; i < graph.size(); ++i){</div>
<div>
if(graph[i].size() == 1){</div>
<div>
vector<bool> visited(nodeCnt, false);</div>
<div>
visited[i] = true;</div>
<div>
ans = max(ans, recursive(graph, memo, visited, i));</div>
<div>
}</div>
<div>
}</div>
<div>
return ans;</div>
<div>
}</div>
<div>
};</div>
</div>
<div>
<a href="https://codesolu.com/2019/11/11/12-fortnight-1245-tree-diameter/">https://codesolu.com/2019/11/11/12-fortnight-1245-tree-diameter/</a></div>
<div>
<div style="-webkit-font-smoothing: antialiased; background-color: white; box-sizing: inherit; color: #111111; font-family: NonBreakingSpaceOverride, "Hoefler Text", "Baskerville Old Face", Garamond, "Times New Roman", serif; font-size: 22px; margin-bottom: 32px; margin-top: 32px; max-width: calc(50vw - 28px); overflow-wrap: break-word;">
Method 1: depth – first:</div>
<div style="background-color: white; box-sizing: inherit; color: #111111; font-family: NonBreakingSpaceOverride, "Hoefler Text", "Baskerville Old Face", Garamond, "Times New Roman", serif; font-size: 22px; margin: 32px 0px; max-width: calc(50vw - 28px);">
<pre style="box-sizing: inherit; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.88889em; line-height: 1.8; overflow: auto;"><span style="box-sizing: inherit;">class</span><span style="box-sizing: inherit;"> Solution:
</span><span style="box-sizing: inherit;">def</span> treeDiameter(self, edges: List[List[int]]) -><span style="box-sizing: inherit;"> int:
adjacency </span>=<span style="box-sizing: inherit;"> collections.defaultdict(set)
</span><span style="box-sizing: inherit;">for</span> i,j <span style="box-sizing: inherit;">in</span><span style="box-sizing: inherit;"> edges:
adjacency[i].add(j)
adjacency[j].add(i)
memo </span>=<span style="box-sizing: inherit;"> {}
</span><span style="box-sizing: inherit;">def</span><span style="box-sizing: inherit;"> dfs(i,j):
</span><span style="box-sizing: inherit;">if</span> (i,j) <span style="box-sizing: inherit;">in</span><span style="box-sizing: inherit;"> memo:
</span><span style="box-sizing: inherit;">return</span><span style="box-sizing: inherit;"> memo[i,j]
longest </span>=<span style="box-sizing: inherit;"> 0
</span><span style="box-sizing: inherit;">for</span> k <span style="box-sizing: inherit;">in</span><span style="box-sizing: inherit;"> adjacency[j]:
</span><span style="box-sizing: inherit;">if</span> k == i:<span style="box-sizing: inherit;">continue</span><span style="box-sizing: inherit;">
longest </span>=<span style="box-sizing: inherit;"> max(longest,dfs(j,k))
memo[(i,j)] </span>= longest + 1
<span style="box-sizing: inherit;">return</span><span style="box-sizing: inherit;"> memo[(i,j)]
</span><span style="box-sizing: inherit;">for</span> i,j <span style="box-sizing: inherit;">in</span><span style="box-sizing: inherit;"> edges:
dfs(i,j)
dfs(j,i)
</span><span style="box-sizing: inherit;">return</span> max(memo[(i,j)]+memo[(j,i)]-1 <span style="box-sizing: inherit;">for</span> i,j <span style="box-sizing: inherit;">in</span> edges)</pre>
</div>
<div style="-webkit-font-smoothing: antialiased; background-color: white; box-sizing: inherit; color: #111111; font-family: NonBreakingSpaceOverride, "Hoefler Text", "Baskerville Old Face", Garamond, "Times New Roman", serif; font-size: 22px; margin-bottom: 32px; margin-top: 32px; max-width: calc(50vw - 28px); overflow-wrap: break-word;">
Method 2: Breadth – first</div>
<div style="background-color: white; box-sizing: inherit; color: #111111; font-family: NonBreakingSpaceOverride, "Hoefler Text", "Baskerville Old Face", Garamond, "Times New Roman", serif; font-size: 22px; margin: 32px 0px; max-width: calc(50vw - 28px);">
<pre style="box-sizing: inherit; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.88889em; line-height: 1.8; overflow: auto;"><span style="box-sizing: inherit;">class</span><span style="box-sizing: inherit;"> Solution:
</span><span style="box-sizing: inherit;">def</span> treeDiameter(self, edges: List[List[int]]) -><span style="box-sizing: inherit;"> int:
adjacency </span>=<span style="box-sizing: inherit;"> collections.defaultdict(set)
</span><span style="box-sizing: inherit;">for</span> i,j <span style="box-sizing: inherit;">in</span><span style="box-sizing: inherit;"> edges:
adjacency[i].add(j)
adjacency[j].add(i)
</span><span style="box-sizing: inherit;">def</span><span style="box-sizing: inherit;"> bfs(node):
queue </span>=<span style="box-sizing: inherit;"> collections.deque()
queue.append((node,0))
memo </span>=<span style="box-sizing: inherit;"> {node}
</span><span style="box-sizing: inherit;">while</span><span style="box-sizing: inherit;"> queue:
n,long </span>=<span style="box-sizing: inherit;"> queue.popleft()
</span><span style="box-sizing: inherit;">for</span> nei <span style="box-sizing: inherit;">in</span><span style="box-sizing: inherit;"> adjacency[n]:
</span><span style="box-sizing: inherit;">if</span> nei <span style="box-sizing: inherit;">not</span> <span style="box-sizing: inherit;">in</span><span style="box-sizing: inherit;"> memo:
memo.add(nei)
queue.append((nei,long</span>+1<span style="box-sizing: inherit;">))
</span><span style="box-sizing: inherit;">return</span><span style="box-sizing: inherit;"> n,long
far,long </span>=<span style="box-sizing: inherit;"> bfs(0)
far2,longest </span>=<span style="box-sizing: inherit;"> bfs(far)
</span><span style="box-sizing: inherit;">return</span> longest</pre>
<div>
<br /></div>
</div>
</div>
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-2474701941147132112020-01-02T23:00:00.000-08:002020-01-02T23:00:01.385-08:00LeetCode 1243 - Array Transformation<a href="https://www.cnblogs.com/seyjs/p/11785282.html">https://www.cnblogs.com/seyjs/p/11785282.html</a><br />
<blockquote style="background: none rgb(255, 255, 255); border: 2px solid rgb(239, 239, 239); color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; margin: 10px 0px; padding: 5px 10px;">
<div style="margin: 10px auto; padding: 0px;">
Given an initial array <code style="margin: 0px; padding: 0px;">arr</code>, every day you produce a new array using the array of the previous day.</div>
<div style="margin: 10px auto; padding: 0px;">
On the <code style="margin: 0px; padding: 0px;">i</code>-th day, you do the following operations on the array of day <code style="margin: 0px; padding: 0px;">i-1</code> to produce the array of day <code style="margin: 0px; padding: 0px;">i</code>:</div>
<ol style="margin: 0px; padding: 0px 0px 0px 40px;">
<li style="list-style: decimal; margin: 0px; padding: 0px;">If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.</li>
<li style="list-style: decimal; margin: 0px; padding: 0px;">If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.</li>
<li style="list-style: decimal; margin: 0px; padding: 0px;">The first and last elements never change.</li>
</ol>
<div style="margin: 10px auto; padding: 0px;">
After some days, the array does not change. Return that final array.</div>
<div style="margin: 10px auto; padding: 0px;">
Example 1:</div>
<pre style="overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;">Input: arr = [6,2,3,4]
Output: [6,3,3,4]
Explanation:
On the first day, the array is changed from [6,2,3,4] to [6,3,3,4].
No more operations can be done to this array.
</pre>
<div style="margin: 10px auto; padding: 0px;">
Example 2:</div>
<pre style="overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;">Input: arr = [1,6,3,4,3,5]
Output: [1,4,4,4,4,5]
Explanation:
On the first day, the array is changed from [1,6,3,4,3,5] to [1,5,4,3,4,5].
On the second day, the array is changed from [1,5,4,3,4,5] to [1,4,4,4,4,5].
No more operations can be done to this array.</pre>
<div style="margin: 10px auto; padding: 0px;">
Constraints:</div>
<ul style="margin: 0px 0px 0px 25px; padding: 0px;">
<li style="list-style: disc; margin: 0px; padding: 0px;"><code style="margin: 0px; padding: 0px;">1 <= arr.length <= 100</code></li>
<li style="list-style: disc; margin: 0px; padding: 0px;"><code style="margin: 0px; padding: 0px;">1 <= arr[i] <= 100</code></li>
</ul>
</blockquote>
<a href="https://dongweidotblog.wordpress.com/2019/11/04/1243-array-transformation/">https://dongweidotblog.wordpress.com/2019/11/04/1243-array-transformation/</a><br />
解题思路:概括以下题目的意思,根据前一个的数组状态来更新当前状态,所以需要有一个数组来保存前一个状态中每一个元素的变化值,每次都需要提前更新,如果没有变化则直接返回答案。<br />
<br />
时间复杂度 O(n) / 空间复杂度 O(n)<br />
vector<int> transformArray(vector<int>& arr) {<br />
vector<int> adder(arr.size(), 0);<br />
bool flag = true;<br />
while(flag){<br />
flag = false;<br />
for(int i = 1; i < arr.size() - 1; ++i){<br />
arr[i] += adder[i];<br />
adder[i] = 0;<br />
}<br />
for(int i = 1; i < arr.size() - 1; ++i){<br />
if(arr[i - 1] < arr[i] && arr[i] > arr[i + 1]){<br />
flag = true;<br />
adder[i] = -1;<br />
}<br />
if(arr[i - 1] > arr[i] && arr[i] < arr[i + 1]){<br />
flag = true;<br />
adder[i] = 1;<br />
}<br />
}<br />
}<br />
return arr;<br />
}<br />
<div>
<a href="https://www.acwing.com/solution/LeetCode/content/5792/">https://www.acwing.com/solution/LeetCode/content/5792/</a></div>
<div>
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(暴力模拟) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mn>100</mn><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mn>100</mn><msup><mi>n^</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">按照题目描述暴力模拟,直到数组不再变化。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">每个数字最多改变 <code>100</code> 次,共有 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0">n</span> 个数字,每轮迭代需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的时间。</li>
<li style="line-height: 1.5em;">故时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mn>100</mn><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-4-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mn>100</mn><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-5-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的空间作为辅助数组。</li>
</ul>
</div>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>> transformArray(<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>& arr) {
<span class="hljs-keyword" style="display: initial !important;">int</span> n = arr.size();
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>> pre(arr.begin(), arr.end());
<span class="hljs-keyword" style="display: initial !important;">bool</span> flag = <span class="hljs-literal" style="display: initial !important;">true</span>;
<span class="hljs-keyword" style="display: initial !important;">while</span> (flag) {
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">1</span>; i < n - <span class="hljs-number" style="display: initial !important;">1</span>; i++) {
<span class="hljs-keyword" style="display: initial !important;">if</span> (pre[i - <span class="hljs-number" style="display: initial !important;">1</span>] < pre[i] && pre[i] > pre[i + <span class="hljs-number" style="display: initial !important;">1</span>])
arr[i]--;
<span class="hljs-keyword" style="display: initial !important;">if</span> (pre[i - <span class="hljs-number" style="display: initial !important;">1</span>] > pre[i] && pre[i] < pre[i + <span class="hljs-number" style="display: initial !important;">1</span>])
arr[i]++;
}
flag = <span class="hljs-literal" style="display: initial !important;">false</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i < n; i++)
<span class="hljs-keyword" style="display: initial !important;">if</span> (pre[i] != arr[i]) {
pre[i] = arr[i];
flag = <span class="hljs-literal" style="display: initial !important;">true</span>;
}
}
<span class="hljs-keyword" style="display: initial !important;">return</span> arr;
}
</code></pre>
<div>
<code style="display: initial !important;"><br /></code></div>
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-39538183989583012652020-01-02T22:57:00.000-08:002020-01-02T22:57:17.423-08:00LeetCode 1250 - Check If It Is a Good Array<a href="https://leetcode.com/problems/check-if-it-is-a-good-array/">https://leetcode.com/problems/check-if-it-is-a-good-array/</a><br />
<div class="content__u3I1 question-content__JfgR" style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin: 1em 0px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Given an array <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nums</code> of positive integers. Your task is to select some subset of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nums</code>, multiply each element by an integer and add all these numbers. The array is said to be <span style="box-sizing: border-box; font-weight: bolder;">good </span>if you can obtain a sum of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1</code> from the array by any possible subset and multiplicand.</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Return <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">True</code> if the array is <span style="box-sizing: border-box; font-weight: bolder;">good </span>otherwise return <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">False</code>.</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 1:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> nums = [12,5,7,23]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> true
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span> Pick numbers 5 and 7.
5*3 + 7*(-2) = 1
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 2:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> nums = [29,6,10]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> true
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span> Pick numbers 29, 6 and 10.
29*1 + 6*(-3) + 10*(-1) = 1
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 3:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> nums = [3,6]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> false
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Constraints:</span></div>
<ul style="box-sizing: border-box; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= nums.length <= 10^5</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= nums[i] <= 10^9</code></li>
</ul>
</div>
</div>
<a href="https://leetcode.com/problems/check-if-it-is-a-good-array/discuss/419368/JavaC%2B%2BPython-Chinese-Remainder-Theorem">https://leetcode.com/problems/check-if-it-is-a-good-array/discuss/419368/JavaC%2B%2BPython-Chinese-Remainder-Theorem</a><br />
<h2 style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: unset; font-weight: 500; margin-bottom: 0.5em; margin-top: 0px;">
<span style="box-sizing: border-box; font-weight: bolder;">Intuition</span></h2>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
This problem is realated a Chinese theorem:<br style="box-sizing: border-box;" />Chinese remainder theorem, created in 5th centry.<br style="box-sizing: border-box;" />Also well known as Hanson Counting (韩信点兵).<br style="box-sizing: border-box;" /></div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<h2 style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: unset; font-weight: 500; margin-bottom: 0.5em; margin-top: 0px;">
<span style="box-sizing: border-box; font-weight: bolder;">Explanation</span></h2>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
If <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">a % x = 0</code> and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">b % x = 0</code>,<br style="box-sizing: border-box;" />appareantly we have <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">(pa + qb) % x == 0</code><br style="box-sizing: border-box;" />If <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">x > 1</code>, making it impossible <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">pa + qb = 1</code>.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Well, I never heard of Bezout's identity.<br style="box-sizing: border-box;" />Even though the intuition only proves the necessary condition,<br style="box-sizing: border-box;" />it's totally enough.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
The process of gcd,<br style="box-sizing: border-box;" />is exactly the process to get the factor.<br style="box-sizing: border-box;" />The problem just doesn't ask this part.<br style="box-sizing: border-box;" /></div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<h2 style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: unset; font-weight: 500; margin-bottom: 0.5em; margin-top: 0px;">
<span style="box-sizing: border-box; font-weight: bolder;">Complexity</span></h2>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Of course you can return true as soon as <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">gcd = 1</code><br style="box-sizing: border-box;" />I take gcd calculate as <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(1)</code>.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Time <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(N)</code>,<br style="box-sizing: border-box;" />Space <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(N)</code></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">boolean</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">isGoodArray</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] A)</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> x = A[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>], y;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> a: A) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">while</span> (a > <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) {
y = x % a;
x = a;
a = y;
}
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> x == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>;
}</code></pre>
<a href="https://leetcode.com/problems/check-if-it-is-a-good-array/discuss/419324/Bezout's-Identity">https://leetcode.com/problems/check-if-it-is-a-good-array/discuss/419324/Bezout's-Identity</a><br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Read (<a href="https://brilliant.org/wiki/bezouts-identity/" style="background-color: transparent; border-bottom: 1px solid transparent; box-sizing: border-box; color: #607d8b; cursor: pointer; outline: none; padding-bottom: 1px; pointer-events: auto; text-decoration-line: none; touch-action: manipulation; transition: border-bottom-color 0.3s ease 0s;">https://brilliant.org/wiki/bezouts-identity/</a>, <a href="https://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity" style="background-color: transparent; border-bottom: 1px solid transparent; box-sizing: border-box; color: #607d8b; cursor: pointer; outline: none; padding-bottom: 1px; pointer-events: auto; text-decoration-line: none; touch-action: manipulation; transition: border-bottom-color 0.3s ease 0s;">https://en.wikipedia.org/wiki/Bézout's_identity</a>)</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
The basic idea is that for integers a and b, if gcd(a,b) = d, then there exist integers x and y, s.t a * x + b * y = d;</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
This can be generalized for (n >= 2) . e.g. if gcd(a,b,c) = d, then there exist integers x, y, and z, s.t, a* x + b*y + c * z = d.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Now this problem is just asking if gcd(x1, ......, xn) = 1</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">class</span> <span class="hljs-symbol" style="box-sizing: border-box; color: #1c00cf;">Solution</span> {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span>:
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">bool</span> isGoodArray(vector<<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>>& nums) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (nums.size() == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> nums[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>;
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> a = nums[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>; i < nums.size(); i++) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (__gcd(a, nums[i]) == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">true</span>;
a = __gcd(a, nums[i]);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">false</span>;
}</code></pre>
<a href="https://www.cnblogs.com/seyjs/p/11881026.html">https://www.cnblogs.com/seyjs/p/11881026.html</a><br />
<div style="background-color: white; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; margin: 10px auto; padding: 0px;">
<strong style="margin: 0px; padding: 0px;">解题思路:</strong>看到这个题目,大家或许能猜到这题对应着数学定律。至于是什么定律,我是不知道的。后来网上搜索才发现对应的定律是裴蜀定理,最后的解法就是求出所有元素的最大公约数,判断是否为1即可。</div>
<blockquote style="background: none rgb(255, 255, 255); border: 2px solid rgb(239, 239, 239); color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; margin: 10px 0px; padding: 5px 10px;">
<div class="para" style="margin: 0px; padding: 0px;">
裴蜀定理(或<a href="https://baike.baidu.com/item/%E8%B4%9D%E7%A5%96%E5%AE%9A%E7%90%86" style="border-bottom: 1px dotted rgb(51, 51, 51); color: black; margin: 0px; padding: 0px; text-decoration-line: none;" target="_blank">贝祖定理</a>,Bézout's identity)得名于法国数学家艾蒂安·裴蜀,说明了对任何<a href="https://baike.baidu.com/item/%E6%95%B4%E6%95%B0" style="border-bottom: 1px dotted rgb(51, 51, 51); color: black; margin: 0px; padding: 0px; text-decoration-line: none;" target="_blank">整数</a>a、b和它们的最大公约<div class="lemma-picture text-pic layout-right" style="margin: 0px; padding: 0px;">
<a class="image-link" href="https://baike.baidu.com/pic/%E8%A3%B4%E8%9C%80%E5%AE%9A%E7%90%86/5186593/0/cdfe7281d4445bc1bc3e1ecc?fr=lemma&ct=single" style="border-bottom: 1px dotted rgb(51, 51, 51); color: black; margin: 0px; padding: 0px; text-decoration-line: none;" target="_blank" title=""><img alt="" src="https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D250/sign=e65fd10ab07eca8016053ee2a1239712/8d5494eef01f3a2979bba2e79925bc315c607ca3.jpg" style="border: 0px; height: auto; margin: 0px; max-width: 900px; padding: 0px;" /></a></div>
数d,关于<a href="https://baike.baidu.com/item/%E6%9C%AA%E7%9F%A5%E6%95%B0" style="border-bottom: 1px dotted rgb(51, 51, 51); color: black; margin: 0px; padding: 0px; text-decoration-line: none;" target="_blank">未知数</a>x和y的线性不定方程(称为裴蜀等式):若a,b是整数,且gcd(a,b)=d,那么对于任意的整数x,y,ax+by都一定是d的倍数,特别地,一定存在整数x,y,使ax+by=d成立。</div>
<div class="para" style="margin: 0px; padding: 0px;">
它的一个重要推论是:a,b<a data-lemmaid="577412" href="https://baike.baidu.com/item/%E4%BA%92%E8%B4%A8/577412" style="border-bottom: 1px dotted rgb(51, 51, 51); color: black; margin: 0px; padding: 0px; text-decoration-line: none;" target="_blank">互质</a>的充要条件是存在<a href="https://baike.baidu.com/item/%E6%95%B4%E6%95%B0" style="border-bottom: 1px dotted rgb(51, 51, 51); color: black; margin: 0px; padding: 0px; text-decoration-line: none;" target="_blank">整数</a>x,y使ax+by=1。</div>
<div>
<br /></div>
</blockquote>
<a href="https://www.cnblogs.com/wentiliangkaihua/p/11870419.html">https://www.cnblogs.com/wentiliangkaihua/p/11870419.html</a><br />
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
找最大公约数是不是1.</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
辗转相除法:第二次开始一直用除数除以余数,知道余数为0,当前除数就是最大公约数。</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
<img alt="" src="https://img2018.cnblogs.com/i-beta/1565840/201911/1565840-20191116051101138-301696766.png" style="border: 0px; height: auto; margin: 0px; max-width: 900px; padding: 0px;" /></div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
<br /></div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
看起来最后返回的是除数,但其实应该是被除数</div>
<div class="cnblogs_code" style="background-color: whitesmoke; border: 1px solid rgb(204, 204, 204); font-family: "Courier New" !important; font-size: 12px !important; margin: 5px 0px; overflow: auto; padding: 5px;">
<div class="cnblogs_code_toolbar" style="margin: 5px 0px 0px; padding: 0px;">
<br /></div>
<pre style="font-family: "Courier New" !important; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">public</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">static</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> gcd(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> a, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> b){
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> c = a %<span style="line-height: 1.5; margin: 0px; padding: 0px;"> b;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">while</span>(c > 0<span style="line-height: 1.5; margin: 0px; padding: 0px;">){
c </span>= a %<span style="line-height: 1.5; margin: 0px; padding: 0px;"> b;
a </span>=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> b;
b </span>=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> c;
}
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> a;
}</span></pre>
</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
因为最后一步把b赋给a了,然后b其实==0.</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
最后写了种自己能写的方法</div>
<div class="cnblogs_code" style="background-color: whitesmoke; border: 1px solid rgb(204, 204, 204); font-family: "Courier New" !important; font-size: 12px !important; margin: 5px 0px; overflow: auto; padding: 5px;">
<div class="cnblogs_code_toolbar" style="margin: 5px 0px 0px; padding: 0px;">
<span class="cnblogs_code_copy" style="line-height: 1.5; margin: 0px; padding: 0px 5px 0px 0px;"><a href="https://www.blogger.com/null" style="border: none !important; margin: 0px; padding: 0px; text-decoration-line: underline;" title="复制代码"><span style="line-height: 1.5; margin: 0px; padding: 0px; white-space: pre-wrap;"><span style="color: #075db3;"> </span></span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px; white-space: pre-wrap;">public</span><span style="color: #075db3; white-space: pre-wrap;"> </span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px; white-space: pre-wrap;">boolean</span><span style="color: #075db3; white-space: pre-wrap;"> isGoodArray(</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px; white-space: pre-wrap;">int</span><span style="color: #075db3; line-height: 1.5; margin: 0px; padding: 0px; white-space: pre-wrap;">[] nums) {</span></a></span></div>
<pre style="font-family: "Courier New" !important; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="line-height: 1.5; margin: 0px; padding: 0px;"> </span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> x = nums[0<span style="line-height: 1.5; margin: 0px; padding: 0px;">];
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span>(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> i = 0; i < nums.length; i++<span style="line-height: 1.5; margin: 0px; padding: 0px;">){
x </span>=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> gcd(x, nums[i]);
}
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span> x == 1<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
}
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">public</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> gcd(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> a, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> b){
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> c = a %<span style="line-height: 1.5; margin: 0px; padding: 0px;"> b;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">while</span>(c > 0<span style="line-height: 1.5; margin: 0px; padding: 0px;">){
c </span>= a %<span style="line-height: 1.5; margin: 0px; padding: 0px;"> b;
a </span>=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> b;
b </span>=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> c;
}
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> a;
}
</span></pre>
<div>
<span style="line-height: 1.5; margin: 0px; padding: 0px;"><br /></span></div>
</div>
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-41901086948446717422020-01-02T22:27:00.000-08:002020-01-02T22:27:52.027-08:00LeetCode 1255 - Maximum Score Words Formed by Letters<a href="https://leetcode.com/problems/maximum-score-words-formed-by-letters/">https://leetcode.com/problems/maximum-score-words-formed-by-letters/</a><br />
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Given a list of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">words</code>, list of single <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">letters</code> (might be repeating) and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">score</code> of every character.</div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Return the maximum score of <span style="box-sizing: border-box; font-weight: bolder;">any</span> valid set of words formed by using the given letters (<code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">words[i]</code> cannot be used two or more times).</div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
It is not necessary to use all characters in <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">letters</code> and each letter can only be used once. Score of letters <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">'a'</code>, <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">'b'</code>, <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">'c'</code>, ... ,<code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">'z'</code> is given by <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">score[0]</code>, <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">score[1]</code>, ... , <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">score[25]</code> respectively.</div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 1:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> words = ["dog","cat","dad","good"], letters = ["a","a","c","d","d","d","g","o","o"], score = [1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 23
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span>
Score a=1, c=9, d=5, g=3, o=2
Given letters, we can form the words "dad" (5+1+5) and "good" (3+2+2+5) with a score of 23.
Words "dad" and "dog" only get a score of 21.</pre>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 2:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> words = ["xxxz","ax","bx","cx"], letters = ["z","a","b","c","x","x","x"], score = [4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,10]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 27
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span>
Score a=4, b=4, c=4, x=5, z=10
Given letters, we can form the words "ax" (4+5), "bx" (4+5) and "cx" (4+5) with a score of 27.
Word "xxxz" only get a score of 25.</pre>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 3:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> words = ["leetcode"], letters = ["l","e","t","c","o","d"], score = [0,0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 0
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span>
Letter "e" can only be used once.</pre>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Constraints:</span></div>
<ul style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= words.length <= 14</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= words[i].length <= 15</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= letters.length <= 100</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">letters[i].length == 1</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">score.length == 26</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0 <= score[i] <= 10</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">words[i]</code>, <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">letters[i]</code> contains only lower case English letters</li>
</ul>
X. <a href="https://www.acwing.com/solution/LeetCode/content/6125/">https://www.acwing.com/solution/LeetCode/content/6125/</a><br />
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(二进制枚举) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo>&#x22C5;</mo><msup><mn>2</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-5-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo>⋅</mo><msup><mn>2^</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">枚举二进制的集合,从 1 到 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>2</mn><mi>n</mi></msup><mo>&#x2212;</mo><mn>1</mn></math>" id="MathJax-Element-6-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>2</mn><mi>n</mi></msup><mo>−</mo><mn>1</mn></math></span></span>。</li>
<li style="line-height: 1.5em;">每一位 0 或 1 代表选或不选。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-7-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math></span></span> 种情况全部枚举,每次枚举后需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-8-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的时间找到被选出的单词,故时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo>&#x22C5;</mo><msup><mn>2</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-9-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo>⋅</mo><msup><mn>2^</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">仅需要常数的额外空间。</li>
</ul>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> <span class="hljs-title" style="display: initial !important;">maxScoreWords</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">string</span>>& words, <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">char</span>>& letters, <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>& score)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">int</span> n = words.size();
<span class="hljs-keyword" style="display: initial !important;">int</span> ans = <span class="hljs-number" style="display: initial !important;">0</span>;
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>> valid(<span class="hljs-number" style="display: initial !important;">26</span>, <span class="hljs-number" style="display: initial !important;">0</span>);
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">char</span> c : letters)
valid[c - <span class="hljs-string" style="display: initial !important;">'a'</span>]++;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> S = <span class="hljs-number" style="display: initial !important;">1</span>; S < (<span class="hljs-number" style="display: initial !important;">1</span> << n); S++) {
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>> used(<span class="hljs-number" style="display: initial !important;">26</span>, <span class="hljs-number" style="display: initial !important;">0</span>);
<span class="hljs-keyword" style="display: initial !important;">int</span> tot = <span class="hljs-number" style="display: initial !important;">0</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i < n; i++)
<span class="hljs-keyword" style="display: initial !important;">if</span> ((S >> i) & <span class="hljs-number" style="display: initial !important;">1</span>) {
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">char</span> c : words[i]) {
used[c - <span class="hljs-string" style="display: initial !important;">'a'</span>]++;
tot += score[c - <span class="hljs-string" style="display: initial !important;">'a'</span>];
}
}
<span class="hljs-keyword" style="display: initial !important;">bool</span> flag = <span class="hljs-literal" style="display: initial !important;">true</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i < <span class="hljs-number" style="display: initial !important;">26</span>; i++)
<span class="hljs-keyword" style="display: initial !important;">if</span> (used[i] > valid[i]) {
flag = <span class="hljs-literal" style="display: initial !important;">false</span>;
<span class="hljs-keyword" style="display: initial !important;">break</span>;
}
<span class="hljs-keyword" style="display: initial !important;">if</span> (flag)
ans = max(ans, tot);
}
<span class="hljs-keyword" style="display: initial !important;">return</span> ans;
}
</code></pre>
<div>
<code style="display: initial !important;"><br /></code></div>
<div>
<br /></div>
X.<br />
<a href="https://www.icode9.com/content-4-554788.html">https://www.icode9.com/content-4-554788.html</a><br />
<div style="background-color: white; color: #424242; font-family: "Microsoft Yahei", "PingFang SC", "HanHei SC", Arial; font-size: 1pc; line-height: 30px; padding: 10px 0px; text-align: justify;">
用回溯法遍历各种可能性,时间复杂度 O(2^n),可以使用一些剪枝的技巧。</div>
<pre class="brush:java;gutter:true;" style="background-color: whitesmoke; border-bottom-color: initial; border-left-color: rgb(204, 204, 204); border-right-color: initial; border-style: solid; border-top-color: initial; border-width: 0px 0px 0px 4px; color: #272a30; font-family: Consolas, monospace; font-size: 13px; line-height: 18.2px; margin-bottom: 20px !important; overflow: auto; padding: 5px; text-align: justify; vertical-align: baseline; white-space: pre-wrap;">private static int result;
private static Map<String, Integer> scoreMap = null;
private static int[] lettersNumber;
public int maxScoreWords(String[] words, char[] letters, int[] score) {
result = 0;
scoreMap = new HashMap<>();
lettersNumber = new int[26];
char[] chars;
int wordScore;
for (String word : words) {
chars = word.toCharArray();
wordScore = 0;
for (char aChar : chars) {
wordScore += score[aChar - 'a'];
}
scoreMap.put(word, wordScore);
}
for (char letter : letters) {
lettersNumber[letter - 'a']++;
}
Arrays.sort(words, (a, b) -> scoreMap.get(b) - scoreMap.get(a));
dfsHelper(0, 0, words);
return result;
}
private void dfsHelper(int score, int index, String[] words) {
if (index >= words.length) {
result = Math.max(result, score);
return;
}
String word = words[index];
if ((score + scoreMap.get(word) * (words.length - index)) <= result) {
return;
}
char[] chars = word.toCharArray();
int len = 0;
for (char aChar : chars) {
if (lettersNumber[aChar - 'a'] > 0) {
lettersNumber[aChar - 'a']--;
len++;
} else {
break;
}
}
if (len == chars.length) {
score += scoreMap.get(word);
dfsHelper(score, index + 1, words);
score -= scoreMap.get(word);
}
for (int i = 0; i < len; i++) {
lettersNumber[chars[i] - 'a']++;
}
dfsHelper(score, index + 1, words);
}</pre>
<br />
<a href="https://leetcode.com/problems/maximum-score-words-formed-by-letters/discuss/425129/java-backtrack-similar-to-78.-Subsets-1ms-beats-100">https://leetcode.com/problems/maximum-score-words-formed-by-letters/discuss/425129/java-backtrack-similar-to-78.-Subsets-1ms-beats-100</a><br />
<br />
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<br />
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Similar to 78. Subsets, use backtrack to solve the problem.</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">maxScoreWords</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(String[] words, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">char</span>[] letters, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] score)</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (words == <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">null</span> || words.length == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> || letters == <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">null</span> || letters.length == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> || score == <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">null</span> || score.length == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] count = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[score.length];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">char</span> ch : letters) {
count[ch - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'a'</span>]++;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> res = backtrack(words, count, score, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> res;
}
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">backtrack</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(String[] words, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] count, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] score, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> index)</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> max = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = index; i < words.length; i++) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> res = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">boolean</span> isValid = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">true</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">char</span> ch : words[i].toCharArray()) {
count[ch - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'a'</span>]--;
res += score[ch - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'a'</span>];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (count[ch - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'a'</span>] < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) isValid = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">false</span>;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (isValid) {
res += backtrack(words, count, score, i + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
max = Math.max(res, max);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">char</span> ch : words[i].toCharArray()) {
count[ch - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'a'</span>]++;
res = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
}
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> max;
}</code></pre>
<a href="https://leetcode.jp/leetcode-1255-maximum-score-words-formed-by-letters-%E8%A7%A3%E9%A2%98%E6%80%9D%E8%B7%AF%E5%88%86%E6%9E%90/">https://leetcode.jp/leetcode-1255-maximum-score-words-formed-by-letters-%E8%A7%A3%E9%A2%98%E6%80%9D%E8%B7%AF%E5%88%86%E6%9E%90/</a><br />
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
先看words数组中的单词能不能被拼写出来,这取决于letters数组中有没有相应足够多的字符,如果没有肯定是要被pass掉的,关键在于,如果letters数组中存在足够多的字符能够拼出当前单词,那么我们就要做出选择,是拼当前字符还是不拼,这两种选择哪种会导致全局结果更好,当前是无法得知的,因此要递归到子问题中继续求解。</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
解题步骤大概如下:</div>
<ol style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 24px 1.5em; padding: 0px; vertical-align: baseline;">
<li style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">先统计下letters数组中26个小写字母各有多少个。</li>
<li style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">从words数组中第一个index的字符串开始递归,如果letters数组中没有足够多的字符能够组成当前字符串,index加一递归到下一层。</li>
<li style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">反之,我们有两种选择,第一,将当前字符拼写出来,同时在letters数组中减去使用掉的字符数量,再算出当前字符串能够得到的积分,最后index加一递归到下一层。第二中选择是,我们不拼写当前字符串,直接index加一递归到下一层。比较两种选择结果更好的一方返回。</li>
<li style="background: transparent; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;">递归结束条件为index到达words数组末尾。</li>
</ol>
<div class="line number1 index0 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">public</code> <code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">maxScoreWords(String[] words, </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">char</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[] letters, </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[] score) {</code></div>
<div class="line number2 index1 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[] letterCount = </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">new</code> <code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">26</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">]; </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 用来统计每个字符的个数</code></div>
<div class="line number3 index2 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 统计每个字符的个数</code></div>
<div class="line number4 index3 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">char</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">c : letters) letterCount[c-</code><code class="java string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(42, 0, 255) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">'a'</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">]++;</code></div>
<div class="line number5 index4 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 从第一个字符串开始递归</code></div>
<div class="line number6 index5 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">helper(words, letterCount, score, </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">, </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">);</code></div>
<div class="line number7 index6 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number8 index7 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// words:words数组</code></div>
<div class="line number9 index8 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// letterCount:letters数组中每种字符的个数</code></div>
<div class="line number10 index9 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// score:score数组</code></div>
<div class="line number11 index10 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// sum:当前为止的累计得分</code></div>
<div class="line number12 index11 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// index:当前index</code></div>
<div class="line number13 index12 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">helper(String[] words, </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[] letterCount, </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[] score, </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">sum, </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">index){</code></div>
<div class="line number14 index13 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 如果index达到数组末尾,递归结束,返回当前累计得分</code></div>
<div class="line number15 index14 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(index == words.length) </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">sum;</code></div>
<div class="line number16 index15 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">String word=words[index]; </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 当前字符串</code></div>
<div class="line number17 index16 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">res = </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">; </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 返回结果(累计得分)</code></div>
<div class="line number18 index17 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 如果存在足够的字符能够组成当前字符串</code></div>
<div class="line number19 index18 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(hasWord(word, letterCount)){</code></div>
<div class="line number20 index19 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 第一种选择:拼写出当前字符串</code></div>
<div class="line number21 index20 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">s=</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">; </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 当前字符串能够拿到的分数</code></div>
<div class="line number22 index21 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 循环字符串中每个字符</code></div>
<div class="line number23 index22 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">i=</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;i<word.length();i++){</code></div>
<div class="line number24 index23 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 消耗一个当前字符</code></div>
<div class="line number25 index24 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">letterCount[word.charAt(i)-</code><code class="java string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(42, 0, 255) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">'a'</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">]--;</code></div>
<div class="line number26 index25 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 累加分数</code></div>
<div class="line number27 index26 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">s+=score[word.charAt(i)-</code><code class="java string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(42, 0, 255) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">'a'</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">];</code></div>
<div class="line number28 index27 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number29 index28 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// sum加上当前字符串分数,index加一,递归到下一层</code></div>
<div class="line number30 index29 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">res = helper(words, letterCount, score, sum+s, index+</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">);</code></div>
<div class="line number31 index30 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 递归后将letterCount数组个数还原</code></div>
<div class="line number32 index31 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">i=</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;i<word.length();i++){</code></div>
<div class="line number33 index32 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">letterCount[word.charAt(i)-</code><code class="java string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(42, 0, 255) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">'a'</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">]++;</code></div>
<div class="line number34 index33 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number35 index34 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number36 index35 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 第二种选择:不拼写出当前字符串,即直接index加一递归到下一层</code></div>
<div class="line number37 index36 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 返回两种选择的最大值</code></div>
<div class="line number38 index37 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">Math.max(res, helper(words, letterCount, score, sum, index+</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">1</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">));</code></div>
<div class="line number39 index38 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number40 index39 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 查看letterCount中是否含有足够的字符以能够组成字符串word</code></div>
<div class="line number41 index40 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">boolean</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">hasWord(String word, </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[] letterCount){</code></div>
<div class="line number42 index41 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[] count = </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">new</code> <code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">[</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">26</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">];</code></div>
<div class="line number43 index42 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">i=</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;i<word.length();i++){</code></div>
<div class="line number44 index43 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">count[word.charAt(i)-</code><code class="java string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(42, 0, 255) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">'a'</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">]++;</code></div>
<div class="line number45 index44 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number46 index45 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">i=</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;i<</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">26</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;i++){</code></div>
<div class="line number47 index46 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(count[i]>letterCount[i]){</code></div>
<div class="line number48 index47 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">false</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;</code></div>
<div class="line number49 index48 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number50 index49 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number51 index50 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">true</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;</code></div>
<div class="line number52 index51 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<a href="https://leetcode.com/problems/maximum-score-words-formed-by-letters/discuss/425104/Detailed-Explanation-using-Recursion">https://leetcode.com/problems/maximum-score-words-formed-by-letters/discuss/425104/Detailed-Explanation-using-Recursion</a><br />
<ul style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; margin-bottom: 1em;">
Since the number of words is quite less (14), we can just generate all the subsets. Let's call a subset valid if all the words in that subset can be formed by using the given letters. It is clear that we need to maximize the score of valid subsets. To do this, we generate all subsets, and check if it is valid. If it is, we find its score and update the global maxima.</div>
<div style="box-sizing: border-box; margin-bottom: 1em;">
</div>
</li>
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; margin-bottom: 1em;">
</div>
<div style="box-sizing: border-box; margin-bottom: 1em;">
To generate the subsets, we create a function call <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">generate_subset(words, n)</code> which generate all the subsets of the vector <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">words</code>. To quickly recap, we have 2 choices for the last element, either to take it or to leave it. We store this choice in an array called <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">taken</code> where <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">taken[i]</code> represent that the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">i-th</code> element was taken in our journey. We then recurse for the remaining elements.</div>
<div style="box-sizing: border-box; margin-bottom: 1em;">
</div>
</li>
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; margin-bottom: 1em;">
</div>
<div style="box-sizing: border-box; margin-bottom: 1em;">
When <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code> becomes zero, it means we cannot make any more choices. So now, we traverse our <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">taken</code> array to find out the elements in this subset. Then we count the frequency of each letter in this subset. If the frequency of each letter is under the provided limit, it means it is a valid subet. Hence, we find the score of this subset and update the maxima</div>
</li>
</ul>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span>:
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">vector</span><<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">bool</span>> taken;
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">vector</span><<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>> count;
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">vector</span><<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>> score;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> max_sum = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">update_score</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-built_in" style="box-sizing: border-box;">vector</span><<span class="hljs-built_in" style="box-sizing: border-box;">string</span>>& words)</span></span>;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">generate_subset</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-built_in" style="box-sizing: border-box;">vector</span><<span class="hljs-built_in" style="box-sizing: border-box;">string</span>>& words, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> n)</span></span>;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">maxScoreWords</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-built_in" style="box-sizing: border-box;">vector</span><<span class="hljs-built_in" style="box-sizing: border-box;">string</span>>& words, <span class="hljs-built_in" style="box-sizing: border-box;">vector</span><<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">char</span>>& letters, <span class="hljs-built_in" style="box-sizing: border-box;">vector</span><<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>>& score)</span></span>;
};
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> Solution :: generate_subset(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">vector</span><<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">string</span>>& words, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> n)
{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(n == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>)
{
update_score(words);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span>;
}
taken[n<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>] = <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">true</span>;
generate_subset(words, n<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>);
taken[n<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>] = <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">false</span>;
generate_subset(words, n<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> Solution :: update_score(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">vector</span><<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">string</span>>& words)
{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> current_score = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">vector</span><<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>> freq(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">26</span>, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < words.size(); i++)
{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(taken[i])
{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">auto</span> ele : words[i])
{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> ind = ele - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'a'</span>;
current_score += score[ind];
freq[ind]++;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(freq[ind] > count[ind])
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span>;
}
}
}
max_sum = max(max_sum, current_score);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> Solution :: maxScoreWords(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">vector</span><<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">string</span>>& words, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">vector</span><<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">char</span>>& letters, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">vector</span><<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>>& score)
{
taken.resize(words.size(), <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">false</span>);
count.resize(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">26</span>, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">this</span>->score = score;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">auto</span> ele : letters)
count[ele - <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">'a'</span>]++;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> n = words.size();
generate_subset(words, n);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> max_sum;
}</code></pre>
<br />
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-70998415910149969582020-01-02T22:19:00.004-08:002020-01-02T22:19:38.444-08:00LeetCode 1254 - Number of Closed Islands<a href="https://leetcode.com/problems/number-of-closed-islands/">https://leetcode.com/problems/number-of-closed-islands/</a><br />
<div class="content__u3I1 question-content__JfgR" style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin: 1em 0px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Given a 2D <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">grid</code> consists of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0s</code> (land) and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1s</code> (water). An <em style="box-sizing: border-box;">island</em> is a maximal 4-directionally connected group of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;"><span style="box-sizing: border-box; font-family: monospace;">0</span>s</code> and a <em style="box-sizing: border-box;">closed island</em> is an island <span style="box-sizing: border-box; font-weight: bolder;">totally</span> (all left, top, right, bottom) surrounded by <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1s.</code></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Return the number of <em style="box-sizing: border-box;">closed islands</em>.</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 1:</span></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<img alt="" src="https://assets.leetcode.com/uploads/2019/10/31/sample_3_1610.png" style="border-style: none; box-sizing: border-box; height: 120px; max-width: 100%; vertical-align: middle; width: 240px;" /></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 2
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span>
Islands in gray are closed because they are completely surrounded by water (group of 1s).</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 2:</span></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<img alt="" src="https://assets.leetcode.com/uploads/2019/10/31/sample_4_1610.png" style="border-style: none; box-sizing: border-box; height: 80px; max-width: 100%; vertical-align: middle; width: 160px;" /></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 1
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 3:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> grid = [[1,1,1,1,1,1,1],
[1,0,0,0,0,0,1],
[1,0,1,1,1,0,1],
[1,0,1,0,1,0,1],
[1,0,1,1,1,0,1],
[1,0,0,0,0,0,1],
[1,1,1,1,1,1,1]]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 2
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Constraints:</span></div>
<ul style="box-sizing: border-box; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= grid.length, grid[0].length <= 100</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0 <= grid[i][j] <=1</code></li>
</ul>
</div>
</div>
X. <a href="https://leetcode.com/problems/number-of-closed-islands/discuss/425150/JavaC%2B%2B-with-picture-Number-of-Enclaves">https://leetcode.com/problems/number-of-closed-islands/discuss/425150/JavaC%2B%2B-with-picture-Number-of-Enclaves</a><br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
This is similar to <a href="https://leetcode.com/problems/number-of-enclaves/discuss/265555/C%2B%2B-with-picture-DFS-and-BFS" style="background-color: transparent; border-bottom: 1px solid transparent; box-sizing: border-box; color: #607d8b; cursor: pointer; outline: none; padding-bottom: 1px; pointer-events: auto; text-decoration-line: none; touch-action: manipulation; transition: border-bottom-color 0.3s ease 0s;">1020. Number of Enclaves</a>.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<h4 style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-weight: 500; margin-bottom: 0.5em; margin-top: 0px;">
Approach 1: Flood Fill</h4>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
First, we need to remove all land connected to the edges using flood fill.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<br />
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
Then, we can count and flood-fill the remaining islands.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Complexity Analysis</span></div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; margin-bottom: 1em;">
</div>
<div style="box-sizing: border-box; margin-bottom: 1em;">
Time: <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(n)</code>, where <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code> is the total number of cells. We flood-fill all land cells once.</div>
<div style="box-sizing: border-box; margin-bottom: 1em;">
</div>
</li>
<li style="box-sizing: border-box;"><div style="box-sizing: border-box; margin-bottom: 1em;">
</div>
<div style="box-sizing: border-box; margin-bottom: 1em;">
Memory: <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(n)</code> for the stack. Flood fill is DFS, and the maximum depth is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code>.</div>
</li>
</ul>
<br />
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 1em; margin-top: 0px; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<br />
<pre style="-webkit-text-stroke-width: 0px; background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin-bottom: 1em; margin-top: 0px; orphans: 2; overflow: auto; padding: 10px 15px; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; padding: 0px; tab-size: 4;">int fill(int[][] g, int <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>, int <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span>) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> || <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> || <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> >= g.<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">length</span> || <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> >= g[<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>].<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">length</span> || g[<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>][<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span>] != <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>)
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> (g[<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>][<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span>] = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) + fill(g, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span>) + fill(g, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>)
+ fill(g, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span>) + fill(g, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
}
public int closedIsland(int[][] g) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (int <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> < g.<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">length</span>; ++<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>)
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (int <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> < g[<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>].<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">length</span>; ++<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span>)
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> * <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> * (<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> - g.<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">length</span> + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) * (<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> - g[<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>].<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">length</span> + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>)
fill(g, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span>);
int res = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (int <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span> < g.<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">length</span>; ++<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>)
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (int <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span> < g[<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>].<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">length</span>; ++<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span>)
res += fill(g, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">i</span>, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">j</span>) > <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> ? <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> : <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> res;
}
</code></pre>
<a href="https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/">https://zxi.mytechroad.com/blog/searching/leetcode-1254-number-of-closed-islands/</a><br />
Solution: DFS/Backtracking<br />
For each connected component, if it can reach the boundary then it’s not a closed island.<br />
<br />
Time complexity: O(n*m)<br />
Space complexity: O(n*m)<br />
<div class="crayon-line crayon-striped-line" id="crayon-5e0db40865242379899670-4" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">closedIsland</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5e0db40865242379899670-5" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db40865242379899670-6" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5e0db40865242379899670-7" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">function</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">></span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dfs</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db40865242379899670-8" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">if</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">||</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">||</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">||</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5e0db40865242379899670-9" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">if</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db40865242379899670-10" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dfs</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dfs</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dfs</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dfs</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5e0db40865242379899670-11" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db40865242379899670-12" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5e0db40865242379899670-13" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ans</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db40865242379899670-14" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line" id="crayon-5e0db40865242379899670-15" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db40865242379899670-16" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">if</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">!</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ans</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dfs</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5e0db40865242379899670-17" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ans</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db40865242379899670-18" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div>
<span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><br /></span></div>
X. BFS<br />
<a href="https://algorithm-notes-allinone.blogspot.com/2019/11/leetcode-1254-number-of-closed-islands.html">https://algorithm-notes-allinone.blogspot.com/2019/11/leetcode-1254-number-of-closed-islands.html</a><br />
<ol class="linenums" style="color: #222222; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; margin-bottom: 0px; margin-top: 0px; white-space: pre;">
<li class="L2" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> closedIsland</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">vector</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">vector</span><span class="str" style="color: #008800;"><int></span><span class="pun" style="color: #666600;">>&</span><span class="pln" style="color: black;"> grid</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L3" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> res </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L4" style="margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> m </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> grid</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">size</span><span class="pun" style="color: #666600;">(),</span><span class="pln" style="color: black;"> n </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> grid</span><span class="pun" style="color: #666600;">[</span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">].</span><span class="pln" style="color: black;">size</span><span class="pun" style="color: #666600;">();</span></li>
<li class="L5" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> vector</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">vector</span><span class="str" style="color: #008800;"><int></span><span class="pun" style="color: #666600;">></span><span class="pln" style="color: black;"> visit</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">m</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> vector</span><span class="str" style="color: #008800;"><int></span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">n</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">));</span><span class="com" style="color: #880000;">//not needed if we can modify grid' values</span></li>
<li class="L6" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">for</span><span class="pun" style="color: #666600;">(</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> i</span><span class="pun" style="color: #666600;">=</span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> i</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">m</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">++</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L7" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">for</span><span class="pun" style="color: #666600;">(</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> j</span><span class="pun" style="color: #666600;">=</span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> j</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">n</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">++</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L8" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">if</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">grid</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">==</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">0</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">&&</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">!</span><span class="pln" style="color: black;">visit</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">])</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L9" style="background: rgb(238, 238, 238); margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> bfs</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">grid</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> i</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> j</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> visit</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> res</span><span class="pun" style="color: #666600;">);</span></li>
<li class="L0" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L1" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L2" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L3" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">return</span><span class="pln" style="color: black;"> res</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L4" style="margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L5" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span></li>
<li class="L6" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="kwd" style="color: #000088;">private</span><span class="pun" style="color: #666600;">:</span></li>
<li class="L7" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">void</span><span class="pln" style="color: black;"> bfs</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">vector</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">vector</span><span class="str" style="color: #008800;"><int></span><span class="pun" style="color: #666600;">>&</span><span class="pln" style="color: black;"> gd</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> x</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> y</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> vector</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">vector</span><span class="str" style="color: #008800;"><int></span><span class="pun" style="color: #666600;">>&</span><span class="pln" style="color: black;"> visit</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">&</span><span class="pln" style="color: black;">res</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L8" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> queue</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">pair</span><span class="pun" style="color: #666600;"><</span><span class="kwd" style="color: #000088;">int</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pun" style="color: #666600;">>></span><span class="pln" style="color: black;"> q</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L9" style="background: rgb(238, 238, 238); margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> q</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">push</span><span class="pun" style="color: #666600;">({</span><span class="pln" style="color: black;">x</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> y</span><span class="pun" style="color: #666600;">});</span></li>
<li class="L0" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> visit</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">x</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">y</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L1" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> vector</span><span class="str" style="color: #008800;"><int></span><span class="pln" style="color: black;"> dirs </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{-</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">-</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">};</span></li>
<li class="L2" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">bool</span><span class="pln" style="color: black;"> ed </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">false</span><span class="pun" style="color: #666600;">;</span><span class="com" style="color: #880000;">//this is the only difference</span></li>
<li class="L3" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">while</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">q</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">size</span><span class="pun" style="color: #666600;">())</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L4" style="margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">auto</span><span class="pln" style="color: black;"> t </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> q</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">front</span><span class="pun" style="color: #666600;">();</span></li>
<li class="L5" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> q</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">pop</span><span class="pun" style="color: #666600;">();</span></li>
<li class="L6" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">for</span><span class="pun" style="color: #666600;">(</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> i</span><span class="pun" style="color: #666600;">=</span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> i</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;"><</span><span class="pln" style="color: black;">dirs</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">size</span><span class="pun" style="color: #666600;">();</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">++</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L7" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> a </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> t</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">first </span><span class="pun" style="color: #666600;">+</span><span class="pln" style="color: black;"> dirs</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">],</span><span class="pln" style="color: black;"> b </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> t</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">second </span><span class="pun" style="color: #666600;">+</span><span class="pln" style="color: black;"> dirs</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">];</span></li>
<li class="L8" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">if</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">a</span><span class="pun" style="color: #666600;"><</span><span class="lit" style="color: #006666;">0</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">||</span><span class="pln" style="color: black;"> a</span><span class="pun" style="color: #666600;">>=</span><span class="pln" style="color: black;"> gd</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">size</span><span class="pun" style="color: #666600;">()</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">||</span><span class="pln" style="color: black;"> b</span><span class="pun" style="color: #666600;"><</span><span class="lit" style="color: #006666;">0</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">||</span><span class="pln" style="color: black;"> b</span><span class="pun" style="color: #666600;">>=</span><span class="pln" style="color: black;">gd</span><span class="pun" style="color: #666600;">[</span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">].</span><span class="pln" style="color: black;">size</span><span class="pun" style="color: #666600;">())</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L9" style="background: rgb(238, 238, 238); margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> ed </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">true</span><span class="pun" style="color: #666600;">;</span><span class="com" style="color: #880000;">//is edge</span></li>
<li class="L0" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">continue</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L1" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L2" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">if</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">visit</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">a</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">b</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">||</span><span class="pln" style="color: black;"> gd</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">a</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">b</span><span class="pun" style="color: #666600;">])</span><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">continue</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L3" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> q</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: black;">push</span><span class="pun" style="color: #666600;">({</span><span class="pln" style="color: black;">a</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> b</span><span class="pun" style="color: #666600;">});</span></li>
<li class="L4" style="margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> visit</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">a</span><span class="pun" style="color: #666600;">][</span><span class="pln" style="color: black;">b</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L5" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L6" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L7" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">if</span><span class="pun" style="color: #666600;">(!</span><span class="pln" style="color: black;">ed</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">++</span><span class="pln" style="color: black;">res</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L8" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
</ol>
<a href="https://www.cnblogs.com/seyjs/p/11841246.html">https://www.cnblogs.com/seyjs/p/11841246.html</a><br />
<a href="https://www.cnblogs.com/onePunchCoder/p/11830972.html">https://www.cnblogs.com/onePunchCoder/p/11830972.html</a><br />
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">private</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">static</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">final</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0<span style="line-height: 1.5; margin: 0px; padding: 0px;">}};
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">public</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> closedIsland(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">[][] grid) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> m =<span style="line-height: 1.5; margin: 0px; padding: 0px;"> grid.length;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> n = grid[0<span style="line-height: 1.5; margin: 0px; padding: 0px;">].length;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>[][] visited = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">[m][n];
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">boolean</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> flag;
LinkedList</span><<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>[]> queue = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> LinkedList<><span style="line-height: 1.5; margin: 0px; padding: 0px;">();
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> result = 0<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> i = 1; i < m - 1; i++<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> j = 1; j < n - 1; j++<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (grid[i][j] == 0 && visited[i][j] == 0<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
flag </span>= <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">true</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
queue.addLast(</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">[]{i, j});
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">[] island;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> I, J;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">while</span> (!<span style="line-height: 1.5; margin: 0px; padding: 0px;">queue.isEmpty()) {
island </span>=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> queue.pollFirst();
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">[] direction : directions) {
I </span>= island[0] + direction[0<span style="line-height: 1.5; margin: 0px; padding: 0px;">];
J </span>= island[1] + direction[1<span style="line-height: 1.5; margin: 0px; padding: 0px;">];
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (I >= 0 && I < m && J >= 0 && J < n && grid[I][J] == 0 && visited[I][J] == 0<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
visited[I][J] </span>= 1<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
queue.addLast(</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">[]{I, J});
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (I == 0 || I == m - 1 || J == 0 || J == n - 1<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
flag </span>= <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">false</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
}
}
}
}
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (flag) result++<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
}
}
}
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> result;
}</span></pre>
<div>
<span style="line-height: 1.5; margin: 0px; padding: 0px;"><br /></span></div>
<br />
<br />
<br />
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-19509415687844723742020-01-02T22:13:00.003-08:002020-01-02T22:13:27.177-08:00LeetCode 1253 - Reconstruct a 2-Row Binary Matrix<a href="https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/">https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/</a><br />
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Given the following details of a matrix with <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code> columns and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2</code> rows :</div>
<ul style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">The matrix is a binary matrix, which means each element in the matrix can be <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code> or <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1</code>.</li>
<li style="box-sizing: border-box;">The sum of elements of the 0-th(upper) row is given as <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">upper</code>.</li>
<li style="box-sizing: border-box;">The sum of elements of the 1-st(lower) row is given as <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">lower</code>.</li>
<li style="box-sizing: border-box;">The sum of elements in the i-th column(0-indexed) is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">colsum[i]</code>, where <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">colsum</code> is given as an integer array with length <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code>.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Your task is to reconstruct the matrix with <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">upper</code>, <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">lower</code> and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">colsum</code>.</div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Return it as a 2-D integer array.</div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
If there are more than one valid solution, any of them will be accepted.</div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
If no valid solution exists, return an empty 2-D array.</div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 1:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> upper = 2, lower = 1, colsum = [1,1,1]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> [[1,1,0],[0,0,1]]
<span style="box-sizing: border-box; font-weight: bolder;">Explanation: </span>[[1,0,1],[0,1,0]], and [[0,1,1],[1,0,0]] are also correct answers.
</pre>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 2:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> upper = 2, lower = 3, colsum = [2,2,1,1]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> []
</pre>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 3:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> upper = 5, lower = 5, colsum = [2,1,2,0,1,0,1,2,0,1]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> [[1,1,1,0,1,0,0,1,0,0],[1,0,1,0,0,0,1,1,0,1]]
</pre>
<div>
<br /></div>
<div>
<a href="https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/discuss/425793/C%2B%2BJava-5-lines">https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/discuss/425793/C%2B%2BJava-5-lines</a></div>
<div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
If the column sum is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2</code> or <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code>, the choice is obvius.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
If it's <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1</code>, we set the upper bit if <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">upper</code> is larger than <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">lower</code>, and lower bit otherwise.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
</div>
<div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> List<List<Integer>> reconstructMatrix(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> u, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> l, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>[] cs) {
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">bool</span>ean[][] res = new <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">bool</span>ean[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>][cs.length];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < cs.length; ++i) {
res[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>][i] = cs[i] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span> || (cs[i] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> && l < u);
res[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>][i] = cs[i] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span> || (cs[i] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> && !res[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>][i]);
u -= res[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>][i] ? <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> : <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
l -= res[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>][i] ? <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> : <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> l == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> && u == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> ? new ArrayList(Arrays.asList(res[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>], res[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>])) : new ArrayList();
}</code></pre>
</div>
<br />
<a href="https://www.cnblogs.com/onePunchCoder/p/11831345.html">https://www.cnblogs.com/onePunchCoder/p/11831345.html</a><br />
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">public</span> List<List<Integer>> reconstructMatrix(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> upper, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> lower, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">[] colums) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> sum = 0<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> numof2 = 0<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> colum : colums) {
sum </span>+=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> colum;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (colum == 2<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
numof2</span>++<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
}
}
List</span><List<Integer>> result = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> ArrayList<><span style="line-height: 1.5; margin: 0px; padding: 0px;">();
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (sum != upper + lower || numof2 ><span style="line-height: 1.5; margin: 0px; padding: 0px;"> Math.min(upper, lower)) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> result;
}
List</span><Integer> upperList = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> ArrayList<><span style="line-height: 1.5; margin: 0px; padding: 0px;">();
List</span><Integer> lowerList = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> ArrayList<><span style="line-height: 1.5; margin: 0px; padding: 0px;">();
result.add(upperList);
result.add(lowerList);
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> colum : colums) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (colum == 0<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
upperList.add(</span>0<span style="line-height: 1.5; margin: 0px; padding: 0px;">);
lowerList.add(</span>0<span style="line-height: 1.5; margin: 0px; padding: 0px;">);
} </span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">else</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (colum == 2<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
upperList.add(</span>1<span style="line-height: 1.5; margin: 0px; padding: 0px;">);
upper</span>--<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
lowerList.add(</span>1<span style="line-height: 1.5; margin: 0px; padding: 0px;">);
lower</span>--<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
} </span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">else</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (upper ><span style="line-height: 1.5; margin: 0px; padding: 0px;"> lower) {
upperList.add(</span>1<span style="line-height: 1.5; margin: 0px; padding: 0px;">);
lowerList.add(</span>0<span style="line-height: 1.5; margin: 0px; padding: 0px;">);
upper</span>--<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
} </span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">else</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> {
upperList.add(</span>0<span style="line-height: 1.5; margin: 0px; padding: 0px;">);
lowerList.add(</span>1<span style="line-height: 1.5; margin: 0px; padding: 0px;">);
lower</span>--<span style="line-height: 1.5; margin: 0px; padding: 0px;">;
}
}
}
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> result;
}</span></pre>
<a href="https://www.codetd.com/en/article/7811117">https://www.codetd.com/en/article/7811117</a><br />
<div style="background-color: white; box-sizing: border-box; color: #454545; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 1rem;">
<span style="box-sizing: border-box; vertical-align: inherit;">2, a brief translation:</span></div>
<div style="background-color: white; box-sizing: border-box; color: #454545; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 1rem;">
<span style="box-sizing: border-box; vertical-align: inherit;"><span style="box-sizing: border-box; vertical-align: inherit;">Construction of a plurality of data rows two columns, and the first row of upper, and the second row of Lower, and to colsum [i] of the i-th column. </span><span style="box-sizing: border-box; vertical-align: inherit;">If such an array does not exist returns NULL</span></span></div>
<div style="background-color: white; box-sizing: border-box; color: #454545; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 1rem;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #454545; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 1rem;">
<span style="box-sizing: border-box; vertical-align: inherit;">3, code analysis:</span></div>
<ul style="background-color: white; box-sizing: border-box; color: #454545; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; margin-bottom: 1rem; margin-top: 0px;">
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; vertical-align: inherit;">If the upper + lower does not mean colsum and then return empty.</span></li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; vertical-align: inherit;">If the number 2 is greater than colsum in upper or lower, return null</span></li>
<li style="box-sizing: border-box;"><span style="box-sizing: border-box; vertical-align: inherit;"><span style="box-sizing: border-box; vertical-align: inherit;">From left to right, a premise is inserted vertically keep balance. </span><span style="box-sizing: border-box; vertical-align: inherit;">(When colums [i] == 1, if lower> = upper plug, the plug remaining cases)</span></span></li>
</ul>
<div class="cnblogs_code" style="background-color: white; box-sizing: border-box; color: #454545; font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px;">
<pre class="hljs cs" style="background: rgb(240, 240, 240); box-sizing: border-box; color: #444444; font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 14px; line-height: 1.2em !important; margin-bottom: 1rem; max-width: 1000px; overflow: auto; padding: 0.5em; white-space: pre-wrap;"><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">public</span></span> List<List<Integer>> reconstructMatrix(<span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span></span> upper, <span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span></span> lower, <span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span></span><span style="box-sizing: border-box; color: black;">[] colums) {
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span></span> sum = <span class="hljs-number" style="box-sizing: border-box; color: #880000;">0</span><span style="box-sizing: border-box; color: black;">;
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span></span> numof2 = <span class="hljs-number" style="box-sizing: border-box; color: #880000;">0</span><span style="box-sizing: border-box; color: black;">;
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span></span> (<span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span></span><span style="box-sizing: border-box; color: black;"> colum : colums) {
sum </span>+=<span style="box-sizing: border-box; color: black;"> colum;
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span></span> (colum == <span class="hljs-number" style="box-sizing: border-box; color: #880000;">2</span><span style="box-sizing: border-box; color: black;">) {
numof2</span>++<span style="box-sizing: border-box; color: black;">;
}
}
List</span><List<Integer>> result = <span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">new</span></span> ArrayList<><span style="box-sizing: border-box; color: black;">();
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span></span> (sum != upper + lower || numof2 ><span style="box-sizing: border-box; color: black;"> Math.min(upper, lower)) {
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span></span><span style="box-sizing: border-box; color: black;"> result;
}
List</span><Integer> upperList = <span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">new</span></span> ArrayList<><span style="box-sizing: border-box; color: black;">();
List</span><Integer> lowerList = <span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">new</span></span> ArrayList<><span style="box-sizing: border-box; color: black;">();
result.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(upperList);
result.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(lowerList);
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">for</span></span> (<span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">int</span></span><span style="box-sizing: border-box; color: black;"> colum : colums) {
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span></span> (colum == <span class="hljs-number" style="box-sizing: border-box; color: #880000;">0</span><span style="box-sizing: border-box; color: black;">) {
upperList.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(</span><span class="hljs-number" style="box-sizing: border-box; color: #880000;">0</span><span style="box-sizing: border-box; color: black;">);
lowerList.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(</span><span class="hljs-number" style="box-sizing: border-box; color: #880000;">0</span><span style="box-sizing: border-box; color: black;">);
} </span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">else</span></span> <span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span></span> (colum == <span class="hljs-number" style="box-sizing: border-box; color: #880000;">2</span><span style="box-sizing: border-box; color: black;">) {
upperList.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(</span><span class="hljs-number" style="box-sizing: border-box; color: #880000;">1</span><span style="box-sizing: border-box; color: black;">);
upper</span>--<span style="box-sizing: border-box; color: black;">;
lowerList.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(</span><span class="hljs-number" style="box-sizing: border-box; color: #880000;">1</span><span style="box-sizing: border-box; color: black;">);
lower</span>--<span style="box-sizing: border-box; color: black;">;
} </span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">else</span></span><span style="box-sizing: border-box; color: black;"> {
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">if</span></span> (upper ><span style="box-sizing: border-box; color: black;"> lower) {
upperList.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(</span><span class="hljs-number" style="box-sizing: border-box; color: #880000;">1</span><span style="box-sizing: border-box; color: black;">);
lowerList.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(</span><span class="hljs-number" style="box-sizing: border-box; color: #880000;">0</span><span style="box-sizing: border-box; color: black;">);
upper</span>--<span style="box-sizing: border-box; color: black;">;
} </span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">else</span></span><span style="box-sizing: border-box; color: black;"> {
upperList.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(</span><span class="hljs-number" style="box-sizing: border-box; color: #880000;">0</span><span style="box-sizing: border-box; color: black;">);
lowerList.<span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">add</span>(</span><span class="hljs-number" style="box-sizing: border-box; color: #880000;">1</span><span style="box-sizing: border-box; color: black;">);
lower</span>--<span style="box-sizing: border-box; color: black;">;
}
}
}
</span><span style="box-sizing: border-box; color: blue;"><span class="hljs-keyword" style="box-sizing: border-box; font-weight: bold;">return</span></span><span style="box-sizing: border-box; color: black;"> result;
}</span></pre>
</div>
<a href="https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/discuss/425170/Detailed-Explanation-using-Greedy-Approach">https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/discuss/425170/Detailed-Explanation-using-Greedy-Approach</a><br />
<ul style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">First, intialize both the rows as <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code>. Now, fill the indices where the vertical column sum is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2</code> (as they don't have a choice). So, now we only need to make choices for the columns with sum <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1</code>. Find out the current sum of the first row. Compare it with the required amount. If the difference becomes negative, it means that there is no solution. Else, greedily fill the required <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1s</code> in the top row and the remaining <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1s</code> in the bottom row. Finally, check if the sum of the bottom row equals <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">lower</code></li>
</ul>
<a href="https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/discuss/425110/O(n)-time-Java-Solution-Easy-to-understand-with-Comments-and-Explaination">https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix/discuss/425110/O(n)-time-Java-Solution-Easy-to-understand-with-Comments-and-Explaination</a><br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
We count the number of columns for which we need 1 in both rows(colsum[i] == 2), similarly we count the number of columns for which we need column-sum as 1 and column-sum as 0.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
For the columns where we need the column-sum as 2, we definitely know that we need 1 in both the rows, similarly if the column-sum is 0 we know we need to place 0s in both the rows.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
For the case where we need column-sum as 1, we need to see if we can have a 1 in row1 or do we have to have a 1 in row2.<br style="box-sizing: border-box;" />For those cases I have done a precomputation and followed a somewhat greedy approach.<br style="box-sizing: border-box;" />The number of columns where we need a 1 in row1 and 0 in row2 is say count1 (as mentioned in code).<br style="box-sizing: border-box;" />We start assigning values to the two rows now by iterating over each value in the colsum array. If we encounter a colsum[i] == 2 or colsum[i] == 0, we assign 1s to both the rows and 0s to both the rows respectively.<br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-weight: bolder;">For the cases where colsum[i] == 1, we check value of count1 variable which will tell us if we can assign a 1 to row1 or not. If value of count1 > 0, we can assign 1 to row1 and 0 to row2 and we simultaneously decrement count1. Else if count1 == 0, we assign a 0 to row1 and 1 to row2.</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">public</span> List<List<<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">Integer</span>>> reconstructMatrix(<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> upper, <span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> lower, <span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span>[] colsum) {
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> n = colsum.length;
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> sum0 = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; // <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">no</span>. <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">of</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">columns</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">with</span> colsum <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> sum1 = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; // <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">no</span>. <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">of</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">columns</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">with</span> colsum <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> sum2 = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; // <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">no</span>. <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">of</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">columns</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">with</span> colsum <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < n; i++){
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(colsum[i] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) sum0 ++;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(colsum[i] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) sum1 ++;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span> sum2 ++;
}
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> count1 = upper - sum2; // <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">no</span>. <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">of</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">columns</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">with</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>st <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">row</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">and</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>nd <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">row</span>
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> count2 = lower - sum2; // <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">no</span>. <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">of</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">columns</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">with</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>st <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">row</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">and</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>nd <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">row</span>
// <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">check</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> arrangement <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">is</span> possible <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">or</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">not</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(count1 < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> || count2 < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> || count1 + count2 != sum1) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">new</span> ArrayList<>();
List<List<<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">Integer</span>>> ans = <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">new</span> ArrayList<>();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>; i++) ans.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">new</span> ArrayList<>());
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < n; i++){
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(colsum[i] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>){
ans.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">get</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>).<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
ans.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">get</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>).<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(colsum[i] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>){
ans.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">get</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>).<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>);
ans.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">get</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>).<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(count1 > <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>){
count1 <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">--;</span>
ans.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">get</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>).<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
ans.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">get</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>).<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span>{
ans.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">get</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>).<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>);
ans.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">get</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>).<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
}
}
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans;
}
</code></pre>
<div>
<code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><a href="https://www.acwing.com/solution/LeetCode/content/6123/">https://www.acwing.com/solution/LeetCode/content/6123/</a></code></div>
<div>
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(贪心) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;"><code>colsum</code> 为 0 或者 2 的列的值可以直接确定,根据情况,更新 <code>upper</code> 和 <code>lower</code>,此时 <code>upper</code> 和 <code>lower</code> 记录当前行还需要多少个 1。如果这时候 <code>upper</code> 或 <code>lower</code> 小于 0,则直接返回空数组。</li>
<li style="line-height: 1.5em;">接下来处理 <code>colsum</code> 为 1 的列,如果 <code>upper</code> 大于 0,则这一列第一行填 0,第二行填 1,然后 <code>upper</code> 减 1。反之亦然。最后处理结束后,如果 <code>upper</code> 或 <code>lower</code> 仍大于 0,则返回空数组。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">每个位置仅遍历两次,故时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">答案需要额外 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的空间。</li>
</ul>
</div>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>> reconstructMatrix(<span class="hljs-keyword" style="display: initial !important;">int</span> upper, <span class="hljs-keyword" style="display: initial !important;">int</span> lower, <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>& colsum) {
<span class="hljs-keyword" style="display: initial !important;">int</span> n = colsum.size();
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>> res(<span class="hljs-number" style="display: initial !important;">2</span>, <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>(n));
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i < n; i++)
<span class="hljs-keyword" style="display: initial !important;">if</span> (colsum[i] == <span class="hljs-number" style="display: initial !important;">0</span>) {
res[<span class="hljs-number" style="display: initial !important;">0</span>][i] = res[<span class="hljs-number" style="display: initial !important;">1</span>][i] = <span class="hljs-number" style="display: initial !important;">0</span>;
} <span class="hljs-keyword" style="display: initial !important;">else</span> <span class="hljs-keyword" style="display: initial !important;">if</span> (colsum[i] == <span class="hljs-number" style="display: initial !important;">2</span>) {
res[<span class="hljs-number" style="display: initial !important;">0</span>][i] = res[<span class="hljs-number" style="display: initial !important;">1</span>][i] = <span class="hljs-number" style="display: initial !important;">1</span>;
upper--; lower--;
<span class="hljs-keyword" style="display: initial !important;">if</span> (upper < <span class="hljs-number" style="display: initial !important;">0</span> || lower < <span class="hljs-number" style="display: initial !important;">0</span>)
<span class="hljs-keyword" style="display: initial !important;">return</span> {};
}
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i < n; i++)
<span class="hljs-keyword" style="display: initial !important;">if</span> (colsum[i] == <span class="hljs-number" style="display: initial !important;">1</span>) {
<span class="hljs-keyword" style="display: initial !important;">if</span> (upper > <span class="hljs-number" style="display: initial !important;">0</span>) {
res[<span class="hljs-number" style="display: initial !important;">0</span>][i] = <span class="hljs-number" style="display: initial !important;">1</span>; res[<span class="hljs-number" style="display: initial !important;">1</span>][i] = <span class="hljs-number" style="display: initial !important;">0</span>;
upper--;
} <span class="hljs-keyword" style="display: initial !important;">else</span> <span class="hljs-keyword" style="display: initial !important;">if</span> (lower > <span class="hljs-number" style="display: initial !important;">0</span>) {
res[<span class="hljs-number" style="display: initial !important;">0</span>][i] = <span class="hljs-number" style="display: initial !important;">0</span>; res[<span class="hljs-number" style="display: initial !important;">1</span>][i] = <span class="hljs-number" style="display: initial !important;">1</span>;
lower--;
} <span class="hljs-keyword" style="display: initial !important;">else</span> {
<span class="hljs-keyword" style="display: initial !important;">return</span> {};
}
}
<span class="hljs-keyword" style="display: initial !important;">if</span> (upper > <span class="hljs-number" style="display: initial !important;">0</span> || lower > <span class="hljs-number" style="display: initial !important;">0</span>)
<span class="hljs-keyword" style="display: initial !important;">return</span> {};
<span class="hljs-keyword" style="display: initial !important;">return</span> res;
}
</code></pre>
<div>
<br /></div>
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-64908648809858575092020-01-02T22:07:00.001-08:002020-01-02T22:07:33.503-08:00LeetCode 1252 - Cells with Odd Values in a Matrix<a href="https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/">https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/</a><br />
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Given <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code> and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m</code> which are the dimensions of a matrix initialized by zeros and given an array <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">indices</code> where <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">indices[i] = [ri, ci]</code>. For each pair of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">[ri, ci]</code> you have to increment all cells in row <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">ri</code> and column <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">ci</code> by 1.</div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Return <em style="box-sizing: border-box;">the number of cells with odd values</em> in the matrix after applying the increment to all <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">indices</code>.</div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 1:</span></div>
<img alt="" src="https://assets.leetcode.com/uploads/2019/10/30/e1.png" style="background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; height: 118px; max-width: 100%; vertical-align: middle; width: 600px;" /><span style="background-color: white; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px;"></span><br />
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> n = 2, m = 3, indices = [[0,1],[1,1]]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 6
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span> Initial matrix = [[0,0,0],[0,0,0]].
After applying first increment it becomes [[1,2,1],[0,1,0]].
The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.
</pre>
<div style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 2:</span></div>
<img alt="" src="https://assets.leetcode.com/uploads/2019/10/30/e2.png" style="background-color: white; border-style: none; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; height: 150px; max-width: 100%; vertical-align: middle; width: 600px;" /><span style="background-color: white; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px;"></span><br />
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> n = 2, m = 2, indices = [[1,1],[0,0]]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 0
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span> Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.
</pre>
<div>
<a href="https://zxi.mytechroad.com/blog/simulation/leetcode-1252-cells-with-odd-values-in-a-matrix/">https://zxi.mytechroad.com/blog/simulation/leetcode-1252-cells-with-odd-values-in-a-matrix/</a></div>
<div>
<div>
Solution 2: Counting</div>
<div>
For each row and column, compute how many times it will be increased (odd or even).</div>
<div>
For each a[i][j], check how many times the i-th row and j-th column were increased, if the sum is odd then a[i][j] will odd.</div>
<div>
Time complexity: O(n*m + k)</div>
<div>
Space complexity: O(n+m)</div>
<div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b735293252203-4" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">oddCells</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">indices</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5e0db4092b735293252203-5" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">></span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">cols</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b735293252203-6" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">></span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">rows</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5e0db4092b735293252203-7" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">auto</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">idx</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">:</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">indices</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b735293252203-8" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">rows</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">idx</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">^=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5e0db4092b735293252203-9" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">cols</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">idx</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">^=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b735293252203-10" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5e0db4092b735293252203-11" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ans</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b735293252203-12" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line" id="crayon-5e0db4092b735293252203-13" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b735293252203-14" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ans</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">rows</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">^</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">cols</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5e0db4092b735293252203-15" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ans</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b735293252203-16" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
</div>
</div>
<div>
<span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><br /></span></div>
<div>
<br /></div>
<div>
<a href="https://www.acwing.com/solution/LeetCode/content/6121/">https://www.acwing.com/solution/LeetCode/content/6121/</a></div>
<div>
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(数学,哈希表) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>L</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>L</mi><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">暴力的做法很简单,直接模拟即可。这里我们假设 <code>n</code> 和 <code>m</code> 都给到了 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>10</mn><mn>9</mn></msup></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>10</mn><mn>9</mn></msup></math></span></span>,<code>indices.length</code> 即 <code>L</code> 给到了 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>10</mn><mn>6</mn></msup></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>10</mn><mn>6</mn></msup></math></span></span>。</li>
<li style="line-height: 1.5em;">分析哪些单元格最后会变成奇数。我们按行来看,假设这一行有过奇数次更新,则这一行中经过偶数次更新的列可以成为奇数;如果这一行经过偶数次更新,则这一行中经过奇数次更新的列可以成为奇数。</li>
<li style="line-height: 1.5em;">我们如果统计出了有多少行经过了奇数次更新 <code>cnt_r</code>,以及有多少列经过了奇数次更新 <code>cnt_c</code>,则答案就是 <code>cnt_r * (m - cnt_c) + cnt_c * (n - cnt_r)</code>。</li>
<li style="line-height: 1.5em;">统计以上信息可以用一个哈希表来记录。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">由于哈希表的时间复杂度是常数,我们只遍历一次 <code>indices</code> 数组即可完成统计。</li>
<li style="line-height: 1.5em;">故总时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>L</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-4-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>L</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">由于需要存储哈希表,而哈希表的大小与 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>" id="MathJax-Element-5-Frame" role="presentation" tabindex="0">L</span> 线性相关,故空间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>L</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-6-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>L</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
</div>
<div>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> <span class="hljs-title" style="display: initial !important;">oddCells</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-keyword" style="display: initial !important;">int</span> n, <span class="hljs-keyword" style="display: initial !important;">int</span> m, <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>>& indices)</span> </span>{
<span class="hljs-built_in" style="display: initial !important;">unordered_map</span><<span class="hljs-keyword" style="display: initial !important;">int</span>, <span class="hljs-keyword" style="display: initial !important;">int</span>> r, c;
<span class="hljs-keyword" style="display: initial !important;">int</span> cnt_r = <span class="hljs-number" style="display: initial !important;">0</span>, cnt_c = <span class="hljs-number" style="display: initial !important;">0</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">auto</span> &v : indices) {
<span class="hljs-keyword" style="display: initial !important;">int</span> &vr = r[v[<span class="hljs-number" style="display: initial !important;">0</span>]], &vc = c[v[<span class="hljs-number" style="display: initial !important;">1</span>]];
vr ^= <span class="hljs-number" style="display: initial !important;">1</span>; vc ^= <span class="hljs-number" style="display: initial !important;">1</span>;
cnt_r += (vr & <span class="hljs-number" style="display: initial !important;">1</span>) ? <span class="hljs-number" style="display: initial !important;">1</span> : <span class="hljs-number" style="display: initial !important;">-1</span>;
cnt_c += (vc & <span class="hljs-number" style="display: initial !important;">1</span>) ? <span class="hljs-number" style="display: initial !important;">1</span> : <span class="hljs-number" style="display: initial !important;">-1</span>;
}
<span class="hljs-keyword" style="display: initial !important;">return</span> cnt_r * (m - cnt_c) + cnt_c * (n - cnt_r);
}
</code></pre>
</div>
<div>
<br /></div>
<div>
<div>
Solution 1: Simulation</div>
<div>
Time complexity: O((n+m)*k + n*m)</div>
<div>
Space complexity: O(n*m)</div>
<div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b72e060305156-4" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">oddCells</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">indices</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5e0db4092b72e060305156-5" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">a</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">></span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b72e060305156-6" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">auto</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">idx</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">:</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">indices</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5e0db4092b72e060305156-7" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">a</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">idx</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">x</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b72e060305156-8" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">a</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">y</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">idx</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5e0db4092b72e060305156-9" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b72e060305156-10" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ans</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5e0db4092b72e060305156-11" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b72e060305156-12" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line" id="crayon-5e0db4092b72e060305156-13" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ans</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">a</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5e0db4092b72e060305156-14" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ans</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5e0db4092b72e060305156-15" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
</div>
</div>
<div>
<span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><a href="https://www.cnblogs.com/qinduanyinghua/p/11835874.html">https://www.cnblogs.com/qinduanyinghua/p/11835874.html</a></span></div>
<div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
时间复杂度:O(L*(n + m) + n * m)</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
空间复杂度:O(n * m)</div>
</div>
<div>
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 1</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> oddCells(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> n, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> m, vector<vector<<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>>>&<span style="line-height: 1.5; margin: 0px; padding: 0px;"> indices) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 2</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> cnt = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 3</span> vector<vector<<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>> > matrix(n, vector<<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>>(m, <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">));
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 4</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> i = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>; i < indices.size(); ++<span style="line-height: 1.5; margin: 0px; padding: 0px;">i) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 5</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> r = indices[i][<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>], c = indices[i][<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">];
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 6</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> j = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>; j < m; ++<span style="line-height: 1.5; margin: 0px; padding: 0px;">j) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 7</span> matrix[r][j] += <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 8</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 9</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> k = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>; k < n; ++<span style="line-height: 1.5; margin: 0px; padding: 0px;">k) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">10</span> matrix[k][c] += <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">11</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">12</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">13</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> i = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>; i < n; ++<span style="line-height: 1.5; margin: 0px; padding: 0px;">i) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">14</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> j = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>; j < m; ++<span style="line-height: 1.5; margin: 0px; padding: 0px;">j) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">15</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> ((matrix[i][j] & <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span>) == <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">)
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">16</span> ++<span style="line-height: 1.5; margin: 0px; padding: 0px;">cnt;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">17</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">18</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">19</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> cnt;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">20</span> }</pre>
</div>
<div>
<br /></div>
<div>
<a href="https://www.cnblogs.com/seyjs/p/11840467.html">https://www.cnblogs.com/seyjs/p/11840467.html</a></div>
<div>
<span style="background-color: white; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px;">首先遍历indices,计算出每行每列分别做了几次+1的操作。然后再遍历matrix,根据matrix[i][j]求得对应的i行和j列分别做了几次+1,判断两者之和的奇偶性即可。</span></div>
<div>
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="line-height: 1.8; margin: 0px; padding: 0px;"> </span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">def</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> oddCells(self, n, m, indices):
</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">"""</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">
:type n: int
:type m: int
:type indices: List[List[int]]
:rtype: int
</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">"""</span><span style="line-height: 1.8; margin: 0px; padding: 0px;">
dic_row </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> {}
dic_column </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> {}
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">for</span> (r,c) <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> indices:
dic_row[r] </span>= dic_row.setdefault(r,0) + 1<span style="line-height: 1.8; margin: 0px; padding: 0px;">
dic_column[c] </span>= dic_column.setdefault(c, 0) + 1<span style="line-height: 1.8; margin: 0px; padding: 0px;">
res </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> 0
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">for</span> i <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> range(n):
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">for</span> j <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> range(m):
count </span>= dic_row.get(i,0) +<span style="line-height: 1.8; margin: 0px; padding: 0px;"> dic_column.get(j,0)
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">if</span> count % 2 == 1:res += 1
<span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">return</span> res</pre>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">思路二:我们判断矩阵(i,j)位置的奇偶性,只需要计算第i行加1的次数 + 第j列加1的次数,就是位置(i, j)的值,只需要判断它的奇偶性就行。进一步,我们只需要知道第i行加1的总次数的奇偶性,以及第j列加1的总次数的奇偶性,这两个数进行异或操作就行(同奇同偶,加起来肯定是偶数,异或结果为0,否则为1)</span></div>
<div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
时间复杂度:O(L + n * m)</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
空间复杂度:O(n + m)</div>
</div>
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 1</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> oddCells(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> n, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> m, vector<vector<<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>>>&<span style="line-height: 1.5; margin: 0px; padding: 0px;"> indices) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 2</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> cnt = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 3</span> vector<<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>> row(n, <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>), col(m, <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>); <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">row(i)表示加1次数的值的奇偶性,0为偶数次,1为奇数次</span>
<span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 4</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> (auto ind: indices) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 5</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> r = ind[<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>], c = ind[<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">];
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 6</span> row[r] ^= <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 7</span> col[c] ^= <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 8</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 9</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> i = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>; i < n; ++<span style="line-height: 1.5; margin: 0px; padding: 0px;">i) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">10</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> j = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>; j < m; ++<span style="line-height: 1.5; margin: 0px; padding: 0px;">j) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">11</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (row[i] ^<span style="line-height: 1.5; margin: 0px; padding: 0px;"> col[j])
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">12</span> ++<span style="line-height: 1.5; margin: 0px; padding: 0px;">cnt;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">13</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">14</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">15</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> cnt;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">16</span> }</pre>
<br />
<span style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">思路三:基于思路二,我们发现判断(i, j)位置的值是否为奇数,只用看row(i) ^ col(j), </span><span style="background-color: #888888; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px;">如果(i, j)位置的数为奇数,则row(i)或者col(j)必有一个是奇数, 如果两个都是奇数,那么为偶数。</span><br />
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
时间复杂度:O(L + n + m)</div>
<span style="background-color: #888888; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px;"></span><br />
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
空间复杂度:O(n + m)</div>
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 1</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> oddCells(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> n, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> m, vector<vector<<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>>>&<span style="line-height: 1.5; margin: 0px; padding: 0px;"> indices) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 2</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> cnt = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 3</span> vector<<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>> row(n, <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>), col(m, <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>); <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">row(i)表示加1次数的值的奇偶性,0为偶数次,1为奇数次</span>
<span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 4</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> (auto ind: indices) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 5</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> r = ind[<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>], c = ind[<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">];
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 6</span> row[r] ^= <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 7</span> col[c] ^= <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 8</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 9</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> cntRow = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>, cntCol = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">10</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> (auto i:row)
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">11</span> cntRow +=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> i;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">12</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> (auto i:col)
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">13</span> cntCol +=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> i;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">14</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span> m * cntRow + n * cntCol - <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">2</span> * cntRow *<span style="line-height: 1.5; margin: 0px; padding: 0px;"> cntCol;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">15</span> }</pre>
<br />
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
时间复杂度:O(L)</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
空间复杂度:O(n + m)</div>
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 1</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> oddCells(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> n, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> m, vector<vector<<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>>>&<span style="line-height: 1.5; margin: 0px; padding: 0px;"> indices) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 2</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> cntRow = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>, cntCol = <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 3</span> vector<<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>> row(n, <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>), col(m, <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>); <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">row(i)表示加1次数的值的奇偶性,0为偶数次,1为奇数次</span>
<span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 4</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> (auto ind: indices) {
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 5</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> r = ind[<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">0</span>], c = ind[<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">];
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 6</span> row[r] ^= <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 7</span> col[c] ^= <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 8</span> cntRow += (row[r] == <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span>) ? <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span> : -<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;"> 9</span> cntCol += (col[c] == <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span>) ? <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span> : -<span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">1</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">10</span> <span style="line-height: 1.5; margin: 0px; padding: 0px;"> }
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">11</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span> m * cntRow + n * cntCol - <span style="color: purple; line-height: 1.5; margin: 0px; padding: 0px;">2</span> * cntRow *<span style="line-height: 1.5; margin: 0px; padding: 0px;"> cntCol;
</span><span style="color: teal; line-height: 1.5; margin: 0px; padding: 0px;">12</span> }</pre>
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-25739715183418680592019-12-27T13:16:00.000-08:002019-12-27T14:28:51.505-08:00Google Interview Questions Deconstructed<a href="https://medium.com/@alexgolec">https://medium.com/@alexgolec</a><br />
<br />
<a href="https://medium.com/@alexgolec/introducing-google-interview-questions-deconstructed-a012e41ea631">https://medium.com/@alexgolec/introducing-google-interview-questions-deconstructed-a012e41ea631</a><br />
<br />
<a href="https://hackernoon.com/google-interview-questions-deconstructed-the-knights-dialer-f780d516f029">https://hackernoon.com/google-interview-questions-deconstructed-the-knights-dialer-f780d516f029</a><br />
More at <a href="https://massivealgorithms.blogspot.com/2018/11/leetcode-935-knight-dialer.html">LeetCode 935 - Knight Dialer</a><br />
<div style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-size: 20px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.6; margin-bottom: 35px; margin-top: 35px; padding: 0px; position: relative; vertical-align: baseline;">
I like it because it hits number of sweet spots:</div>
<ul style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; list-style-image: initial; list-style-position: initial; margin: 0px; padding: 0px 0px 0px 40px; vertical-align: baseline;">
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: 20px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.6; margin: 35px 0px; padding: 0px; position: relative; vertical-align: baseline;">It’s easy to state and understand.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: 20px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.6; margin: 35px 0px; padding: 0px; position: relative; vertical-align: baseline;">It has a number of solutions, each requiring varying degrees of algorithms and data structures knowledge. Also, a little bit of insight goes a long way.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: 20px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.6; margin: 35px 0px; padding: 0px; position: relative; vertical-align: baseline;">Each solution can be implemented in relatively few lines, making it perfect for a time-constrained environment.</li>
</ul>
<a href="https://medium.com/@alexgolec/google-interview-questions-deconstructed-the-knights-dialer-impossibly-fast-edition-c288da1685b8" target="_blank">Google Interview Questions Deconstructed: The Knight’s Dialer (Logarithmic Time Edition)</a><br />
<br />
<br />
<a href="https://medium.com/@alexgolec/google-interview-problems-synonymous-queries-36425145387c" target="_blank">Google Interview Questions Deconstructed: Synonymous Queries</a><br />
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="1866" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
Imagine you operate a popular search engine and in your logs you observe two queries, let’s say “obama approval ratings” and “obama popularity rate.” Those two queries are different strings, but I think we can all agree that they’re basically searching for the same thing, and should be considered equivalent when counting queries, showing results, etc. How can we detect that two queries are synonymous?</div>
<div>
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; letter-spacing: -0.004em;"><br /></span></div>
<div>
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; letter-spacing: -0.004em;">To make this concrete, here is a sample input to illustrate:</span></div>
SYNONYMS = [<br />
('rate', 'ratings'),<br />
('approval', 'popularity'),<br />
]<br />
<br />
QUERIES = [<br />
('obama approval rate', 'obama popularity ratings'),<br />
('obama approval rates', 'obama popularity ratings'),<br />
('obama approval rate', 'popularity ratings obama')<br />
]<br />
<br />
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="c0d2" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="gl hc" style="box-sizing: inherit; font-weight: 700;">Part 1: The (Not So) Simple Case</span></div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="224d" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
However candidates arrive at these questions, they inevitably end up asking me for an answer, and I always start off with the simplest case possible: words can have multiple synonyms, order matters, synonyms are not transitive, and synonyms can only map from one word to another. This makes for a pretty limited feature in a search engine, but there’s more than enough subtlety in it to make for an interested interview question.</div>
<div>
<br /></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="16b2" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
You’d think, but there’s more subtlety here than you’d see at first glance. By far the trickiest component of this simple algorithm is the synonym comparison. While simple to understand and describe, there are a lot of ways the synonym comparison component can go wrong. I’ll go into some of the more common ones I’ve seen here.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="db48" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
To be clear, none of these mistakes are disqualifying in my mind; if the candidate produced an implementation with an error, I would simply point it out, they would adjust their solution, and we would move on. However, an interview is, first and foremost, a battle against time. Making, noticing, and correcting mistakes is expected, but it saps time that could be spent elsewhere, such as producing a more optimal solution. Very few candidates make no mistakes, but candidates who make fewer make more progress simply because they spend less time cleaning up after themselves.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="df22" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
This is why I like this problem: unlike the knight’s dialer, which requires a flash of algorithmic insight followed by a (hopefully) simple implementation, this question requires a multitude of small, incremental steps in the right direction. Each step represents a tiny hurdle over which the candidate can either leap gracefully or trip and have to recover. Good candidates avoid these little pitfalls using their experience and intuition and are rewarded with a more fleshed-out and correct solution, while weaker ones waste time and energy on mistakes and are usually left with buggy code.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="2727" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
While every interview saw a different mix of leaps and faceplants, here is a small sampling of the more common errors I saw.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="6ea0" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<em class="hb" style="box-sizing: inherit;">Accidental Runtime Killers</em></div>
</div>
<div>
<em class="hb" style="box-sizing: inherit;"></em><br />
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="827a" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: normal; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<em class="hb" style="box-sizing: inherit;">In terms of practical advice, this is actually an easy mistake to avoid. First off, never forget the types of your objects, even if you’re using an untyped language like python! Second, remember that when you use the <code class="gd hn ho hp hq b" style="background-color: rgba(0, 0, 0, 0.05); box-sizing: inherit; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 15.75px; padding: 2px 4px;">in</code> keyword on a list, that’s a linear search. Unless that list is guaranteed to always be very small, it’s going to be a performance killer.</em></div>
<em class="hb" style="box-sizing: inherit;">
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="c1b4" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: normal; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
Reminding candidates that the input structure is a list is usually enough to rouse them. What happens after I give a hint is very informative. The better candidates immediately think to preprocess the synonyms somehow, which is a good start. However, that approach is not without its pitfalls…</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="ce7b" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: normal; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<em class="hb" style="box-sizing: inherit;">Use the Right Data Structure</em></div>
<div>
<em class="hb" style="box-sizing: inherit;"></em><br />
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="9695" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: normal; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<em class="hb" style="box-sizing: inherit;">Which of these two the candidate chooses is less interesting to me than what they put in it. (By the way, never use a dict/hashmap that goes to <code class="gd hn ho hp hq b" style="background-color: rgba(0, 0, 0, 0.05); box-sizing: inherit; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 15.75px; padding: 2px 4px;">True</code> or <code class="gd hn ho hp hq b" style="background-color: rgba(0, 0, 0, 0.05); box-sizing: inherit; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 15.75px; padding: 2px 4px;">False</code>. That’s called a set.) Most candidates settle on some sort of dict/hashmap. The most common error I see is a subconscious assumption that each word can have at most one synonym:</em></div>
<em class="hb" style="box-sizing: inherit;">
<figure class="fn fo fp fq fr fs" style="background-color: white; box-sizing: inherit; clear: both; color: rgba(0, 0, 0, 0.8); font-family: medium-content-sans-serif-font, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; font-style: normal; margin: 56px 0px 0px;"><div class="gc r fv" style="box-sizing: inherit; margin: auto; position: relative;">
</div>
</figure></em></div>
<div>
<em class="hb" style="box-sizing: inherit;"></em><br />
<div>
<em class="hb" style="box-sizing: inherit;">A slightly more serious problem is not realizing that the synonym relationship goes both ways. You’ll notice the above code does this. Correcting this, however, can be error prone. Consider the following approach to implementing this property:</em></div>
<em class="hb" style="box-sizing: inherit;">
<div>
<br /></div>
<div>
Why perform two insertions and use double the memory when you can use no additional memory and perform two checks?</div>
<div>
<br /></div>
<div>
The takeaway: always ask yourself if you can do less work! In hindsight, permuting the lookup is an obvious way of saving time if you look for it, but using a suboptimal implementation suggests the candidate didn’t think to look for ways to optimize. Again, I’m happy to give a hint, but it’d be better if I didn’t have to.</div>
<div>
<br /></div>
</em></div>
<div>
<em class="hb" style="box-sizing: inherit;"></em><br />
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="fb49" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: normal; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<em class="hb" style="box-sizing: inherit;"><span class="gl hc" style="box-sizing: inherit; font-weight: 700;">Transitivity: Naive Approaches</span></em></div>
<em class="hb" style="box-sizing: inherit;">
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="baeb" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: normal; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
The first constraint I like to relax is the one around transitivity, meaning that if words <em class="hb" style="box-sizing: inherit;">A</em> and <em class="hb" style="box-sizing: inherit;">B</em> are synonymous, and if words <em class="hb" style="box-sizing: inherit;">B</em> and <em class="hb" style="box-sizing: inherit;">C</em> are synonymous, then words <em class="hb" style="box-sizing: inherit;">A</em> and <em class="hb" style="box-sizing: inherit;">C</em> are synonymous. Sharp candidates quickly realize they can adapt their earlier solution to solve this because they’re still determining whether simple pairs of words represent synonym pairs, whereas the other relaxations invalidate the core logic of the earlier algorithm.</div>
<div>
<br /></div>
</em></div>
</em></div>
<div>
<em class="hb" style="box-sizing: inherit;"><span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; font-style: normal; letter-spacing: -0.084px;">So far, so good, but there’s clearly no blinding performance just yet. The genius of this structure is in a procedure called </span><em class="hb" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.084px;">compaction</em><span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; font-style: normal; letter-spacing: -0.084px;">.</span></em></div>
<div>
<em class="hb" style="box-sizing: inherit;"><br /></em></div>
<div>
<em class="hb" style="box-sizing: inherit;"><br /></em></div>
<div>
<em class="hb" style="box-sizing: inherit;"><span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; font-style: normal; letter-spacing: -0.084px;">Yes you can, it turns out. In a way, every element in this tree is destined to arrive at “fast.” Instead of traversing the tree multiple times, why not simply change the parent of each element along the way to “fast” and save ourselves the work? This process is called compaction</span></em></div>
<div>
<em class="hb" style="box-sizing: inherit;"><br /></em></div>
<div>
<em class="hb" style="box-sizing: inherit;"><span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; font-style: normal; letter-spacing: -0.084px;">This leaves us with a few more follow-ups: a version of this question where word order doesn’t matter, and one where synonyms can span multiple words. The solutions to each of these are challenging and delightful.</span></em></div>
<div>
<em class="hb" style="box-sizing: inherit;"><br /></em></div>
<div>
<em class="hb" style="box-sizing: inherit;"><br /></em></div>
<div>
<em class="hb" style="box-sizing: inherit;"><a href="https://medium.com/@alexgolec/google-interview-problems-ratio-finder-d7aa8bf201e3" target="_blank">Google Interview Problems: Ratio Finder</a></em></div>
<div>
More at <a href="https://massivealgorithms.blogspot.com/2016/09/leetcode-399-evaluate-division.html" target="_blank">LeetCode 399 - Evaluate Division</a></div>
<div>
<div class="gj gk dc hb gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="487c" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: italic; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="bk" style="box-sizing: inherit; font-style: normal;">Given a list of conversion rates (formatted in the language of your choice) as a collection of origin unit, destination unit, and multiplier, for example:</span></div>
<div class="gj gk dc hb gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="9323" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: italic; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="bk" style="box-sizing: inherit; font-style: normal;">foot inch 12</span></div>
<div class="gj gk dc hb gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="0129" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: italic; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="bk" style="box-sizing: inherit; font-style: normal;">foot yard 0.3333333</span></div>
<div class="gj gk dc hb gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="5355" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: italic; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="bk" style="box-sizing: inherit; font-style: normal;">etc…</span></div>
<div class="gj gk dc hb gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="8626" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; font-style: italic; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="bk" style="box-sizing: inherit; font-style: normal;">Such that ORIGIN * MULTIPLIER = DESTINATION, design an algorithm that takes two arbitrary unit values and returns the conversion rate between them.</span></div>
</div>
<div>
<br /></div>
<div>
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; letter-spacing: -0.084px;">For context, framing is the act of translating a problem where the solution is not obvious into an equivalent one where the solution yields naturally. If that sounds completely abstract and unapproachable, forgive me because it is</span></div>
<div>
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; letter-spacing: -0.084px;"><br /></span></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="fcb3" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
For instance, <em class="hb" style="box-sizing: inherit;">what if there is no conversion?</em> The obvious approach doesn’t tell you anything about whether there actually is a conversion, and if I were given a thousand conversion rates, I would have a very difficult time determining whether such a conversion exists. Perhaps I’m being asked to convert between unfamiliar (or made up) units called <em class="hb" style="box-sizing: inherit;">wobbles</em> and a <em class="hb" style="box-sizing: inherit;">thingles,</em> and I have no idea where to even start. How would the intuitive approach handle that?</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="ee76" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
I have to admit, that’s kind of a contrived scenario, but there’s another, more realistic one to consider. You’ll notice that my problem statement only includes units of distance. This is very intentional. What if I ask my system to translate from inches to kilogram? You and I both know this can’t be done because those units measure different things, but our input tells us nothing about the “kind” of thing each unit measures.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="c8fd" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
This is where the careful statement of the question allows strong candidates to shine. Strong candidates think through the edge cases of a system <em class="hb" style="box-sizing: inherit;">before</em> they design an algorithm, and this problem statement purposefully gives them an opportunity to ask me whether we’ll be translating different units. It’s not a huge deal if they don’t catch this issue early on, but it’s always a good sign when someone asks me “what should I return if there is no conversion?” Stating the question this way gives me an indication.</div>
</div>
<div>
<br /></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="7a1a" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<em class="hb" style="box-sizing: inherit;">The Graph Framing</em></div>
</div>
<div>
<em class="hb" style="box-sizing: inherit;"><br /></em></div>
<div>
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; letter-spacing: -0.084px;">Framing the problem as a graph unlocks all the classic graph search problems. In particular, two algorithms are useful here: breadth first search (BFS) and depth first search (DFS). </span></div>
<div>
<br /></div>
<div>
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; letter-spacing: -0.084px;">We don’t want to find whether a path exists, we want to find the conversion rate! This is where the candidate must make a leap: it turns out you can modify any search algorithm to find the conversion rate, simply by keeping additional state as you traverse.</span></div>
<div>
<br /></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="893f" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
This is a fine implementation, but it suffers from two major weaknesses. First off, it is recursive. If it turns out the path we need is more than a thousand or so hops long, we’ll crash. Sure, it’s not likely, but if there’s one thing you don’t want happening in a long-running service, it’s crashing. Second off, even if we were to stop successfully, our answer has some undesirable properties.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="d16b" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
I actually already gave you a hint way up at the top of the post. Did you notice how Google says the conversion rate is <code class="gd hq hr hs ht b" style="background-color: rgba(0, 0, 0, 0.05); box-sizing: inherit; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 15.75px; padding: 2px 4px;">1.0739e-17</code> but the conversion I computed manually came out to <code class="gd hq hr hs ht b" style="background-color: rgba(0, 0, 0, 0.05); box-sizing: inherit; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 15.75px; padding: 2px 4px;">1.073<span class="gl hc" style="box-sizing: inherit; font-family: inherit; font-weight: 700;">7</span>e-17</code>? It turns out given all the floating point multiplications we’re performing, we have to start worrying about error propagation. The subtleties are a little more than I want to go into for this post, but the gist of it is we want to perform as few floating point multiplications as possible to avoid errors accumulating and causing trouble.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="8a62" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
DFS is a fine search algorithm, and if a solution exists it will find it, but it lacks a crucial property: it does not necessarily find the shortest path. This is relevant to us because a shorter path means fewer hops, which means fewer error-propagating floating point multiplications. To get around this, we’ll want to use BFS.</div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="background-color: white; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.084px;">the recursive DFS solution’s major weaknesses are that it’s recursive and it doesn’t minimize the number of multiplications. BFS,as we’ll soon see, does minimize the number of multiplications, and it also happens to be very tricky to implement recursively.</span></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<ul class="" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.8); font-family: medium-content-sans-serif-font, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; list-style: none none; margin: 0px; padding: 0px;">
<li class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw hf hg hh" data-selectable-paragraph="" id="e2c2" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2em; padding-left: 0px;">Understand the question</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="d417" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">Frame the conversion network as a graph</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="d641" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">Realize conversion rates can be mapped to paths through the graph</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="3f80" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">Recognize they can use search algorithms to accomplish this</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="f1c2" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">Choose their favorite algorithm and modify it to track the conversion rate</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="795f" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">If they implemented DFS as a naive solution, realize its weaknesses</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="f1e2" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">Implement BFS</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="118b" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">Step back and examine the edge cases:</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="1537" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">What if we’re asked for a nonexistent node?</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="46bd" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">What if the conversion rate does not exist?</li>
<li class="gj gk dc bk gl b gm hi go hj gq hk gs hl gu hm gw hf hg hh" data-selectable-paragraph="" id="1f53" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">Realize the reversing the conversions is possible and likely necessary</li>
</ul>
</div>
<div>
<br /></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="4412" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="gl hc" style="box-sizing: inherit; font-weight: 700;">Part 4: Can You Do Any Better?</span></div>
<div>
<span class="gl hc" style="box-sizing: inherit; font-weight: 700;"><br /></span></div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="8db9" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="gl hc" style="box-sizing: inherit; font-weight: 700;">Part 4: Constant Time</span></div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="2fd9" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
So it turns out that the “cache everything” solution is actually not far off from the mark. In that approach, we (eventually) end up with an edge between each node and every other node, meaning our conversion happen in a single edge lookup. But do we really need to store conversion <em class="hb" style="box-sizing: inherit;">from</em> every node <em class="hb" style="box-sizing: inherit;">to</em> every node? What if we just stored the conversion rates from <em class="hb" style="box-sizing: inherit;">one </em>node to every other node?</div>
<br />
<br />
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="febb" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="gl hc" style="box-sizing: inherit; font-weight: 700;">But Wait, There’s More!</span></div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="30d1" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span style="letter-spacing: -0.004em;">First, a warm-up: in the constant time solution I laid out, I chose the root node of each connected component arbitrarily. In particular, I use the first node of that component we encounter. This is not optimal, because for all we know we’ve chosen some node way off on the fringes of the graph, while some other node might be more central and so have shorter paths to all other nodes. Your assignment is to replace this arbitrary choice with one which minimizes the number of multiplications required and this the floating point error propagation.</span></div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="ff4c" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
Second, this whole discussion assumes all equal-length paths through the graph are created equal, which isn’t always the case. One interesting variant of this problem is currency conversions: nodes are currencies, and edges from <code class="gd hq hr hs ht b" style="background-color: rgba(0, 0, 0, 0.05); box-sizing: inherit; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 15.75px; padding: 2px 4px;">A</code> to <code class="gd hq hr hs ht b" style="background-color: rgba(0, 0, 0, 0.05); box-sizing: inherit; font-family: Menlo, Monaco, "Courier New", Courier, monospace; font-size: 15.75px; padding: 2px 4px;">B</code> and vice-versa are the bid/ask prices of each currency pair. We can rephrase the unit conversion question as a forex arbitrage question: implement an algorithm that, given a currency conversion graph, computes a cycle through the graph that can leave a trader with more money than when they started. Assume no transaction fees.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="d81a" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
Finally, a real doozie: some units are expressed as a combination of various basic units. For instance, the watt is defined, in SI units, as “kilogram meters squared by seconds cubed”. The final challenge is to extend this system to support converting between these units given only the definitions of the basic SI units.</div>
</div>
<div>
<br /></div>
<div id="gtx-trans" style="left: 372px; position: absolute; top: 7485.56px;">
<div class="gtx-trans-icon">
</div>
</div>
Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-81193986667431210132019-12-27T12:02:00.001-08:002019-12-27T15:11:36.075-08:00How to Conduct Algorithm interview<a href="https://medium.com/@alexgolec/introducing-google-interview-questions-deconstructed-a012e41ea631">https://medium.com/@alexgolec/introducing-google-interview-questions-deconstructed-a012e41ea631</a><br />
<div class="gh gi co at gj b gk gl gm gn go gp gq gr gs gt gu" data-selectable-paragraph="" id="a143" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
After each interview is done, the interviewer writes up feedback. For entry-level technical roles, this feedback is only on your technical abilities: <mark class="nm nn iq" style="background-color: rgba(12, 242, 143, 0.2); box-sizing: inherit; color: currentcolor; cursor: pointer;">How well did you apply your data structures and algorithms knowledge? How well do you code? Did your solution handle all the edge cases? How close did your solution get to optimal time and space complexity for the problem? How well did you communicate your thoughts and respond to questions?</mark></div>
<div>
<mark class="nm nn iq" style="background-color: rgba(12, 242, 143, 0.2); box-sizing: inherit; color: currentcolor; cursor: pointer;"><br /></mark></div>
<br />
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; letter-spacing: -0.084px;">When I interview, I only care about your ability to solve problems and communicate as you do it. In other words, don’t spend too much time worrying about the your outfit or the strength of your handshake.</span><br />
<br />
<a href="https://hackernoon.com/google-interview-questions-deconstructed-the-knights-dialer-f780d516f029">https://hackernoon.com/google-interview-questions-deconstructed-the-knights-dialer-f780d516f029</a><br />
<div style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-size: 20px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.6; margin-bottom: 35px; margin-top: 35px; padding: 0px; position: relative; vertical-align: baseline;">
Every interview I conduct basically breaks down into two parts: first we find an algorithmic solution and then the candidate implements it in code. I say “we” find a solution because I’m not a mute spectator: 45 minutes is not a lot of time to design and implement anything under the best circumstances, never mind under pressure. I let candidates take the lead in the discussion, generating ideas, solving instances of the problem, etc., but I’m more than happy to give a nudge in the right direction. The better the candidate, the fewer hints I tend to have to give, but I have yet to see a candidate who required no input from me at all.</div>
<div style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-size: 20px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.6; margin-bottom: 35px; margin-top: 35px; padding: 0px; position: relative; vertical-align: baseline;">
I should underscore this, because it’s important: as an interviewer, I’m not in the business of sitting back and watching people fail. I want to write as much positive feedback as I can, and I try to give you opportunities to allow me to write good things about you. Hints are my way of saying “okay, I’m gonna give this bit to you, but only so you can move on and show me what you’ve got on the other parts of the question.”</div>
<div>
<div style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-size: 20px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.6; margin-bottom: 35px; margin-top: 35px; padding: 0px; position: relative; vertical-align: baseline;">
With that being said, your first action after hearing the question should be stepping up to the whiteboard and solving small instances of the problem by hand. <em style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: inherit; font-stretch: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Never dive right into code!</em> Solving small instances lets you spot patterns, observed and edge cases, and also helps crystallize a solution in your head. As an example, suppose you start on 6 and have two hops to make. Your sequences will be…</div>
<ul style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; list-style-image: initial; list-style-position: initial; margin: 0px; padding: 0px 0px 0px 40px; vertical-align: baseline;"></ul>
</div>
<div style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-size: 20px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.6; margin-bottom: 35px; margin-top: 35px; padding: 0px; position: relative; vertical-align: baseline;">
One of the surprises I had when I started using this problem is how often candidates get stuck on computing the keys to which we can hop from a given position, also known as the neighbors. My advice is: when in doubt, write an empty placeholder and ask the interviewer if you can implement it later. This problem’s complexity does not lie in the neighbor computation; I’m paying attention to how well you count full numbers. Any time spent on neighbor computation is effectively wasted.</div>
<div style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-size: 20px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.6; margin-bottom: 35px; margin-top: 35px; padding: 0px; position: relative; vertical-align: baseline;">
I would accept “let’s assume there’s a function that gives me the neighbors” along with the following stub. Of course, I’ll probably ask you to double back and implement this later, but only if we have time. You can simply write a stub like this and move on:</div>
<pre class="graf graf--pre graf-after--p" id="21cb" name="21cb" style="background-color: #eeeeee; border: 0px; box-sizing: border-box; color: #111111; display: inline; font-size: inherit; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; padding: 3px; vertical-align: baseline;">def neighbors(position):
...</pre>
<div style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-size: 20px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: 1.6; margin-bottom: 35px; margin-top: 35px; padding: 0px; position: relative; vertical-align: baseline;">
Also, you don’t really lose much by asking to use a stub: if the question’s complexity is elsewhere I’ll allow it. If not, I’ll ask you to actually implement it. I don’t mind when candidates don’t realize where the complexity of a question lies, especially in the early stages when they might not have fully explored the problem.</div>
<br />
<br />
<br />
<figure style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; text-align: center; vertical-align: baseline;"><div class="aspectRatioPlaceholder is-locked" style="border: 0px; box-sizing: border-box; font: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</div>
</figure><br />
<div style="-webkit-text-stroke-width: 0px; border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-size: 20px; font-stretch: inherit; font-style: normal; font-variant-caps: normal; font-variant-east-asian: inherit; font-variant-ligatures: normal; font-variant-numeric: inherit; font-weight: 400; letter-spacing: normal; line-height: 1.6; margin: 35px 0px; orphans: 2; padding: 0px; position: relative; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; vertical-align: baseline; white-space: normal; widows: 2; word-spacing: 0px;">
As for the neighbors function here, given that it never changes you can simply create a map and return the appropriate value:</div>
<ul style="border: 0px; box-sizing: border-box; color: #111111; font-family: "IBM Plex Sans", sans-serif; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; list-style-image: initial; list-style-position: initial; margin: 0px; padding: 0px 0px 0px 40px; vertical-align: baseline;">
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: 20px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.6; margin: 35px 0px; padding: 0px; position: relative; vertical-align: baseline;"><strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Always start by solving a small instance of the problem by hand</strong>. In this problem the recurrence relation and the repetition of function calls become much more obvious when you hand-solve a problem.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: 20px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.6; margin: 35px 0px; padding: 0px; position: relative; vertical-align: baseline;"><strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Pay attention to when your solution is computing things you don’t need</strong>, like how the naive counting solution generates the sequences but doesn’t actually use them. Reducing unnecessary computation can often provide simpler solutions, if not open the door to more efficient ones.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: 20px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.6; margin: 35px 0px; padding: 0px; position: relative; vertical-align: baseline;"><strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Know your recursion. </strong>It’s almost useless in most production code because it blasts through the stack, but it’s a very powerful algorithm design tactic. Recursive solutions can often be adapted and improved: the difference between the exponential time naive solution and the linear time nearly-optimal memoization solution is minimal.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: 20px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.6; margin: 35px 0px; padding: 0px; position: relative; vertical-align: baseline;"><strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Know your Big-O analysis!</strong> You’re practically guaranteed to be asked this at some point during the interview process.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: 20px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.6; margin: 35px 0px; padding: 0px; position: relative; vertical-align: baseline;"><strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Always be on the lookout for opportunities to memoize.</strong> If your function is deterministic and you’ll be calling it multiple times with the same inputs, your solution may benefit from memoization.</li>
<li style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: 20px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 1.6; margin: 35px 0px; padding: 0px; position: relative; vertical-align: baseline;"><strong style="border: 0px; box-sizing: border-box; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Find and write out the recurrence relation.</strong> In this case writing it out makes it obvious that counts for N hops depend only on counts for N-1 hops.</li>
</ul>
<a href="https://medium.com/@alexgolec/google-interview-problems-synonymous-queries-36425145387c" target="_blank">Google Interview Questions Deconstructed: Synonymous Queries</a><br />
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="ceb1" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<span class="gl hc" style="box-sizing: inherit; font-weight: 700;">Questions, Questions</span></div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="270e" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
On the face of it, this is a simple problem. However, the closer you look, the more complex it becomes. Right off the bat, it becomes clear this problem is pretty under-defined. Can words have multiple synonyms? Does word order matter? Are synonym relationships transitive, meaning if <em class="hb" style="box-sizing: inherit;">A</em> is synonymous with <em class="hb" style="box-sizing: inherit;">B</em>, and <em class="hb" style="box-sizing: inherit;">B</em> is synonymous with <em class="hb" style="box-sizing: inherit;">C</em>, does that mean <em class="hb" style="box-sizing: inherit;">A</em> is synonymous with <em class="hb" style="box-sizing: inherit;">C</em>? Can synonyms span multiple words, such as how “USA” is synonymous with “United States of America” or “United States”?</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="ada3" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
This ambiguity immediately gives good candidates an opportunity to set themselves apart. The first thing a good candidate does is sniff out these sorts of ambiguities and try to resolve them. How they do this varies from candidate to candidate: some step up to the whiteboard to try to manually solve specific cases, whereas some take one look at the question and see the gaps immediately. In any case, spotting these issues early is crucial.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="9bd7" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
I place a lot of importance on this question’s “problem understanding” phase. Software engineering is what I like to call a fractal discipline, meaning it shares a quality with fractals where zooming in reveals additional complexity. Just when you think you understand a problem, you look closer and realize there’s a subtlety you overlooked, or there’s an implementation detail you can improve on, or there’s a new way of framing the problem that reveals additional insight.</div>
<br />
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="be66" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
An engineer’s caliber is largely determined by how deeply they can understand a problem. Transforming a vague problem statement into a detailed set of requirements is step zero in this process, and purposefully under-defining the problem allows me to assess how well a candidate approaches novel situations.</div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.84); font-family: , "georgia" , "cambria" , "times new roman" , "times" , serif; font-size: 21px; letter-spacing: -0.004em;">An engineer’s caliber is largely determined by how deeply they can understand a problem. Transforming a vague problem statement into a detailed set of requirements is step zero in this process, and purposefully under-defining the problem allows me to assess how well a candidate approaches novel situations.</span></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="afc4" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<em class="hb" style="box-sizing: inherit;">As an aside, there are also the trivial questions like “does capitalization matter?” that, unbeknownst to the candidate, don’t affect the core algorithmic problem. For these questions, I always give whatever answer is easiest on the candidate (in this case, “assume everything is already preprocessed to be lowercase”).</em></div>
</div>
<div>
<em class="hb" style="box-sizing: inherit;"><br /></em></div>
<div>
<a href="https://medium.com/@alexgolec/google-interview-problems-synonymous-queries-36425145387c" target="_blank">Google Interview Questions Deconstructed: Synonymous Queries</a><br />
<div>
</div>
</div>
<div>
<ul class="" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.8); font-family: medium-content-sans-serif-font, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; list-style: none none; margin: 0px; padding: 0px;">
<li class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw hr hs ht" data-selectable-paragraph="" id="672d" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 2em; padding-left: 0px;"><em class="hb" style="box-sizing: inherit;">Analyze a problem statement</em> and <em class="hb" style="box-sizing: inherit;">recognize where it is ambiguous and under-defined</em>, clarifying where necessary to develop an unambiguous problem statement. Continue doing this as you progress through a solution and encounter new questions. For maximal efficiency, do as much of this stage as early as possible because recovering from errors becomes more costly as your work progresses.</li>
<li class="gj gk dc bk gl b gm hu go hv gq hw gs hx gu hy gw hr hs ht" data-selectable-paragraph="" id="271e" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;"><em class="hb" style="box-sizing: inherit;">Frame the problem</em> in such a way that makes it easier to approach and solve. In this case, the most important point is the observation that you can line up corresponding words in the queries.</li>
<li class="gj gk dc bk gl b gm hu go hv gq hw gs hx gu hy gw hr hs ht" data-selectable-paragraph="" id="841b" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;"><em class="hb" style="box-sizing: inherit;">Implement your solution</em>. This involves choosing optimal data structure and algorithms, as well as designing your logic to be readable and easy to modify in the future.</li>
<li class="gj gk dc bk gl b gm hu go hv gq hw gs hx gu hy gw hr hs ht" data-selectable-paragraph="" id="b50d" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">Circle back and <em class="hb" style="box-sizing: inherit;">try to spot bugs and errors</em>. These could be actual bugs like how I forgot to insert a “continue” statement above, or performance bugs like using an incorrect data structure.</li>
<li class="gj gk dc bk gl b gm hu go hv gq hw gs hx gu hy gw hr hs ht" data-selectable-paragraph="" id="7f95" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;">When the problem definition changes, <em class="hb" style="box-sizing: inherit;">repeat this process and adapt your solution whenever appropriate</em>, and scrap it where not. Knowing when to do both is a critical skill, both in an interview and in the real world.</li>
<li class="gj gk dc bk gl b gm hu go hv gq hw gs hx gu hy gw hr hs ht" data-selectable-paragraph="" id="847d" style="box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; list-style-type: disc; margin-bottom: -0.46em; margin-left: 30px; margin-top: 1.05em; padding-left: 0px;"><mark class="nh ni iw" style="background-color: rgba(12, 242, 143, 0.2); box-sizing: inherit; color: currentcolor; cursor: pointer;"><em class="hb" style="box-sizing: inherit;">Keep your data structures and algorithms knowledge at your fingertips</em></mark><mark class="nh ni iw" style="background-color: rgba(12, 242, 143, 0.2); box-sizing: inherit; color: currentcolor; cursor: pointer;">. The disjoint set data structure is not exactly a common structure, but it’s not really that rare and refined either. The only way you can ensure you know the tool for the job is by learning as much as you can.</mark></li>
<li></li>
</ul>
</div>
<div>
<br /></div>
<div>
<em class="hb" style="box-sizing: inherit;"><a href="https://medium.com/@alexgolec/google-interview-problems-ratio-finder-d7aa8bf201e3" target="_blank">Google Interview Problems: Ratio Finder</a></em></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="c5c8" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
<mark class="nl nm ja" style="background-color: rgba(12, 242, 143, 0.2); box-sizing: inherit; color: currentcolor; cursor: pointer;">However, I </mark><mark class="nl nm ja" style="background-color: rgba(12, 242, 143, 0.2); box-sizing: inherit; color: currentcolor; cursor: pointer;"><em class="hb" style="box-sizing: inherit;">can</em></mark><mark class="nl nm ja" style="background-color: rgba(12, 242, 143, 0.2); box-sizing: inherit; color: currentcolor; cursor: pointer;"> remember many who were not sufficiently prepared for the interview, or worked at too slow a pace, or required too much supervision to solve a problem, or communicated in an unclear way, or failed to translate their ideas into code, or had an attitude that simply wasn’t going to lead them to success in the long term, etc. The definition of “worth hiring” is squishy and varies by company, and the interview process is there to see how well each candidate fits into a given company’s definition.</mark></div>
</div>
<div>
<mark class="nl nm ja" style="background-color: rgba(12, 242, 143, 0.2); box-sizing: inherit; color: currentcolor; cursor: pointer;"><br /></mark></div>
<div>
<br /></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="bb72" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
This bears repeating: when in doubt, write out a naive solution! Even if it’s not fully optimal, having code on the board is an accomplishment, and you can often iterate on it to get to a proper solution. Another way of saying this is: never let work go to waste. Odds are you thought of a naive solution but didn’t want to throw it out there because you know it’s not optimal. If you have a better solution on the tip of your tongue, that’s fine, but if not make sure you bank the progress you’ve made before you move on to more sophisticated things.</div>
</div>
<div>
<br /></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="8c60" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
If we break solving this problem into four hurdles (framing discussion, algorithm choice, implementation, constant time discussion), almost all candidates made it as far as “algorithm choice” by the end of the interview. As I suspected, the framing discussion turned out to be a good separator: candidates either got it immediately or struggled to grasp it despite substantial hints.</div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="17db" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
Algorithm choice, on the other hand, turned out not to be a particularly useful source of signal. People who got over the framing stage usually arrived at using this algorithm without much trouble. I suspect this comes from the fact that search algorithms are almost always taught alongside graphs themselves, so if someone is familiar with one they’re likely to know the other.</div>
<div class="gj gk dc bk gl b gm gn go gp gq gr gs gt gu gv gw" data-selectable-paragraph="" id="f150" style="background-color: white; box-sizing: inherit; color: rgba(0, 0, 0, 0.84); font-family: medium-content-serif-font, Georgia, Cambria, "Times New Roman", Times, serif; font-size: 21px; letter-spacing: -0.004em; line-height: 1.58; margin-bottom: -0.46em; margin-top: 2em;">
Implementation turned out to be tricky. Many people had no problems implementing DFS recursively, but as I mentioned above, that implementation is not going to cut it in a production setting. To my surprise, the iterative BFS and DFS implementations do not seem to be at the tips of peoples’ fingers, and I often found that even after I gave substantial hints, people often faltered.</div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-39434230985030137032019-12-13T22:53:00.001-08:002019-12-13T22:53:33.590-08:00LeetCode 1256 - Encode Number<a href="https://www.acwing.com/file_system/file/content/whole/index/content/150815/">https://www.acwing.com/file_system/file/content/whole/index/content/150815/</a><br />
<br />
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<br />
<div class="content__u3I1 question-content__JfgR" style="box-sizing: border-box; margin-top: 0px !important; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; widows: 2;">
<div style="box-sizing: border-box;">
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 0px 0px 16px; text-transform: none; white-space: normal; word-spacing: 0px;">
Given a non-negative integer<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">num</code>, Return its<span> </span><em style="box-sizing: border-box;">encoding</em><span> </span>string.</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 0px 0px 16px; text-transform: none; white-space: normal; word-spacing: 0px;">
The encoding is done by converting the integer to a string using a secret function that you should deduce from the following table:</div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 0px 0px 16px; text-transform: none; white-space: normal; word-spacing: 0px;">
<img alt="" src="https://assets.leetcode.com/uploads/2019/06/21/encode_number.png" style="background-color: white; border: 0px none; box-sizing: content-box; height: 360px; max-width: 100%; vertical-align: middle; width: 164px;" /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 0px 0px 16px; text-transform: none; white-space: normal; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 0px 0px 16px; text-transform: none; white-space: normal; word-spacing: 0px;">
<strong style="box-sizing: border-box; font-weight: 600;">Example 1:</strong></div>
<pre style="-webkit-text-stroke-width: 0px; background-color: #f6f8fa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; display: block; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.45; margin: 0px 0px 16px; overflow-wrap: normal; overflow: auto; padding: 16px; text-transform: none; white-space: normal; word-break: break-all; word-spacing: 0px;"><strong style="box-sizing: border-box; font-weight: 600;">Input:</strong> num = 23
<strong style="box-sizing: border-box; font-weight: 600;">Output:</strong> "1000"
</pre>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 0px 0px 16px; text-transform: none; white-space: normal; word-spacing: 0px;">
<strong style="box-sizing: border-box; font-weight: 600;">Example 2:</strong></div>
<pre style="-webkit-text-stroke-width: 0px; background-color: #f6f8fa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; display: block; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; line-height: 1.45; margin: 0px 0px 16px; overflow-wrap: normal; overflow: auto; padding: 16px; text-transform: none; white-space: normal; word-break: break-all; word-spacing: 0px;"><strong style="box-sizing: border-box; font-weight: 600;">Input:</strong> num = 107
<strong style="box-sizing: border-box; font-weight: 600;">Output:</strong> "101100"
</pre>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 0px 0px 16px; text-transform: none; white-space: normal; word-spacing: 0px;">
<br /></div>
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin: 0px 0px 16px; text-transform: none; white-space: normal; word-spacing: 0px;">
<strong style="box-sizing: border-box; font-weight: 600;">Constraints:</strong></div>
<ul style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-bottom: 16px; margin-top: 0px; padding-left: 2em; text-transform: none; white-space: normal; word-spacing: 0px;">
<li style="box-sizing: border-box;"><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">0 <= num <= 10^9</code></li>
</ul>
<a href="http://qiujiaqi.cn/leetcode/encode-number/">http://qiujiaqi.cn/leetcode/encode-number/</a></div>
<div style="box-sizing: border-box;">
<ul style="background-color: white; color: #555555; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 18px;">
<li><div class="has-jax" style="margin-bottom: 20px;">
这题一开始我的思路是,加密后的字符串长度 len 可以用 <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><msub><mi>g</mi><mn>2</mn></msub><mo stretchy="false">(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" style="border: 0px; direction: ltr; display: inline; float: none; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; overflow-wrap: normal; padding: 0px; position: relative; white-space: nowrap; word-spacing: normal;" tabindex="0"><nobr aria-hidden="true" style="border: 0px; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; transition: none 0s ease 0s; vertical-align: 0px;"><span class="math" id="MathJax-Span-1" style="border: 0px; box-sizing: content-box; display: inline-block; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 5.465em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; font-size: 21.6px; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 4.539em;"><span style="border: 0px; box-sizing: content-box; clip: rect(1.715em, 1004.49em, 2.873em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -2.544em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-2" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-3" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑙</span><span class="mi" id="MathJax-Span-4" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑜</span><span class="msubsup" id="MathJax-Span-5" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 0.928em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.382em, 1000.51em, 4.308em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.979em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-6" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑔</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; left: 0.512em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.84em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-7" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">2</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span><span class="mo" id="MathJax-Span-8" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">(</span><span class="mi" id="MathJax-Span-9" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span><span class="mo" id="MathJax-Span-10" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">+</span><span class="mn" id="MathJax-Span-11" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-12" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">)</span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 2.549em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-bottom-style: initial; border-color: initial; border-image: initial; border-left-style: solid; border-right-style: initial; border-top-style: initial; border-width: 0px; box-sizing: content-box; display: inline-block; height: 1.169em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: -0.275em; width: 0px;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation" style="border: 0px; box-sizing: content-box; clip: rect(1px, 1px, 1px, 1px); display: inline; height: 1px !important; left: 0px; line-height: normal; margin: 0px; overflow: hidden !important; padding: 0px; position: static; top: 0px; transition: none 0s ease 0s; user-select: none; vertical-align: 0px; width: 1px !important;"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><msub><mi>g</mi><mn>2</mn></msub><mo stretchy="false">(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></math></span></span> 计算,而尾值即可用 (n + 1) 对 最大的 2 的幂的数求余得出,即 <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mspace width="0.667em" /><mi>mod</mi><mspace width="thinmathspace" /><mspace width="thinmathspace" /><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mi>l</mi><mi>e</mi><mi>n</mi></mrow></msup></math>" id="MathJax-Element-2-Frame" role="presentation" style="border: 0px; direction: ltr; display: inline; float: none; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; overflow-wrap: normal; padding: 0px; position: relative; white-space: nowrap; word-spacing: normal;" tabindex="0"><nobr aria-hidden="true" style="border: 0px; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; transition: none 0s ease 0s; vertical-align: 0px;"><span class="math" id="MathJax-Span-13" style="border: 0px; box-sizing: content-box; display: inline-block; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 9.123em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; font-size: 21.6px; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 7.595em;"><span style="border: 0px; box-sizing: content-box; clip: rect(1.53em, 1007.6em, 2.873em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -2.544em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-14" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-15" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">(</span><span class="mi" id="MathJax-Span-16" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span><span class="mo" id="MathJax-Span-17" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">+</span><span class="mn" id="MathJax-Span-18" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-19" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">)</span><span class="TeXmathchoice" id="MathJax-Span-20" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mspace" id="MathJax-Span-21" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.743em;"></span></span><span class="mi" id="MathJax-Span-22" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.188em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">mod</span><span class="mspace" id="MathJax-Span-23" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.188em;"></span><span class="mspace" id="MathJax-Span-24" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.188em;"></span><span class="msubsup" id="MathJax-Span-25" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.188em; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 1.484em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.15em, 1000.47em, 4.123em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.979em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-26" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">2</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; left: 0.512em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -4.396em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="texatom" id="MathJax-Span-27" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-28" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-29" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑙</span><span class="mi" id="MathJax-Span-30" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑒</span><span class="mi" id="MathJax-Span-31" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 2.549em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-bottom-style: initial; border-color: initial; border-image: initial; border-left-style: solid; border-right-style: initial; border-top-style: initial; border-width: 0px; box-sizing: content-box; display: inline-block; height: 1.392em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: -0.275em; width: 0px;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation" style="border: 0px; box-sizing: content-box; clip: rect(1px, 1px, 1px, 1px); display: inline; height: 1px !important; left: 0px; line-height: normal; margin: 0px; overflow: hidden !important; padding: 0px; position: static; top: 0px; transition: none 0s ease 0s; user-select: none; vertical-align: 0px; width: 1px !important;"><math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">(</mo><mi>n</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mspace width="0.667em"></mspace><mi>mod</mi><mspace width="thinmathspace"></mspace><mspace width="thinmathspace"></mspace><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mi>l</mi><mi>e</mi><mi>n</mi></mrow></msup></math></span></span>,将这个尾值转化为二进制,再往这个尾值头部插入 0,直至长度为 len</div>
</li>
<li><div style="margin-bottom: 20px;">
例如:</div>
<div class="has-jax" style="margin-bottom: 20px;">
6,加密后长度为 <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><msub><mi>g</mi><mn>2</mn></msub><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">(</mo><mn>6</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></math>" id="MathJax-Element-3-Frame" role="presentation" style="border: 0px; direction: ltr; display: inline; float: none; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; overflow-wrap: normal; padding: 0px; position: relative; white-space: nowrap; word-spacing: normal;" tabindex="0"><nobr aria-hidden="true" style="border: 0px; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; transition: none 0s ease 0s; vertical-align: 0px;"><span class="math" id="MathJax-Span-32" style="border: 0px; box-sizing: content-box; display: inline-block; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 5.465em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; font-size: 21.6px; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 4.539em;"><span style="border: 0px; box-sizing: content-box; clip: rect(1.715em, 1004.49em, 2.873em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -2.544em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-33" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-34" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑙</span><span class="mi" id="MathJax-Span-35" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑜</span><span class="msubsup" id="MathJax-Span-36" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 0.928em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.382em, 1000.51em, 4.308em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.979em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-37" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑔</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; left: 0.512em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.84em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-38" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">2</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span><span class="texatom" id="MathJax-Span-39" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-40" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-41" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">(</span><span class="mn" id="MathJax-Span-42" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">6</span><span class="mo" id="MathJax-Span-43" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">+</span><span class="mn" id="MathJax-Span-44" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-45" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">)</span></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 2.549em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-bottom-style: initial; border-color: initial; border-image: initial; border-left-style: solid; border-right-style: initial; border-top-style: initial; border-width: 0px; box-sizing: content-box; display: inline-block; height: 1.169em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: -0.275em; width: 0px;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation" style="border: 0px; box-sizing: content-box; clip: rect(1px, 1px, 1px, 1px); display: inline; height: 1px !important; left: 0px; line-height: normal; margin: 0px; overflow: hidden !important; padding: 0px; position: static; top: 0px; transition: none 0s ease 0s; user-select: none; vertical-align: 0px; width: 1px !important;"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><msub><mi>g</mi><mn>2</mn></msub><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">(</mo><mn>6</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></math></span></span>,即 2,尾值为 <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">(</mo><mn>6</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mspace width="0.667em" /><mi>mod</mi><mspace width="thinmathspace" /><mspace width="thinmathspace" /><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mn>2</mn></mrow></msup></math>" id="MathJax-Element-4-Frame" role="presentation" style="border: 0px; direction: ltr; display: inline; float: none; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; overflow-wrap: normal; padding: 0px; position: relative; white-space: nowrap; word-spacing: normal;" tabindex="0"><nobr aria-hidden="true" style="border: 0px; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; transition: none 0s ease 0s; vertical-align: 0px;"><span class="math" id="MathJax-Span-46" style="border: 0px; box-sizing: content-box; display: inline-block; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 8.475em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; font-size: 21.6px; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 7.039em;"><span style="border: 0px; box-sizing: content-box; clip: rect(1.53em, 1007.04em, 2.873em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -2.544em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-47" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-48" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">(</span><span class="mn" id="MathJax-Span-49" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">6</span><span class="mo" id="MathJax-Span-50" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">+</span><span class="mn" id="MathJax-Span-51" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-52" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">)</span><span class="TeXmathchoice" id="MathJax-Span-53" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mspace" id="MathJax-Span-54" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.743em;"></span></span><span class="mi" id="MathJax-Span-55" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.188em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">mod</span><span class="mspace" id="MathJax-Span-56" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.188em;"></span><span class="mspace" id="MathJax-Span-57" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.188em;"></span><span class="msubsup" id="MathJax-Span-58" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.188em; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 0.928em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.15em, 1000.47em, 4.123em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.979em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-59" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">2</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; left: 0.512em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -4.396em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="texatom" id="MathJax-Span-60" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-61" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-62" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">2</span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 2.549em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-bottom-style: initial; border-color: initial; border-image: initial; border-left-style: solid; border-right-style: initial; border-top-style: initial; border-width: 0px; box-sizing: content-box; display: inline-block; height: 1.392em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: -0.275em; width: 0px;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation" style="border: 0px; box-sizing: content-box; clip: rect(1px, 1px, 1px, 1px); display: inline; height: 1px !important; left: 0px; line-height: normal; margin: 0px; overflow: hidden !important; padding: 0px; position: static; top: 0px; transition: none 0s ease 0s; user-select: none; vertical-align: 0px; width: 1px !important;"><math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">(</mo><mn>6</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mspace width="0.667em"></mspace><mi>mod</mi><mspace width="thinmathspace"></mspace><mspace width="thinmathspace"></mspace><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mn>2</mn></mrow></msup></math></span></span>,即为 11,长度已经足够不需补 0,得 11</div>
<div class="has-jax" style="margin-bottom: 20px;">
7,加密后长度为 <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><msub><mi>g</mi><mn>2</mn></msub><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">(</mo><mn>7</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></math>" id="MathJax-Element-5-Frame" role="presentation" style="border: 0px; direction: ltr; display: inline; float: none; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; overflow-wrap: normal; padding: 0px; position: relative; white-space: nowrap; word-spacing: normal;" tabindex="0"><nobr aria-hidden="true" style="border: 0px; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; transition: none 0s ease 0s; vertical-align: 0px;"><span class="math" id="MathJax-Span-63" style="border: 0px; box-sizing: content-box; display: inline-block; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 5.465em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; font-size: 21.6px; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 4.539em;"><span style="border: 0px; box-sizing: content-box; clip: rect(1.715em, 1004.49em, 2.873em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -2.544em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-64" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-65" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑙</span><span class="mi" id="MathJax-Span-66" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑜</span><span class="msubsup" id="MathJax-Span-67" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 0.928em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.382em, 1000.51em, 4.308em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.979em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-68" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑔</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; left: 0.512em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.84em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-69" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">2</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span><span class="texatom" id="MathJax-Span-70" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-71" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-72" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">(</span><span class="mn" id="MathJax-Span-73" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">7</span><span class="mo" id="MathJax-Span-74" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">+</span><span class="mn" id="MathJax-Span-75" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-76" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">)</span></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 2.549em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-bottom-style: initial; border-color: initial; border-image: initial; border-left-style: solid; border-right-style: initial; border-top-style: initial; border-width: 0px; box-sizing: content-box; display: inline-block; height: 1.169em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: -0.275em; width: 0px;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation" style="border: 0px; box-sizing: content-box; clip: rect(1px, 1px, 1px, 1px); display: inline; height: 1px !important; left: 0px; line-height: normal; margin: 0px; overflow: hidden !important; padding: 0px; position: static; top: 0px; transition: none 0s ease 0s; user-select: none; vertical-align: 0px; width: 1px !important;"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><msub><mi>g</mi><mn>2</mn></msub><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">(</mo><mn>7</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></math></span></span>,即 3,尾值为 <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">(</mo><mn>7</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mspace width="0.667em" /><mi>mod</mi><mspace width="thinmathspace" /><mspace width="thinmathspace" /><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mn>3</mn></mrow></msup></math>" id="MathJax-Element-6-Frame" role="presentation" style="border: 0px; direction: ltr; display: inline; float: none; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; overflow-wrap: normal; padding: 0px; position: relative; white-space: nowrap; word-spacing: normal;" tabindex="0"><nobr aria-hidden="true" style="border: 0px; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; transition: none 0s ease 0s; vertical-align: 0px;"><span class="math" id="MathJax-Span-77" style="border: 0px; box-sizing: content-box; display: inline-block; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 8.475em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; font-size: 21.6px; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 7.039em;"><span style="border: 0px; box-sizing: content-box; clip: rect(1.53em, 1007.04em, 2.873em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -2.544em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-78" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-79" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">(</span><span class="mn" id="MathJax-Span-80" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">7</span><span class="mo" id="MathJax-Span-81" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">+</span><span class="mn" id="MathJax-Span-82" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-83" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">)</span><span class="TeXmathchoice" id="MathJax-Span-84" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mspace" id="MathJax-Span-85" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.743em;"></span></span><span class="mi" id="MathJax-Span-86" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.188em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">mod</span><span class="mspace" id="MathJax-Span-87" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.188em;"></span><span class="mspace" id="MathJax-Span-88" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.188em;"></span><span class="msubsup" id="MathJax-Span-89" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.188em; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 0.928em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.15em, 1000.47em, 4.123em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.979em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-90" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">2</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; left: 0.512em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -4.396em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="texatom" id="MathJax-Span-91" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-92" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-93" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">3</span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 2.549em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-bottom-style: initial; border-color: initial; border-image: initial; border-left-style: solid; border-right-style: initial; border-top-style: initial; border-width: 0px; box-sizing: content-box; display: inline-block; height: 1.392em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: -0.275em; width: 0px;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation" style="border: 0px; box-sizing: content-box; clip: rect(1px, 1px, 1px, 1px); display: inline; height: 1px !important; left: 0px; line-height: normal; margin: 0px; overflow: hidden !important; padding: 0px; position: static; top: 0px; transition: none 0s ease 0s; user-select: none; vertical-align: 0px; width: 1px !important;"><math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">(</mo><mn>7</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mspace width="0.667em"></mspace><mi>mod</mi><mspace width="thinmathspace"></mspace><mspace width="thinmathspace"></mspace><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mn>3</mn></mrow></msup></math></span></span>,即为 0,往头部补 0,得 000</div>
<div class="has-jax" style="margin-bottom: 20px;">
8,加密后长度为 <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><msub><mi>g</mi><mn>2</mn></msub><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">(</mo><mn>8</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></math>" id="MathJax-Element-7-Frame" role="presentation" style="border: 0px; direction: ltr; display: inline; float: none; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; overflow-wrap: normal; padding: 0px; position: relative; white-space: nowrap; word-spacing: normal;" tabindex="0"><nobr aria-hidden="true" style="border: 0px; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; transition: none 0s ease 0s; vertical-align: 0px;"><span class="math" id="MathJax-Span-94" style="border: 0px; box-sizing: content-box; display: inline-block; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 5.465em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; font-size: 21.6px; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 4.539em;"><span style="border: 0px; box-sizing: content-box; clip: rect(1.715em, 1004.49em, 2.873em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -2.544em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-95" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-96" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑙</span><span class="mi" id="MathJax-Span-97" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑜</span><span class="msubsup" id="MathJax-Span-98" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 0.928em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.382em, 1000.51em, 4.308em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.979em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-99" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">𝑔</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; left: 0.512em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.84em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-100" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">2</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span><span class="texatom" id="MathJax-Span-101" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-102" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-103" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">(</span><span class="mn" id="MathJax-Span-104" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">8</span><span class="mo" id="MathJax-Span-105" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">+</span><span class="mn" id="MathJax-Span-106" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-107" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">)</span></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 2.549em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-bottom-style: initial; border-color: initial; border-image: initial; border-left-style: solid; border-right-style: initial; border-top-style: initial; border-width: 0px; box-sizing: content-box; display: inline-block; height: 1.169em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: -0.275em; width: 0px;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation" style="border: 0px; box-sizing: content-box; clip: rect(1px, 1px, 1px, 1px); display: inline; height: 1px !important; left: 0px; line-height: normal; margin: 0px; overflow: hidden !important; padding: 0px; position: static; top: 0px; transition: none 0s ease 0s; user-select: none; vertical-align: 0px; width: 1px !important;"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi><mi>o</mi><msub><mi>g</mi><mn>2</mn></msub><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">(</mo><mn>8</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></math></span></span>,即 3,尾值为 <span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">(</mo><mn>8</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mspace width="0.667em" /><mi>mod</mi><mspace width="thinmathspace" /><mspace width="thinmathspace" /><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mn>3</mn></mrow></msup></math>" id="MathJax-Element-8-Frame" role="presentation" style="border: 0px; direction: ltr; display: inline; float: none; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; overflow-wrap: normal; padding: 0px; position: relative; white-space: nowrap; word-spacing: normal;" tabindex="0"><nobr aria-hidden="true" style="border: 0px; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; transition: none 0s ease 0s; vertical-align: 0px;"><span class="math" id="MathJax-Span-108" style="border: 0px; box-sizing: content-box; display: inline-block; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 8.475em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; font-size: 21.6px; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 7.039em;"><span style="border: 0px; box-sizing: content-box; clip: rect(1.53em, 1007.04em, 2.873em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -2.544em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-109" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-110" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">(</span><span class="mn" id="MathJax-Span-111" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">8</span><span class="mo" id="MathJax-Span-112" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">+</span><span class="mn" id="MathJax-Span-113" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.234em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-114" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">)</span><span class="TeXmathchoice" id="MathJax-Span-115" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mspace" id="MathJax-Span-116" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.743em;"></span></span><span class="mi" id="MathJax-Span-117" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.188em; position: static; transition: none 0s ease 0s; vertical-align: 0px;">mod</span><span class="mspace" id="MathJax-Span-118" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.188em;"></span><span class="mspace" id="MathJax-Span-119" style="border: 0px; box-sizing: content-box; display: inline-block; height: 0em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0em; width: 0.188em;"></span><span class="msubsup" id="MathJax-Span-120" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.188em; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; transition: none 0s ease 0s; vertical-align: 0px; width: 0.928em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.15em, 1000.47em, 4.123em, -999.998em); left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -3.979em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-121" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">2</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; left: 0.512em; line-height: normal; margin: 0px; padding: 0px; position: absolute; top: -4.396em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="texatom" id="MathJax-Span-122" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-123" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-124" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 15.2712px; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px;">3</span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.984em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 2.549em; line-height: normal; margin: 0px; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-bottom-style: initial; border-color: initial; border-image: initial; border-left-style: solid; border-right-style: initial; border-top-style: initial; border-width: 0px; box-sizing: content-box; display: inline-block; height: 1.392em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; transition: none 0s ease 0s; vertical-align: -0.275em; width: 0px;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation" style="border: 0px; box-sizing: content-box; clip: rect(1px, 1px, 1px, 1px); display: inline; height: 1px !important; left: 0px; line-height: normal; margin: 0px; overflow: hidden !important; padding: 0px; position: static; top: 0px; transition: none 0s ease 0s; user-select: none; vertical-align: 0px; width: 1px !important;"><math xmlns="http://www.w3.org/1998/Math/MathML"><mo stretchy="false">(</mo><mn>8</mn><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo><mspace width="0.667em"></mspace><mi>mod</mi><mspace width="thinmathspace"></mspace><mspace width="thinmathspace"></mspace><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mn>3</mn></mrow></msup></math></span></span>,即为 1,往头部补 0,得 001</div>
</li>
</ul>
<pre style="background: rgb(247, 247, 247); border: 0px; color: #4d4d4c; font-family: consolas, Menlo, monospace, "PingFang SC", "Microsoft YaHei"; font-size: 15.75px; line-height: 1.6; overflow: auto; padding: 10px 0px 10px 10px; width: 561px;"><span class="line"> <span class="function" style="color: #4271ae;"><span class="keyword" style="color: #8959a8;">public</span> String <span class="title" style="color: #3e999f;">encode</span><span class="params" style="color: #f5871f;">(<span class="keyword" style="color: #8959a8;">int</span> num)</span> </span>{</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">if</span> (num == <span class="number" style="color: #f5871f;">0</span>) {</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">return</span> <span class="string" style="color: #718c00;">""</span>;</span>
<span class="line"> }</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">int</span> len = (<span class="keyword" style="color: #8959a8;">int</span>) (Math.log(num + <span class="number" style="color: #f5871f;">1</span>) / Math.log(<span class="number" style="color: #f5871f;">2</span>));</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">int</span> r = (<span class="keyword" style="color: #8959a8;">int</span>) ((num + <span class="number" style="color: #f5871f;">1</span>) % Math.pow(<span class="number" style="color: #f5871f;">2</span>, len));</span>
<span class="line"> String res = <span class="string" style="color: #718c00;">""</span>;</span>
<span class="line"> String tail = Integer.toString(r, <span class="number" style="color: #f5871f;">2</span>);</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">for</span> (<span class="keyword" style="color: #8959a8;">int</span> i = <span class="number" style="color: #f5871f;">0</span>; i < len - tail.length(); i++) {</span>
<span class="line"> res += <span class="string" style="color: #718c00;">"0"</span>;</span>
<span class="line"> }</span>
<span class="line"> res += tail;</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">return</span> res;</span>
<span class="line"> }</span></pre>
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(找规律) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo>&#x2061;</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo></mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">可以很容易发现,编码的字符串在每个长度 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>l</mi></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0">l</span> 内都是按 0 到 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>2</mn><mi>l</mi></msup><mo>&#x2212;</mo><mn>1</mn></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>2</mn><mi>l</mi></msup><mo>−</mo><mn>1</mn></math></span></span> 的方式排列的。</li>
<li style="line-height: 1.5em;">首先寻找最终编码字符串的长度,寻找长度的过程中,让目标值减去当前长度能编码字符串的数量。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">寻找长度的时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo>&#x2061;</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-4-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo></mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span>,构造答案的时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo>&#x2061;</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-5-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo></mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span>。</li>
<li style="line-height: 1.5em;">故最终时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo>&#x2061;</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-6-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo></mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">需要额外 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo>&#x2061;</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-7-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo></mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的空间存储答案。</li>
</ul>
<div style="box-sizing: border-box;">
<pre class="hljs cs" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">string</span> <span class="hljs-title" style="display: initial !important;">encode</span>(<span class="hljs-params" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> num</span>) </span>{
<span class="hljs-keyword" style="display: initial !important;">int</span> len = <span class="hljs-number" style="display: initial !important;">0</span>;
<span class="hljs-keyword" style="display: initial !important;">while</span> (num >= (<span class="hljs-number" style="display: initial !important;">1</span> << len)) {
num -= (<span class="hljs-number" style="display: initial !important;">1</span> << len);
len++;
}
<span class="hljs-keyword" style="display: initial !important;">string</span> ans;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i < len; i++, num >>= <span class="hljs-number" style="display: initial !important;">1</span>)
<span class="hljs-keyword" style="display: initial !important;">if</span> (num & <span class="hljs-number" style="display: initial !important;">1</span>)
ans += <span class="hljs-string" style="display: initial !important;">'1'</span>;
<span class="hljs-keyword" style="display: initial !important;">else</span>
ans += <span class="hljs-string" style="display: initial !important;">'0'</span>;
reverse(ans.begin(), ans.end());
<span class="hljs-keyword" style="display: initial !important;">return</span> ans;
}
</code></pre>
<div>
<code style="display: initial !important;"><br /></code></div>
<div>
<code style="display: initial !important;"><a href="https://leetcode.jp/leetcode-1256-encode-number-%E8%A7%A3%E9%A2%98%E6%80%9D%E8%B7%AF%E5%88%86%E6%9E%90/">https://leetcode.jp/leetcode-1256-encode-number-%E8%A7%A3%E9%A2%98%E6%80%9D%E8%B7%AF%E5%88%86%E6%9E%90/</a></code></div>
<div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
本题需要先观察出给定的加密规律,与普通的二进制数不同,当出现进位时,所有位数的值要变为0,比如:11加1后不是100,而应该是000,同理111加一后是0000,虽然在二进制中000与0000都代表0,但在本题中因为位数不同而所代表的的意义也是不同的。</div>
<div style="background: rgb(255, 255, 255); border: 0px; color: #333333; font-family: Georgia, "Bitstream Charter", serif; font-size: 16px; margin-bottom: 24px; padding: 0px; vertical-align: baseline;">
接下来再看,1位数能代表2个数字,0和1,2位数能代表4个数,00,01,10,11。以此类推可得到:n位数能代表2的n次方个数字。通过题目给出的数字,首先我们可以算出该数字加密后的位数,方法很简单,在满足num大于等于0的前提下,不断的用num减去2的n次方(n>=0, n++),直到不能再相减为止,此时的n即为加密后数字的位数,同时num剩下的值即是当前位数长度下的二进制数字。(注意高位不足的地方需要补0)</div>
</div>
<div>
<div class="line number1 index0 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">public</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">String encode(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">num) {</code></div>
<div class="line number2 index1 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">length=</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">; </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 加密后的位数</code></div>
<div class="line number3 index2 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">while</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">true</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">){</code></div>
<div class="line number4 index3 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 计算当前位数下能有多少数字</code></div>
<div class="line number5 index4 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">n=(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">)Math.pow(</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">2</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">,length);</code></div>
<div class="line number6 index5 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 如果num不足以表示下n个数字,退出</code></div>
<div class="line number7 index6 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">if</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(num-n<</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">) </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">break</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;</code></div>
<div class="line number8 index7 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">num-=n; </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// num减去n,继续循环</code></div>
<div class="line number9 index8 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">length++; </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 位数加一</code></div>
<div class="line number10 index9 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number11 index10 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">String res=</code><code class="java string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(42, 0, 255) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">""</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">; </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 返回结果</code></div>
<div class="line number12 index11 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">for</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">(</code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">int</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">i=</code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">0</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;i<length;i++){ </code><code class="java comments" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(63, 95, 191) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">// 循环位数计算二进制</code></div>
<div class="line number13 index12 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">res = num % </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">2</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">+ </code><code class="java string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(42, 0, 255) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">""</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">+ res;</code></div>
<div class="line number14 index13 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">num /= </code><code class="java value" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 153, 0) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">2</code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">;</code></div>
<div class="line number15 index14 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
<div class="line number16 index15 alt1" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java spaces" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;"> </code><code class="java keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(127, 0, 85) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">return</code> <code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">res;</code></div>
<div class="line number17 index16 alt2" style="background: none white !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: #333333; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-size: 16px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<code class="java plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 13px; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; text-size-adjust: 160%; top: auto !important; vertical-align: baseline !important; width: auto !important;">}</code></div>
</div>
</div>
<br />X.</div>
<div style="box-sizing: border-box;">
<a href="https://blog.csdn.net/qq_17550379/article/details/103109066">https://blog.csdn.net/qq_17550379/article/details/103109066</a></div>
<div style="box-sizing: border-box;">
<span style="background-color: white; color: #4d4d4d; font-family: "Microsoft YaHei", "SF Pro Display", Roboto, Noto, Arial, "PingFang SC", sans-serif; font-size: 16px; font-variant-ligatures: common-ligatures;">找规律的问题,规律也非常的简单。通过观察</span></div>
<pre style="background-color: white; box-sizing: border-box; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; font-variant-ligatures: common-ligatures; line-height: 22px; margin-bottom: 24px; margin-top: 1.2em; outline: 0px; overflow-wrap: break-word; padding: 0px; position: relative;"><code style="background-color: #1d1f21; border-radius: 4px; box-sizing: border-box; color: #c5c8c6; display: block; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; line-height: 22px; margin: 0px; min-width: 94%; outline: 0px; overflow-wrap: break-word; overflow-x: auto; padding: 0.5em; position: unset; text-size-adjust: none;">bin(0 + 1) -> 1
bin(1 + 1) -> 10
bin(2 + 1) -> 11
bin(3 + 1) -> 100
bin(4 + 1) -> 101
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #4d4d4d; font-family: "Microsoft YaHei", "SF Pro Display", Roboto, Noto, Arial, "PingFang SC", sans-serif; font-size: 16px; font-variant-ligatures: common-ligatures; line-height: 26px; margin-bottom: 16px; outline: 0px; overflow-wrap: break-word; padding: 0px;">
不难发现结果就是<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">num+1</code>的二进制取出第一位即可。当然也可以通过数学来得到,首先不难发现相同位数的数成等比数列</div>
<pre style="background-color: white; box-sizing: border-box; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; font-variant-ligatures: common-ligatures; line-height: 22px; margin-bottom: 24px; margin-top: 1.2em; outline: 0px; overflow-wrap: break-word; padding: 0px; position: relative;"><code style="background-color: #1d1f21; border-radius: 4px; box-sizing: border-box; color: #c5c8c6; display: block; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; line-height: 22px; margin: 0px; min-width: 94%; outline: 0px; overflow-wrap: break-word; overflow-x: auto; padding: 0.5em; position: unset; text-size-adjust: none;">0位 -> 1
1位 -> 2
2位 -> 4
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #4d4d4d; font-family: "Microsoft YaHei", "SF Pro Display", Roboto, Noto, Arial, "PingFang SC", sans-serif; font-size: 16px; font-variant-ligatures: common-ligatures; line-height: 26px; margin-bottom: 16px; outline: 0px; overflow-wrap: break-word; padding: 0px;">
那么通过等于数列求和公式很容易得到<span style="box-sizing: border-box; font-weight: 700; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">每位的第一个数</span>为<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">2^n - 1</code>(其中<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">n</code>表示位数),那么<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">num</code>显然就可以表示为<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">bin(num + 1 - 2^n)</code>,而<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">n</code>就是<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">bin(num)</code>的最高位<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">1</code>所在的位置<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">+1</code>,那么为了方便计算可以忽略<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">2^n</code>,最后的结果就是<code style="background-color: #f9f2f4; border-radius: 2px; box-sizing: border-box; color: #c7254e; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; line-height: 22px; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 2px 4px;">bin(num + 1)</code>去除最高位的结果。</div>
<pre style="background-color: white; box-sizing: border-box; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 14px; font-variant-ligatures: common-ligatures; line-height: 22px; margin-bottom: 24px; margin-top: 1.2em; outline: 0px; overflow-wrap: break-word; padding: 0px; position: relative;"><code class="prism language-python" style="background: rgb(29, 31, 33); border-radius: 4px; box-sizing: border-box; color: #c5c8c6; display: block; font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif; line-height: 22px; margin: 0px; min-width: 94%; outline: 0px; overflow-wrap: break-word; overflow-x: auto; padding: 0.5em; position: unset; text-size-adjust: none;"><span class="token keyword" style="box-sizing: border-box; color: #b294bb; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">class</span> <span class="token class-name" style="box-sizing: border-box; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">Solution</span><span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">:</span>
<span class="token keyword" style="box-sizing: border-box; color: #b294bb; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">def</span> <span class="token function" style="box-sizing: border-box; color: #81a2be; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">encode</span><span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">(</span>self<span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">,</span> num<span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">:</span> <span class="token builtin" style="box-sizing: border-box; color: #b5bd68; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">int</span><span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">)</span> <span class="token operator" style="box-sizing: border-box; color: #a67f59; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">-</span><span class="token operator" style="box-sizing: border-box; color: #a67f59; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">></span> <span class="token builtin" style="box-sizing: border-box; color: #b5bd68; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">str</span><span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">:</span>
<span class="token keyword" style="box-sizing: border-box; color: #b294bb; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">return</span> <span class="token builtin" style="box-sizing: border-box; color: #b5bd68; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">bin</span><span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">(</span>num <span class="token operator" style="box-sizing: border-box; color: #a67f59; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">+</span> <span class="token number" style="box-sizing: border-box; color: #b5bd68; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">1</span><span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">)</span><span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">[</span><span class="token number" style="box-sizing: border-box; color: #b5bd68; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">3</span><span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">:</span><span class="token punctuation" style="box-sizing: border-box; color: #999999; font-stretch: normal !important; font-variant-east-asian: normal !important; font-variant-ligatures: normal !important; font-variant-numeric: normal !important; line-height: normal !important; margin: 0px; outline: 0px; overflow-wrap: break-word; padding: 0px;">]</span></code></pre>
<div style="box-sizing: border-box;">
<ul style="background-color: white; color: #555555; font-family: Lato, "PingFang SC", "Microsoft YaHei", sans-serif; font-size: 18px;">
<li>一开始参赛写出来很开心,最后发现,原来一句话就能写出来了,桑心。</li>
<li>num + 1 的二进制去掉首位就是了。</li>
</ul>
<pre style="background: rgb(247, 247, 247); border: 0px; color: #4d4d4c; font-family: consolas, Menlo, monospace, "PingFang SC", "Microsoft YaHei"; font-size: 15.75px; line-height: 1.6; overflow: auto; padding: 10px 0px 10px 10px; width: 551px;"><span class="line"> <span class="function" style="color: #4271ae;"><span class="keyword" style="color: #8959a8;">public</span> String <span class="title" style="color: #3e999f;">encode</span><span class="params" style="color: #f5871f;">(<span class="keyword" style="color: #8959a8;">int</span> num)</span> </span>{</span>
<span class="line"> <span class="keyword" style="color: #8959a8;">return</span> Integer.toString(num + <span class="number" style="color: #f5871f;">1</span>, <span class="number" style="color: #f5871f;">2</span>).substring(<span class="number" style="color: #f5871f;">1</span>);</span>
<span class="line"> }</span></pre>
</div>
<div style="box-sizing: border-box;">
<br /></div>
<div style="box-sizing: border-box;">
<br /></div>
</div>
Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-1892661270975491352019-12-13T22:47:00.003-08:002019-12-13T22:47:43.985-08:00LeetCode 1257 - Smallest Common Region<a href="https://www.cnblogs.com/seyjs/p/11878841.html">https://www.cnblogs.com/seyjs/p/11878841.html</a><br />
<br />
<div style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
</div>
<br />
<div class="content__u3I1 question-content__JfgR" style="-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #24292e; font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; margin-top: 0px !important; orphans: 2; text-align: start; text-decoration-color: initial; text-decoration-style: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box; margin: 0px 0px 16px;">
You are given some lists of<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">regions</code> where the first region of each list includes all other regions in that list.</div>
<div style="box-sizing: border-box; margin: 0px 0px 16px;">
Naturally, if a region<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">X</code> contains another region<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">Y</code> then<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">X</code> is bigger than<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">Y</code>. Also by definition a region X contains itself.</div>
<div style="box-sizing: border-box; margin: 0px 0px 16px;">
Given two regions<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">region1</code>,<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">region2</code>, find out the<span> </span><strong style="box-sizing: border-box; font-weight: 600;">smallest</strong><span> </span>region that contains both of them.</div>
<div style="box-sizing: border-box; margin: 0px 0px 16px;">
If you are given regions<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">r1</code>,<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">r2</code><span> </span>and<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">r3</code><span> </span>such that<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">r1</code><span> </span>includes<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">r3</code>, it is guaranteed there is no<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">r2</code><span> </span>such that<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">r2</code><span> </span>includes<span> </span><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">r3</code>.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />It's guaranteed the smallest region exists.</div>
<div style="box-sizing: border-box; margin: 0px 0px 16px;">
<br /></div>
<div style="box-sizing: border-box; margin: 0px 0px 16px;">
<strong style="box-sizing: border-box; font-weight: 600;">Example 1:</strong></div>
<pre style="background-color: #f6f8fa; border-radius: 3px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; display: block; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; line-height: 1.45; margin: 0px 0px 16px; overflow-wrap: normal; overflow: auto; padding: 16px; word-break: break-all;"><strong style="box-sizing: border-box; font-weight: 600;">Input:
</strong>regions = [["Earth","North America","South America"],
["North America","United States","Canada"],
["United States","New York","Boston"],
["Canada","Ontario","Quebec"],
["South America","Brazil"]],
region1 = "Quebec",
region2 = "New York"
<strong style="box-sizing: border-box; font-weight: 600;">Output:</strong> "North America"
</pre>
<div style="box-sizing: border-box; margin: 0px 0px 16px;">
<br /></div>
<div style="box-sizing: border-box; margin: 0px 0px 16px;">
<strong style="box-sizing: border-box; font-weight: 600;">Constraints:</strong></div>
<ul style="box-sizing: border-box; margin-bottom: 16px; margin-top: 0px; padding-left: 2em;">
<li style="box-sizing: border-box;"><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">2 <= regions.length <= 10^4</code></li>
<li style="box-sizing: border-box; margin-top: 0.25em;"><code style="background-color: rgba(27, 31, 35, 0.05); border-radius: 3px; box-sizing: border-box; color: #c7254e; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin: 0px; padding: 0.2em 0.4em;">region1 != region2</code></li>
<li style="box-sizing: border-box; margin-top: 0.25em;">All strings consist of English letters and spaces with at most 20 letters.</li>
</ul>
</div>
</div>
<br />
<a href="https://www.acwing.com/file_system/file/content/whole/index/content/150816/">https://www.acwing.com/file_system/file/content/whole/index/content/150816/</a><br />
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(暴力枚举) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">根据题目描述,区域构成的数据结构就是一棵树,每个列表给出的是边关系。(如果不是树则不能保证最近公共祖先一定存在)。</li>
<li style="line-height: 1.5em;">每个区域记录下自己的父亲区域,然后暴力求出 <code>region1</code> 的祖先区域列表,然后在 <code>region2</code> 求的过程中匹配最近的 <code>region1</code> 的区域列表。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">遍历一次区域列表,然后遍历两个区域的祖先,故时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">需要额外 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的空间记录每个区域的父亲,以及 <code>region1</code> 的祖先区域列表。</li>
</ul>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-function" style="display: initial !important;"><span class="hljs-built_in" style="display: initial !important;">string</span> <span class="hljs-title" style="display: initial !important;">findSmallestRegion</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">string</span>>>& regions, <span class="hljs-built_in" style="display: initial !important;">string</span> region1, <span class="hljs-built_in" style="display: initial !important;">string</span> region2)</span> </span>{
<span class="hljs-built_in" style="display: initial !important;">unordered_map</span><<span class="hljs-built_in" style="display: initial !important;">string</span>, <span class="hljs-built_in" style="display: initial !important;">string</span>> fa;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">auto</span> &v : regions)
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">1</span>; i < v.size(); i++)
fa[v[i]] = v[<span class="hljs-number" style="display: initial !important;">0</span>];
<span class="hljs-built_in" style="display: initial !important;">unordered_set</span><<span class="hljs-built_in" style="display: initial !important;">string</span>> anc;
<span class="hljs-keyword" style="display: initial !important;">while</span> (fa.find(region1) != fa.end()) {
anc.insert(region1);
region1 = fa[region1];
}
anc.insert(region1);
<span class="hljs-keyword" style="display: initial !important;">while</span> (fa.find(region2) != fa.end()) {
<span class="hljs-keyword" style="display: initial !important;">if</span> (anc.find(region2) != anc.end())
<span class="hljs-keyword" style="display: initial !important;">return</span> region2;
region2 = fa[region2];
}
<span class="hljs-keyword" style="display: initial !important;">return</span> region2;
}</code></pre>
<br />
<br />
<div style="background-color: white; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; margin: 10px auto; padding: 0px;">
首先递归找出region1所属的regions链,并保持结果;然后再递归查找region2所属的regions链,找到第一个region在region1所属的regions链即可。</div>
<div>
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="line-height: 1.8; margin: 0px; padding: 0px;"> </span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">def</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> findSmallestRegion(self, regions, region1, region2):
</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">"""</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">
:type regions: List[List[str]]
:type region1: str
:type region2: str
:rtype: str
</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">"""</span><span style="line-height: 1.8; margin: 0px; padding: 0px;">
dic </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> {}
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">for</span> region <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> regions:
parent </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> region[0]
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">for</span> i <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span> range(1<span style="line-height: 1.8; margin: 0px; padding: 0px;">,len(region)):
dic[region[i]] </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> parent
dic_region1 </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> {}
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">while</span> region1 <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> dic:
dic_region1[region1] </span>= 1<span style="line-height: 1.8; margin: 0px; padding: 0px;">
region1 </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> dic[region1]
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">while</span> region2 <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> dic:
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">if</span> region2 <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> dic_region1:
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> region2
region2 </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> dic[region2]
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">return</span> regions[0][0]</pre>
</div>
<div>
<br /></div>
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-45871375520464315732019-12-13T22:45:00.002-08:002019-12-13T22:45:25.820-08:00LeetCode 1258 - Synonymous Sentences<a href="https://www.coordinate.wang/index.php/archives/2763/">https://www.coordinate.wang/index.php/archives/2763/</a><br />
<div style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #313131; font-family: "Microsoft Yahei", arial; font-size: 14px; line-height: 1.5; margin-bottom: 10px; margin-top: 20px; overflow-wrap: break-word; padding: 0px; vertical-align: baseline; word-break: break-word;">
给你一个近义词表 <code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">synonyms</code> 和一个句子 <code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">text</code> , <code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">synonyms</code> 表中是一些近义词对 ,你可以将句子 <code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">text</code> 中每个单词用它的近义词来替换。</div>
<div style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #313131; font-family: "Microsoft Yahei", arial; font-size: 14px; line-height: 1.5; margin-bottom: 10px; margin-top: 20px; overflow-wrap: break-word; padding: 0px; vertical-align: baseline; word-break: break-word;">
请你找出所有用近义词替换后的句子,按 <strong style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; padding: 0px;">字典序排序</strong> 后返回。</div>
<div style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #313131; font-family: "Microsoft Yahei", arial; font-size: 14px; line-height: 1.5; margin-bottom: 10px; margin-top: 20px; overflow-wrap: break-word; padding: 0px; vertical-align: baseline; word-break: break-word;">
<strong style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; padding: 0px;">示例 1:</strong></div>
<pre style="-webkit-font-smoothing: antialiased; background: rgb(247, 247, 247); border-radius: 4px; border: 0px; box-sizing: border-box; color: #313131; font-size: 14px; margin-bottom: 20px; margin-top: 20px; overflow-x: auto; padding: 0px; position: relative; text-shadow: none; vertical-align: baseline;"><code class="hljs bash" style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 3px; border: 0px; box-sizing: border-box; display: block; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; line-height: 20px; margin: 0px; overflow: auto; padding: 1.3em;">输入:
synonyms = [[<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"happy"</span>,<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"joy"</span>],[<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"sad"</span>,<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"sorrow"</span>],[<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"joy"</span>,<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"cheerful"</span>]],
text = <span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"I am happy today but was sad yesterday"</span>
输出:
[<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"I am cheerful today but was sad yesterday"</span>,
<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"I am cheerful today but was sorrow yesterday"</span>,
<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"I am happy today but was sad yesterday"</span>,
<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"I am happy today but was sorrow yesterday"</span>,
<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"I am joy today but was sad yesterday"</span>,
<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">"I am joy today but was sorrow yesterday"</span>]</code></pre>
<div style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #313131; font-family: "Microsoft Yahei", arial; font-size: 14px; line-height: 1.5; margin-bottom: 10px; margin-top: 20px; overflow-wrap: break-word; padding: 0px; vertical-align: baseline; word-break: break-word;">
<strong style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; padding: 0px;">提示:</strong></div>
<ul style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #313131; font-family: "Microsoft Yahei", arial; font-size: 14px; margin: 20px 0px; padding: 0px 0px 0px 30px; vertical-align: baseline;">
<li style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; overflow-wrap: break-word; padding: 0px;"><code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">0 <= synonyms.length <= 10</code></li>
<li style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; overflow-wrap: break-word; padding: 0px;"><code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">synonyms[i].length == 2</code></li>
<li style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; overflow-wrap: break-word; padding: 0px;"><code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">synonyms[0] != synonyms[1]</code></li>
<li style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; overflow-wrap: break-word; padding: 0px;">所有单词仅包含英文字母,且长度最多为 <code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">10</code> 。</li>
<li style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; overflow-wrap: break-word; padding: 0px;"><code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">text</code> 最多包含 <code style="-webkit-font-smoothing: antialiased; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; margin: 0px 3px; padding: 2px 6px;">10</code> 个单词,且单词间用单个空格分隔开。</li>
</ul>
<div>
<div style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #313131; font-family: "Microsoft Yahei", arial; font-size: 14px; line-height: 1.5; margin-bottom: 10px; margin-top: 20px; overflow-wrap: break-word; padding: 0px; vertical-align: baseline; word-break: break-word;">
首先观察题目,发现相同近义词可能有很多个,所以不难想到通过并查集找到所有的集合。然后单词替换问题,实际上就是<a href="https://mp.csdn.net/postedit/82621887" style="-webkit-font-smoothing: antialiased; border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-image: initial; border-left-color: initial; border-left-style: initial; border-right-color: initial; border-right-style: initial; border-top-color: initial; border-top-style: initial; border-width: 0px 0px 1px; box-sizing: border-box; color: #313131; margin: 0px; outline: 0px; padding: 0px; text-decoration-line: none;" target="_blank">Leetcode 78:子集(最详细的解法!!!)</a>。</div>
<pre style="-webkit-font-smoothing: antialiased; background: rgb(247, 247, 247); border-radius: 4px; border: 0px; box-sizing: border-box; color: #313131; font-size: 14px; margin-bottom: 20px; margin-top: 20px; overflow-x: auto; padding: 0px; position: relative; text-shadow: none; vertical-align: baseline;"><code class="lang-python hljs" style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 3px; border: 0px; box-sizing: border-box; display: block; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; line-height: 20px; margin: 0px; overflow: auto; padding: 1.3em;"> <span class="hljs-function" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; padding: 0px;"><span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">def</span> <span class="hljs-title" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #1c00cf; margin: 0px; padding: 0px;">generateSentences</span><span class="hljs-params" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #5c2699; margin: 0px; padding: 0px;">(self, synonyms: List[List[str]], text: str)</span> -> List[str]:</span>
parent = collections.defaultdict(str)
data = collections.defaultdict(list)
<span class="hljs-function" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; padding: 0px;"><span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">def</span> <span class="hljs-title" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #1c00cf; margin: 0px; padding: 0px;">find</span><span class="hljs-params" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #5c2699; margin: 0px; padding: 0px;">(x)</span>:</span>
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">if</span> x <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">not</span> <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> parent:
parent[x] = x
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">if</span> x != parent[x]:
parent[x] = find(parent[x])
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">return</span> parent[x]
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> p, q <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> synonyms:
x, y = find(p), find(q)
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">if</span> x != y:
parent[x] = y
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> k <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> parent:
data[find(k)].append(k)
res, texList = set(), text.split(<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">" "</span>)
<span class="hljs-function" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; padding: 0px;"><span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">def</span> <span class="hljs-title" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #1c00cf; margin: 0px; padding: 0px;">dfs</span><span class="hljs-params" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #5c2699; margin: 0px; padding: 0px;">(index, path)</span>:</span>
res.add(<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">" "</span>.join(path))
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> i <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> range(index, len(texList)):
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> t <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> data[find(texList[i])]:
cur = path[:]
cur[i] = t
dfs(i + <span class="hljs-number" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #1c00cf; margin: 0px; padding: 0px;">1</span>, cur)
dfs(i + <span class="hljs-number" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #1c00cf; margin: 0px; padding: 0px;">1</span>, path)
dfs(<span class="hljs-number" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #1c00cf; margin: 0px; padding: 0px;">0</span>, texList)
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">return</span> sorted(list(res))</code></pre>
<div style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #313131; font-family: "Microsoft Yahei", arial; font-size: 14px; line-height: 1.5; margin-bottom: 10px; margin-top: 20px; overflow-wrap: break-word; padding: 0px; vertical-align: baseline; word-break: break-word;">
<br /></div>
<div style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; line-height: 1.5; margin-bottom: 10px; margin-top: 20px; overflow-wrap: break-word; padding: 0px; vertical-align: baseline; word-break: break-word;">
<span style="color: #313131; font-family: Microsoft Yahei, arial;"><span style="font-size: 14px;">更加简洁的写法</span></span></div>
<pre style="-webkit-font-smoothing: antialiased; background: rgb(247, 247, 247); border-radius: 4px; border: 0px; box-sizing: border-box; color: #313131; font-size: 14px; margin-bottom: 20px; margin-top: 20px; overflow-x: auto; padding: 0px; position: relative; text-shadow: none; vertical-align: baseline;"><code class="lang-python hljs" style="-webkit-font-smoothing: antialiased; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border-radius: 3px; border: 0px; box-sizing: border-box; display: block; font-family: Menlo, "Bitstream Vera Sans Mono", "DejaVu Sans Mono", Monaco, Consolas, monospace; font-size: 13px; line-height: 20px; margin: 0px; overflow: auto; padding: 1.3em;"> <span class="hljs-function" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; padding: 0px;"><span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">def</span> <span class="hljs-title" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #1c00cf; margin: 0px; padding: 0px;">generateSentences</span><span class="hljs-params" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #5c2699; margin: 0px; padding: 0px;">(self, synonyms: List[List[str]], text: str)</span> -> List[str]:</span>
parent = collections.defaultdict(str)
data = collections.defaultdict(list)
<span class="hljs-function" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; margin: 0px; padding: 0px;"><span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">def</span> <span class="hljs-title" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #1c00cf; margin: 0px; padding: 0px;">find</span><span class="hljs-params" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #5c2699; margin: 0px; padding: 0px;">(x)</span>:</span>
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">if</span> x <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">not</span> <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> parent:
parent[x] = x
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">if</span> x != parent[x]:
parent[x] = find(parent[x])
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">return</span> parent[x]
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> p, q <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> synonyms:
x, y = find(p), find(q)
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">if</span> x != y:
parent[x] = y
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> k <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> parent:
data[find(k)].append(k)
res = [[]]
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> w <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> text.split(<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">" "</span>):
res = [it + [v] <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> it <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> res <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> v <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> data[find(w)] <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">or</span> [w]]
<span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">return</span> sorted([<span class="hljs-string" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #c41a16; margin: 0px; padding: 0px;">" "</span>.join(it) <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">for</span> it <span class="hljs-keyword" style="-webkit-font-smoothing: antialiased; border: 0px; box-sizing: border-box; color: #aa0d91; margin: 0px; padding: 0px;">in</span> res])</code></pre>
<br /><br /><a href="https://www.acwing.com/solution/LeetCode/content/6334/">https://www.acwing.com/solution/LeetCode/content/6334/</a><br /><h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(并查集,递归枚举) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo>&#x22C5;</mo><mi>L</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2^</mn><mi>n</mi></msup><mo>⋅</mo><mi>L</mi><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">用并查集求出来同义词组,然后通过递归的方式,枚举替换同义词。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">求同义词组的总时间复杂度近似为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span>,递归的时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo>&#x22C5;</mo><mi>L</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo>⋅</mo><mi>L</mi><mo stretchy="false">)</mo></math></span></span>,其中 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>L</mi></math>" id="MathJax-Element-4-Frame" role="presentation" tabindex="0">L</span> 为字符串的长度(每次替换单词需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>L</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-5-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>L</mi><mo stretchy="false">)</mo></math></span></span> 的时间)。</li>
<li style="line-height: 1.5em;">故总时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo>&#x22C5;</mo><mi>L</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-6-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo>⋅</mo><mi>L</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">并查集需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-7-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的空间,记录答案需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo>&#x22C5;</mo><mi>L</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-8-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo>⋅</mo><mi>L</mi><mo stretchy="false">)</mo></math></span></span> 的空间,故总空间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo>&#x22C5;</mo><mi>L</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-9-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mn>2</mn><mi>n</mi></msup><mo>⋅</mo><mi>L</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<pre class="hljs cpp" style="overflow: auto; padding: 10px;"><code style="background-color: #e0e0e0; color: #1f1f1f; display: initial !important; font-size: 16px;"> <span class="hljs-built_in" style="display: initial !important;">unordered_set</span><<span class="hljs-built_in" style="display: initial !important;">string</span>> candidates;
<span class="hljs-built_in" style="display: initial !important;">unordered_map</span><<span class="hljs-built_in" style="display: initial !important;">string</span>, <span class="hljs-built_in" style="display: initial !important;">string</span>> fa;
<span class="hljs-function" style="display: initial !important;"><span class="hljs-built_in" style="display: initial !important;">string</span> <span class="hljs-title" style="display: initial !important;">find</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-built_in" style="display: initial !important;">string</span> x)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">return</span> x == fa[x] ? x : fa[x] = find(fa[x]);
}
<span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">void</span> <span class="hljs-title" style="display: initial !important;">solve</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-keyword" style="display: initial !important;">int</span> idx, <span class="hljs-built_in" style="display: initial !important;">string</span>& text, <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">string</span>>& ans)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">if</span> (idx > text.length()) {
ans.push_back(text);
<span class="hljs-keyword" style="display: initial !important;">return</span>;
}
<span class="hljs-built_in" style="display: initial !important;">string</span> word;
<span class="hljs-keyword" style="display: initial !important;">int</span> i;
<span class="hljs-keyword" style="display: initial !important;">for</span> (i = idx; i < text.size() && text[i] != <span class="hljs-string" style="display: initial !important;">' '</span>; i++)
word += text[i];
i++;
solve(i, text, ans);
<span class="hljs-built_in" style="display: initial !important;">string</span> tmp = text;
<span class="hljs-keyword" style="display: initial !important;">if</span> (fa.find(word) != fa.end()) {
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-built_in" style="display: initial !important;">string</span> &s: candidates)
<span class="hljs-keyword" style="display: initial !important;">if</span> (word != s && find(word) == find(s)) {
text.replace(idx, word.length(), s);
solve(i - word.size() + s.size(), text, ans);
text = tmp;
}
}
}
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">string</span>> generateSentences(<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">string</span>>>& synonyms, <span class="hljs-built_in" style="display: initial !important;">string</span> text) {
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">string</span>> ans;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">auto</span> &v: synonyms) {
fa[v[<span class="hljs-number" style="display: initial !important;">0</span>]] = v[<span class="hljs-number" style="display: initial !important;">0</span>];
fa[v[<span class="hljs-number" style="display: initial !important;">1</span>]] = v[<span class="hljs-number" style="display: initial !important;">1</span>];
}
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">auto</span> &v: synonyms) {
candidates.insert(v[<span class="hljs-number" style="display: initial !important;">0</span>]);
candidates.insert(v[<span class="hljs-number" style="display: initial !important;">1</span>]);
<span class="hljs-keyword" style="display: initial !important;">if</span> (find(v[<span class="hljs-number" style="display: initial !important;">0</span>]) != find(v[<span class="hljs-number" style="display: initial !important;">1</span>]))
fa[find(v[<span class="hljs-number" style="display: initial !important;">0</span>])] = find(v[<span class="hljs-number" style="display: initial !important;">1</span>]);
}
solve(<span class="hljs-number" style="display: initial !important;">0</span>, text, ans);
sort(ans.begin(), ans.end());
<span class="hljs-keyword" style="display: initial !important;">return</span> ans;
}</code><span style="display: initial !important;">
</span></pre>
<a href="https://www.cnblogs.com/seyjs/p/11878846.html">https://www.cnblogs.com/seyjs/p/11878846.html</a><br /><div style="background-color: white; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; margin: 10px auto; padding: 0px;">
我的方法是替换,依次遍历synoyms,如果text中存在synoyms[i][0],把text中的第一个synoyms[i][0]替换成synoyms[i][1];同样,如果存在synoyms[i][1],把text中的第一个synoyms[i][1]替换成synoyms[i][0]。然后再对替换过的text做同样的操作,直到不能替换位置。考虑到synoyms[i][0] = 'a',而text中存在'an'这种情况,为了防止误替换,可以把每个单词的前后都加上'#'作为分隔。</div>
<div style="background-color: white; color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; margin: 10px auto; padding: 0px;">
<strong style="margin: 0px; padding: 0px;"></strong></div>
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="line-height: 1.8; margin: 0px; padding: 0px;"> </span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">def</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> generateSentences(self, synonyms, text):
</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">"""</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">
:type synonyms: List[List[str]]
:type text: str
:rtype: List[str]
</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">"""</span><span style="line-height: 1.8; margin: 0px; padding: 0px;">
text </span>= <span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span> + text.replace(<span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span> <span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span>, <span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span>) + <span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="line-height: 1.8; margin: 0px; padding: 0px;">
queue </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> [text]
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">for</span> (w1,w2) <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> synonyms:
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">for</span> text <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> queue :
newtext </span>= text.replace(<span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span> + w1+<span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span>,<span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span> + w2+<span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span>,1<span style="line-height: 1.8; margin: 0px; padding: 0px;">)
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">if</span> newtext != text <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">and</span> newtext <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">not</span> <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> queue:
queue.append(newtext)
newtext </span>= text.replace(<span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span> + w2 + <span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span>, <span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span> + w1 + <span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span>,1<span style="line-height: 1.8; margin: 0px; padding: 0px;">)
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">if</span> newtext != text <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">and</span> newtext <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">not</span> <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> queue:
queue.append(newtext)
res </span>=<span style="line-height: 1.8; margin: 0px; padding: 0px;"> []
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">for</span> i <span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">in</span><span style="line-height: 1.8; margin: 0px; padding: 0px;"> sorted(queue):
newtext </span>= i.replace(<span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">#</span><span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span>, <span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span> <span style="color: maroon; line-height: 1.8; margin: 0px; padding: 0px;">'</span><span style="line-height: 1.8; margin: 0px; padding: 0px;">)
res.append(newtext[</span>1:-1<span style="line-height: 1.8; margin: 0px; padding: 0px;">])
</span><span style="color: blue; line-height: 1.8; margin: 0px; padding: 0px;">return</span> res</pre>
<div>
<br /></div>
<br /><br /></div>
Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-32274914165000542532019-12-13T22:40:00.001-08:002019-12-13T22:40:19.106-08:00LeetCode 1259 - Handshakes That Don't Cross<a href="https://algorithm-notes-allinone.blogspot.com/2019/11/leetcode-1259-handshakes-that-dont-cross.html">https://algorithm-notes-allinone.blogspot.com/2019/11/leetcode-1259-handshakes-that-dont-cross.html</a><br />
<a href="https://leetcode.com/problems/handshakes-that-dont-cross/description/" style="background-color: #fff9ee; color: #888888; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px; text-decoration-line: none;">https://leetcode.com/problems/handshakes-that-dont-cross/description/</a><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br />
<div class="question-description__3U1T" style="background-color: white; box-sizing: border-box; color: #333333; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 24px; padding-top: 10px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box; margin-bottom: 10px;">
You are given an <span style="box-sizing: border-box; font-weight: 700;">even</span> number of people <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">num_people</code> that stand around a circle and each person shakes hands with someone else, so that there are <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">num_people / 2</code> handshakes total.</div>
<div style="box-sizing: border-box; margin-bottom: 10px;">
Return the number of ways these handshakes could occur such that none of the handshakes cross.</div>
<div style="box-sizing: border-box; margin-bottom: 10px;">
Since this number could be very big, return the answer <span style="box-sizing: border-box; font-weight: 700;">mod <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">10^9 + 7</code></span></div>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<br /></div>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<span style="box-sizing: border-box; font-weight: 700;">Example 1:</span></div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow-wrap: break-word; overflow: auto; padding: 9.5px; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 700;">Input:</span> num_people = 2
<span style="box-sizing: border-box; font-weight: 700;">Output:</span> 1
</pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<span style="box-sizing: border-box; font-weight: 700;">Example 2:</span></div>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<img alt="" src="https://assets.leetcode.com/uploads/2019/07/11/5125_example_2.png" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; box-shadow: rgba(0, 0, 0, 0.1) 1px 1px 5px; box-sizing: border-box; height: 311px; padding: 5px; vertical-align: middle; width: 651px;" /></div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow-wrap: break-word; overflow: auto; padding: 9.5px; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 700;">Input:</span> num_people = 4
<span style="box-sizing: border-box; font-weight: 700;">Output:</span> 2
<span style="box-sizing: border-box; font-weight: 700;">Explanation:</span> There are two ways to do it, the first way is [(1,2),(3,4)] and the second one is [(2,3),(4,1)].
</pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<span style="box-sizing: border-box; font-weight: 700;">Example 3:</span></div>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<img alt="" src="https://assets.leetcode.com/uploads/2019/07/11/5125_example_3.png" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; box-shadow: rgba(0, 0, 0, 0.1) 1px 1px 5px; box-sizing: border-box; height: 992px; padding: 5px; vertical-align: middle; width: 664px;" /></div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow-wrap: break-word; overflow: auto; padding: 9.5px; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 700;">Input:</span> num_people = 6
<span style="box-sizing: border-box; font-weight: 700;">Output:</span> 5
</pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<span style="box-sizing: border-box; font-weight: 700;">Example 4:</span></div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow-wrap: break-word; overflow: auto; padding: 9.5px; word-break: break-all;"><span style="box-sizing: border-box; font-weight: 700;">Input:</span> num_people = 8
<span style="box-sizing: border-box; font-weight: 700;">Output:</span> 14
</pre>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<br /></div>
<div style="box-sizing: border-box; margin-bottom: 10px;">
<span style="box-sizing: border-box; font-weight: 700;">Constraints:</span></div>
<ul style="box-sizing: border-box; line-height: 1.4; margin: 0px 0px 10px; padding: 0px 2.5em;">
<li style="box-sizing: border-box; margin: 0px 0px 0.25em; padding: 0px;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">2 <= num_people <= 1000</code></li>
<li style="box-sizing: border-box; margin: 0px 0px 0.25em; padding: 0px;"><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 4px;">num_people % 2 == 0</code></li>
</ul>
</div>
</div>
<br />
<span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">This problem can be solved by dp.</span><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">Why? because, as shown in the above figures, it can be easily converted to similar questions but with smaller sizes.</span><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">Let us use dp[n] to represent the total ways for n people.</span><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">The first person can shake hands with the second one, but cannot shake hands with the second one. Why?</span><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">Because if the first person connects with the third one, then the second one have to cross the "line" to shake hands with other people.</span><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">Thus, if the first person shake hands with the jth person, this essentially divide the original circle into two parts:</span><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">one is from i+1 to the j-1 people, and the other is from j+1 to the nth people.</span><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">So we need to leave even number of people in both sides.</span><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">Thus, dp[n] = dp[0]* dp[n-2] + dp[2]*dp[n-2-2] + ... + dp[j]*dp[n-j-2].</span><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><br style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;" /><span style="background-color: #fff9ee; color: #222222; font-family: Georgia, Utopia, "Palatino Linotype", Palatino, serif; font-size: 15.4px;">In addition, due to symmetry, we can just calculate half of the circle.</span><br />
<br />
<ol class="linenums" style="color: #222222; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; margin-bottom: 0px; margin-top: 0px; white-space: pre;">
<li class="L2" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> numberOfWays</span><span class="pun" style="color: #666600;">(</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> num_people</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L3" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> n </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> num_people</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> mod </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1e9</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">7</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L4" style="margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> vector</span><span class="str" style="color: #008800;"><long></span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">n</span><span class="pun" style="color: #666600;">+</span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">,</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">);</span></li>
<li class="L5" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">1</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L6" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">for</span><span class="pun" style="color: #666600;">(</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> i</span><span class="pun" style="color: #666600;">=</span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> i</span><span class="pun" style="color: #666600;"><=</span><span class="pln" style="color: black;">n</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> i </span><span class="pun" style="color: #666600;">+=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span></li>
<li class="L7" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">for</span><span class="pun" style="color: #666600;">(</span><span class="kwd" style="color: #000088;">int</span><span class="pln" style="color: black;"> j</span><span class="pun" style="color: #666600;">=</span><span class="lit" style="color: #006666;">0</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> j</span><span class="pun" style="color: #666600;"><=</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">-</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">-</span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">;</span><span class="pln" style="color: black;"> j </span><span class="pun" style="color: #666600;">+=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">{</span><span class="com" style="color: #880000;">//calculate half circle</span></li>
<li class="L8" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">if</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">==</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">-</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">-</span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">)</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">+=</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]*</span><span class="pln" style="color: black;">dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">-</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">-</span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">]%</span><span class="pln" style="color: black;">mod</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L9" style="background: rgb(238, 238, 238); margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">else</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">+=</span><span class="pln" style="color: black;"> </span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">*((</span><span class="pln" style="color: black;">dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">]%</span><span class="pln" style="color: black;">mod</span><span class="pun" style="color: #666600;">)*(</span><span class="pln" style="color: black;">dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">-</span><span class="pln" style="color: black;">j</span><span class="pun" style="color: #666600;">-</span><span class="lit" style="color: #006666;">2</span><span class="pun" style="color: #666600;">]%</span><span class="pln" style="color: black;">mod</span><span class="pun" style="color: #666600;">)%</span><span class="pln" style="color: black;">mod</span><span class="pun" style="color: #666600;">);</span></li>
<li class="L0" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">i</span><span class="pun" style="color: #666600;">]</span><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">%=</span><span class="pln" style="color: black;">mod</span><span class="pun" style="color: #666600;">;</span></li>
<li class="L1" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L2" style="list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
<li class="L3" style="background: rgb(238, 238, 238); list-style-type: decimal !important; margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="kwd" style="color: #000088;">return</span><span class="pln" style="color: black;"> dp</span><span class="pun" style="color: #666600;">[</span><span class="pln" style="color: black;">n</span><span class="pun" style="color: #666600;">];</span></li>
<li class="L4" style="margin: 0px 0px 0.25em; padding: 0px;"><span class="pln" style="color: black;"> </span><span class="pun" style="color: #666600;">}</span></li>
</ol>
<br />
<a href="https://www.acwing.com/file_system/file/content/whole/index/content/150818/">https://www.acwing.com/file_system/file/content/whole/index/content/150818/</a><br />
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(动态规划) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;"><span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo stretchy="false">)</mo></math></span></span> 表示 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0">i</span> 个时的方案数,其中 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math>" id="MathJax-Element-4-Frame" role="presentation" tabindex="0">i</span> 为偶数。</li>
<li style="line-height: 1.5em;">初始时 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mn>0</mn><mo stretchy="false">)</mo><mo>=</mo><mn>1</mn></math>" id="MathJax-Element-5-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mn>0</mn><mo stretchy="false">)</mo><mo>=</mo><mn>1</mn></math></span></span>,其余为 0。</li>
<li style="line-height: 1.5em;">转移时,枚举第 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math>" id="MathJax-Element-6-Frame" role="presentation" tabindex="0">i</span> 个人和编号为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>j</mi></math>" id="MathJax-Element-7-Frame" role="presentation" tabindex="0">j</span> 的人握手,其中 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>j</mi></math>" id="MathJax-Element-8-Frame" role="presentation" tabindex="0">j</span> 为奇数,则 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo stretchy="false">)</mo><mo>=</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo stretchy="false">)</mo><mo>+</mo><mi>f</mi><mo stretchy="false">(</mo><mi>j</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">)</mo><mo>&#x2217;</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>&#x2212;</mo><mi>j</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">)</mo></math>" id="MathJax-Element-9-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo stretchy="false">)</mo><mo>=</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo stretchy="false">)</mo><mo>+</mo><mi>f</mi><mo stretchy="false">(</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo>∗</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>−</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></math></span></span>。这个公式的含义是,如果第 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi></math>" id="MathJax-Element-10-Frame" role="presentation" tabindex="0">i</span> 个人与第 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>j</mi></math>" id="MathJax-Element-11-Frame" role="presentation" tabindex="0">j</span> 个人握手,则前 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>j</mi><mo>&#x2212;</mo><mn>1</mn></math>" id="MathJax-Element-12-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>j</mi><mo>−</mo><mn>1</mn></math></span></span> 个人的握手问题为子问题,共有 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>j</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">)</mo></math>" id="MathJax-Element-13-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></math></span></span> 种方案;<code>[j + 1, i - 1]</code> 为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>&#x2212;</mo><mi>j</mi><mo>&#x2212;</mo><mn>1</mn></math>" id="MathJax-Element-14-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>i</mi><mo>−</mo><mi>j</mi><mo>−</mo><mn>1</mn></math></span></span> 个人握手的问题,共有 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>&#x2212;</mo><mi>j</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">)</mo></math>" id="MathJax-Element-15-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>−</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></math></span></span> 种方案,根据乘法原理和加法原理,对于每个 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>j</mi></math>" id="MathJax-Element-16-Frame" role="presentation" tabindex="0">j</span>,我们需要累加答案 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>j</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">)</mo><mo>&#x22C5;</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>&#x2212;</mo><mi>j</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">)</mo></math>" id="MathJax-Element-17-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo>⋅</mo><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><mo>−</mo><mi>j</mi><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo></math></span></span>。</li>
<li style="line-height: 1.5em;">最终答案为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-18-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>f</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">共有 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-19-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 个状态,每个状态需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-20-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的时间转移,故总时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-21-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><msup><mi>n</mi><mn>2</mn></msup><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">需要额外 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-22-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的空间存储状态。</li>
</ul>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-meta" style="display: none;">#<span class="hljs-meta-keyword" style="display: none;">define</span> LL long long</span>
<span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> <span class="hljs-title" style="display: initial !important;">numberOfWays</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-keyword" style="display: initial !important;">int</span> num_people)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">int</span> mod = <span class="hljs-number" style="display: initial !important;">1000000007</span>;
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>> f(num_people + <span class="hljs-number" style="display: initial !important;">1</span>, <span class="hljs-number" style="display: initial !important;">0</span>);
f[<span class="hljs-number" style="display: initial !important;">0</span>] = <span class="hljs-number" style="display: initial !important;">1</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">2</span>; i <= num_people; i += <span class="hljs-number" style="display: initial !important;">2</span>)
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> j = <span class="hljs-number" style="display: initial !important;">1</span>; j < i; j += <span class="hljs-number" style="display: initial !important;">2</span>)
f[i] = (f[i] + (LL)(f[j - <span class="hljs-number" style="display: initial !important;">1</span>]) * f[i - j - <span class="hljs-number" style="display: initial !important;">1</span>] % mod) % mod;
<span class="hljs-keyword" style="display: initial !important;">return</span> f[num_people];
}
</code></pre>
<div>
<br /></div>
<a href="https://www.cnblogs.com/wowpH/p/11880952.html">https://www.cnblogs.com/wowpH/p/11880952.html</a><br />
<div style="background-color: white; font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; font-size: 14px; margin: 10px auto; padding: 0px;">
先分析一下示例 3,<br style="margin: 0px; padding: 0px;" />第 6 个人和第 5 个人握手,圆被分成两部分,一部分是 4 个人,另一部分是 0 个人。0 个人的方案数为 1,4 个人的方案数可以递归计算为 2,所以这种情况有 2 种方案。<br style="margin: 0px; padding: 0px;" />第 6 个人和第 3 个人握手,圆被分成两部分,每部分都是 2 个人,2 个人的方案数是 1,所以这种情况有 1 种方案。<br style="margin: 0px; padding: 0px;" />第 6 个人和第 1 个人握手,圆被分成两部分,一部分是 0 个人,另一部分是 4 个人,所以这种情况有 2 中方案。<br style="margin: 0px; padding: 0px;" />因此 6 个人的时候有 5 种方案数。@wowpH</div>
<div style="background-color: white; font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; font-size: 14px; margin: 10px auto; padding: 0px;">
有 <code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">n</code> 个人(<code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">n</code>为偶数),如果第 <code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">n</code> 个人和第 <code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">i</code> (<code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">i = n - 1, n - 3, ……,1</code>)个人握手,那么分成的两部分中,一部分有 <code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">i - 1</code> 人,另一部分有 <code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">n - i - 1</code> 人。这两部分又是一个新的子问题。</div>
<div style="background-color: white; font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; font-size: 14px; margin: 10px auto; padding: 0px;">
所以题目可以采用 <strong style="margin: 0px; padding: 0px;">动态规划(DP)</strong> 来解决。</div>
<div style="background-color: white; font-family: "Helvetica Neue", Helvetica, Verdana, Arial, sans-serif; font-size: 14px; margin: 10px auto; padding: 0px;">
用大小为 <code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">num_people + 1</code> 的 <code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">long</code>型一维数组 <code style="background-color: rgb(245, 245, 245) !important; border-radius: 4px !important; border: 0.5px solid rgb(204, 204, 204) !important; display: inline-block; font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.4; margin: 1px 5px; padding: 0px 5px !important; vertical-align: middle;">arr</code> 来保存每种人数时的方案数。公式为:<br style="margin: 0px; padding: 0px;" /><span class="math display" style="margin: 0px; padding: 0px;"><span class="MathJax_Preview" style="color: inherit; margin: 0px; padding: 0px;"></span></span></div>
<br />
<br />
<br />
<div class="MathJax_Display" style="display: block !important; margin: 1em 0em; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; position: relative; text-align: center; text-indent: 0px; width: 828px;">
<span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>n</mi><mo stretchy="false">]</mo><mo>=</mo><mrow><mo>{</mo><mtable columnalign="left left" rowspacing=".2em" columnspacing="1em" displaystyle="false"><mtr><mtd><mn>1</mn></mtd><mtd><mtext>&#xA0;</mtext><mi>n</mi><mo>=</mo><mn>0</mn><mrow class="MJX-TeXAtom-ORD"><mo>&#x6216;</mo></mrow><mi>n</mi><mo>=</mo><mn>2</mn></mtd></mtr><mtr><mtd><mstyle displaystyle="true" scriptlevel="0"><munderover><mo>&#x2211;</mo><mrow class="MJX-TeXAtom-ORD"><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mrow class="MJX-TeXAtom-ORD"><mi>n</mi><mo>&#x2212;</mo><mn>1</mn></mrow></munderover><mo stretchy="false">(</mo><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">]</mo><mo>&#x2217;</mo><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>n</mi><mo>&#x2212;</mo><mi>i</mi><mo>&#x2212;</mo><mn>1</mn><mo stretchy="false">]</mo><mo stretchy="false">)</mo></mstyle></mtd><mtd><mtext>&#xA0;</mtext><mi>n</mi><mo>&gt;</mo><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mo>&#xFF0C;</mo></mrow><mi>n</mi><mrow class="MJX-TeXAtom-ORD"><mo>&#x4E3A;</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>&#x5076;</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>&#x6570;</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>&#xFF0C;</mo></mrow><mi>i</mi><mrow class="MJX-TeXAtom-ORD"><mo>&#x4E3A;</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>&#x5947;</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>&#x6570;</mo></mrow></mtd></mtr></mtable><mo fence="true" stretchy="true" symmetric="true"></mo></mrow><mo>.</mo></math>" id="MathJax-Element-1-Frame" role="presentation" style="border: 0px; direction: ltr; display: inline; float: none; font-size: 14px; font-style: normal; font-weight: normal; letter-spacing: normal; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; overflow-wrap: normal; padding: 0px; position: relative; text-align: center; text-indent: 0px; text-transform: none; white-space: nowrap; word-spacing: normal;" tabindex="0"><nobr aria-hidden="true" style="border: 0px; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; padding: 0px; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; white-space: nowrap !important;"><span class="math" id="MathJax-Span-1" style="border: 0px; box-sizing: content-box; display: inline-block; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 31.551em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; font-size: 16.8px; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 26.253em;"><span style="border: 0px; box-sizing: content-box; clip: rect(2.265em, 1026.19em, 6.908em, -999.997em); display: inline; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -4.818em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-2" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-3" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑎</span><span class="mi" id="MathJax-Span-4" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑟</span><span class="mi" id="MathJax-Span-5" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑟</span><span class="mo" id="MathJax-Span-6" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">[</span><span class="mi" id="MathJax-Span-7" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span><span class="mo" id="MathJax-Span-8" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">]</span><span class="mo" id="MathJax-Span-9" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.301em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">=</span><span class="mrow" id="MathJax-Span-10" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.301em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-11" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 2.384em;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0.658em;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXSizeOneSym; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -3.271em; transition: none 0s ease 0s; vertical-align: 0px;">⎧<span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXSizeOneSym; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: 0.003em; transition: none 0s ease 0s; vertical-align: 0px;">⎩<span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXSizeOneSym; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -1.664em; transition: none 0s ease 0s; vertical-align: 0px;">⎨<span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXSizeOneSym; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -2.497em; transition: none 0s ease 0s; vertical-align: 0px;">⎪<span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXSizeOneSym; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -0.83em; transition: none 0s ease 0s; vertical-align: 0px;">⎪<span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span><span class="mtable" id="MathJax-Span-12" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px 0.182em; padding: 0px; position: relative; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 21.015em;"><span style="border: 0px; box-sizing: content-box; clip: rect(2.682em, 1012.5em, 7.205em, -999.997em); display: inline; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -5.116em; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 12.563em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.158em, 1000.42em, 4.17em, -999.997em); display: inline; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -5.592em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mtd" id="MathJax-Span-13" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-14" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mn" id="MathJax-Span-15" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">1</span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; clip: rect(2.086em, 1012.5em, 5.479em, -999.997em); display: inline; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -3.449em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mtd" id="MathJax-Span-28" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-29" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mstyle" id="MathJax-Span-30" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-31" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="munderover" id="MathJax-Span-32" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 1.313em;"><span style="border: 0px; box-sizing: content-box; clip: rect(2.86em, 1001.19em, 4.646em, -999.997em); display: inline; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -3.985em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-33" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXSizeOneSym; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: -0.533em;">∑</span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; clip: rect(3.336em, 1001.01em, 4.289em, -999.997em); display: inline; left: 0.122em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -2.854em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="texatom" id="MathJax-Span-34" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-35" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-36" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; font-size: 11.8776px; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑖</span><span class="mo" id="MathJax-Span-37" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 11.8776px; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">=</span><span class="mn" id="MathJax-Span-38" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 11.8776px; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">1</span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; clip: rect(3.217em, 1001.13em, 4.289em, -999.997em); display: inline; left: 0.063em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -5.176em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="texatom" id="MathJax-Span-39" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-40" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mi" id="MathJax-Span-41" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; font-size: 11.8776px; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span><span class="mo" id="MathJax-Span-42" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 11.8776px; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">−</span><span class="mn" id="MathJax-Span-43" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; font-size: 11.8776px; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">1</span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span><span class="mo" id="MathJax-Span-44" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">(</span><span class="mi" id="MathJax-Span-45" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑎</span><span class="mi" id="MathJax-Span-46" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑟</span><span class="mi" id="MathJax-Span-47" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑟</span><span class="mo" id="MathJax-Span-48" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">[</span><span class="mi" id="MathJax-Span-49" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑖</span><span class="mo" id="MathJax-Span-50" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.241em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">−</span><span class="mn" id="MathJax-Span-51" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.241em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-52" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">]</span><span class="mo" id="MathJax-Span-53" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.241em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">∗</span><span class="mi" id="MathJax-Span-54" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.241em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑎</span><span class="mi" id="MathJax-Span-55" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑟</span><span class="mi" id="MathJax-Span-56" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑟</span><span class="mo" id="MathJax-Span-57" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">[</span><span class="mi" id="MathJax-Span-58" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span><span class="mo" id="MathJax-Span-59" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.241em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">−</span><span class="mi" id="MathJax-Span-60" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.241em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑖</span><span class="mo" id="MathJax-Span-61" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.241em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">−</span><span class="mn" id="MathJax-Span-62" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.241em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">1</span><span class="mo" id="MathJax-Span-63" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">]</span><span class="mo" id="MathJax-Span-64" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">)</span></span></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 4.051em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 5.122em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; clip: rect(1.67em, 1007.32em, 5.241em, -999.997em); display: inline; left: 13.693em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -4.223em; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 0px; line-height: normal; margin: 0px; padding: 0px; position: relative; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 7.324em;"><span style="border: 0px; box-sizing: content-box; clip: rect(3.039em, 1005.3em, 4.348em, -999.997em); display: inline; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -5.592em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mtd" id="MathJax-Span-16" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-17" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mtext" id="MathJax-Span-18" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"> </span><span class="mi" id="MathJax-Span-19" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span><span class="mo" id="MathJax-Span-20" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.301em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">=</span><span class="mn" id="MathJax-Span-21" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.301em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">0</span><span class="texatom" id="MathJax-Span-22" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-23" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-24" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral, "Arial Unicode MS", serif; font-size: 13.944px; font-style: normal; font-weight: normal; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">或</span></span></span></span><span class="mi" id="MathJax-Span-25" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span><span class="mo" id="MathJax-Span-26" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.301em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">=</span><span class="mn" id="MathJax-Span-27" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.301em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">2</span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span><span style="border: 0px; box-sizing: content-box; clip: rect(3.039em, 1007.32em, 4.348em, -999.997em); display: inline; left: 0em; line-height: normal; margin: 0px; padding: 0px; position: absolute; text-decoration: none; top: -3.39em; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mtd" id="MathJax-Span-65" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-66" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mtext" id="MathJax-Span-67" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"> </span><span class="mi" id="MathJax-Span-68" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span><span class="mo" id="MathJax-Span-69" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.301em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">></span><span class="mn" id="MathJax-Span-70" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.301em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">2</span><span class="texatom" id="MathJax-Span-71" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-72" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-73" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral, "Arial Unicode MS", serif; font-size: 13.944px; font-style: normal; font-weight: normal; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">,</span></span></span></span><span class="mi" id="MathJax-Span-74" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑛</span><span class="texatom" id="MathJax-Span-75" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-76" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-77" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral, "Arial Unicode MS", serif; font-size: 13.944px; font-style: normal; font-weight: normal; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">为</span></span></span></span><span class="texatom" id="MathJax-Span-78" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-79" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-80" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral, "Arial Unicode MS", serif; font-size: 13.944px; font-style: normal; font-weight: normal; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">偶</span></span></span></span><span class="texatom" id="MathJax-Span-81" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-82" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-83" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral, "Arial Unicode MS", serif; font-size: 13.944px; font-style: normal; font-weight: normal; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">数</span></span></span></span><span class="texatom" id="MathJax-Span-84" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-85" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-86" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral, "Arial Unicode MS", serif; font-size: 13.944px; font-style: normal; font-weight: normal; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">,</span></span></span></span><span class="mi" id="MathJax-Span-87" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Italic; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">𝑖</span><span class="texatom" id="MathJax-Span-88" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-89" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-90" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral, "Arial Unicode MS", serif; font-size: 13.944px; font-style: normal; font-weight: normal; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">为</span></span></span></span><span class="texatom" id="MathJax-Span-91" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-92" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-93" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral, "Arial Unicode MS", serif; font-size: 13.944px; font-style: normal; font-weight: normal; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">奇</span></span></span></span><span class="texatom" id="MathJax-Span-94" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mrow" id="MathJax-Span-95" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span class="mo" id="MathJax-Span-96" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"><span style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral, "Arial Unicode MS", serif; font-size: 13.944px; font-style: normal; font-weight: normal; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">数</span></span></span></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 3.991em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 4.229em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span></span><span class="mo" id="MathJax-Span-97" style="border: 0px; box-sizing: content-box; display: inline; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;"></span></span><span class="mo" id="MathJax-Span-98" style="border: 0px; box-sizing: content-box; display: inline; font-family: STIXGeneral-Regular; line-height: normal; margin: 0px; padding: 0px 0px 0px 0.182em; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px;">.</span></span><span style="border: 0px; box-sizing: content-box; display: inline-block; height: 4.824em; line-height: normal; margin: 0px; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: 0px; width: 0px;"></span></span></span><span style="border-bottom-style: initial; border-color: initial; border-image: initial; border-left-style: solid; border-right-style: initial; border-top-style: initial; border-width: 0px; box-sizing: content-box; display: inline-block; height: 5.289em; line-height: normal; margin: 0px; overflow: hidden; padding: 0px; position: static; text-decoration: none; transition: none 0s ease 0s; vertical-align: -2.354em; width: 0px;"></span></span></nobr><span class="MJX_Assistive_MathML MJX_Assistive_MathML_Block" role="presentation" style="border: 0px; box-sizing: content-box; clip: rect(1px, 1px, 1px, 1px); display: inline; height: 1px !important; left: 0px; line-height: normal; margin: 0px; overflow: hidden !important; padding: 0px; position: static; text-decoration: none; top: 0px; transition: none 0s ease 0s; user-select: none; vertical-align: 0px; width: 441.703px;"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>n</mi><mo stretchy="false">]</mo><mo>=</mo><mrow><mo>{</mo><mtable columnalign="left left" columnspacing="1em" displaystyle="false" rowspacing=".2em"><mtr><mtd><mn>1</mn></mtd><mtd><mtext> </mtext><mi>n</mi><mo>=</mo><mn>0</mn><mrow class="MJX-TeXAtom-ORD"><mo>或</mo></mrow><mi>n</mi><mo>=</mo><mn>2</mn></mtd></mtr><mtr><mtd><mstyle displaystyle="true" scriptlevel="0"><munderover><mo>∑</mo><mrow class="MJX-TeXAtom-ORD"><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mrow class="MJX-TeXAtom-ORD"><mi>n</mi><mo>−</mo><mn>1</mn></mrow></munderover><mo stretchy="false">(</mo><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy="false">]</mo><mo>∗</mo><mi>a</mi><mi>r</mi><mi>r</mi><mo stretchy="false">[</mo><mi>n</mi><mo>−</mo><mi>i</mi><mo>−</mo><mn>1</mn><mo stretchy="false">]</mo><mo stretchy="false">)</mo></mstyle></mtd><mtd><mtext> </mtext><mi>n</mi><mo>></mo><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mo>,</mo></mrow><mi>n</mi><mrow class="MJX-TeXAtom-ORD"><mo>为</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>偶</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>数</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>,</mo></mrow><mi>i</mi><mrow class="MJX-TeXAtom-ORD"><mo>为</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>奇</mo></mrow><mrow class="MJX-TeXAtom-ORD"><mo>数</mo></mrow></mtd></mtr></mtable><mo fence="true" stretchy="true" symmetric="true"></mo></mrow><mo>.</mo></math></span></span></div>
<span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> </span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">private</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> </span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">static</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> </span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">final</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> </span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">int</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> mod = </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1000000007</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">;
</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">private</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> </span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">long</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">[] arr;
</span><span class="hljs-function" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;"><span class="hljs-keyword" style="color: blue; margin: 0px; padding: 0px;">public</span> <span class="hljs-keyword" style="color: blue; margin: 0px; padding: 0px;">int</span> <span class="hljs-title" style="color: #a31515; margin: 0px; padding: 0px;">numberOfWays</span><span class="hljs-params" style="margin: 0px; padding: 0px;">(<span class="hljs-keyword" style="color: blue; margin: 0px; padding: 0px;">int</span> num_people)</span> </span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">{
arr = </span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">new</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> </span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">long</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">[num_people + </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">];
</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">return</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> (</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">int</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">) dp(num_people);
}
</span><span class="hljs-function" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;"><span class="hljs-keyword" style="color: blue; margin: 0px; padding: 0px;">private</span> <span class="hljs-keyword" style="color: blue; margin: 0px; padding: 0px;">long</span> <span class="hljs-title" style="color: #a31515; margin: 0px; padding: 0px;">dp</span><span class="hljs-params" style="margin: 0px; padding: 0px;">(<span class="hljs-keyword" style="color: blue; margin: 0px; padding: 0px;">int</span> n)</span> </span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">{
</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">if</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> (n == </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">0</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> || n == </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">2</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">) {
</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">return</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">;
}
</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">long</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> ret = </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">0</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">;
</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">for</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> (</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">int</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> i = n - </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">; i >= </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">; i -= </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">2</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">) {
</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">if</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> (arr[i - </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">] == </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">0</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">) {
arr[i - </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">] = dp(i - </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">);
}
</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">if</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> (arr[n - i - </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">] == </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">0</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">) {
arr[n - i - </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">] = dp(n - i - </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">);
}
ret += arr[i - </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">] * arr[n - i - </span><span class="hljs-number" style="font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">1</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;">];
ret %= mod;
}
</span><span class="hljs-keyword" style="color: blue; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; margin: 0px; padding: 0px; white-space: pre-wrap;">return</span><span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"> ret;
}
</span><br />
<div>
<span style="background-color: whitesmoke; font-family: Consolas, "Microsoft YaHei", monospace; font-size: 16px; white-space: pre-wrap;"><br /></span></div>
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-2466878282623913712019-12-13T22:34:00.004-08:002019-12-13T22:34:47.643-08:00LeetCode 1260 - Shift 2D Grid<a href="https://leetcode.com/problems/shift-2d-grid/">https://leetcode.com/problems/shift-2d-grid/</a><br />
<div class="content__u3I1 question-content__JfgR" style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin: 1em 0px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Given a 2D <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">grid</code> of size <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m x n</code> and an integer <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">k</code>. You need to shift the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">grid</code> <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">k</code> times.</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
In one shift operation:</div>
<ul style="box-sizing: border-box; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">Element at <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">grid[i][j]</code> becomes at <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">grid[i][j + 1]</code>.</li>
<li style="box-sizing: border-box;">Element at <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">grid[i][n - 1]</code> becomes at <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">grid[i + 1][0]</code>.</li>
<li style="box-sizing: border-box;">Element at <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">grid[n - 1][n - 1]</code> becomes at <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">grid[0][0]</code>.</li>
</ul>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Return the <em style="box-sizing: border-box;">2D grid</em> after applying shift operation <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">k</code> times.</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 1:</span></div>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/05/e1.png" style="border-style: none; box-sizing: border-box; height: 178px; max-width: 100%; vertical-align: middle; width: 400px;" /><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> <code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;">grid</code> = [[1,2,3],[4,5,6],[7,8,9]], k = 1
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> [[9,1,2],[3,4,5],[6,7,8]]
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 2:</span></div>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/05/e2.png" style="border-style: none; box-sizing: border-box; height: 166px; max-width: 100%; vertical-align: middle; width: 400px;" /><pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> <code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;">grid</code> = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 3:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> <code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;">grid</code> = [[1,2,3],[4,5,6],[7,8,9]], k = 9
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> [[1,2,3],[4,5,6],[7,8,9]]
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Constraints:</span></div>
<ul style="box-sizing: border-box; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m == grid.length</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n == grid[i].length</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= m <= 50</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= n <= 50</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">-1000 <= grid[i][j] <= 1000</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0 <= k <= 100</code></li>
</ul>
</div>
</div>
<a href="https://leetcode.com/problems/shift-2d-grid/discuss/431102/JavaPython-3-2-simple-codes-using-mod-space-O(m-*-n)-and-O(1).">https://leetcode.com/problems/shift-2d-grid/discuss/431102/JavaPython-3-2-simple-codes-using-mod-space-O(m-*-n)-and-O(1).</a><br />
<ol style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">Number the cells from 0 to m * n - 1;</li>
<li style="box-sizing: border-box;">In case k >= m * n, use k % (m * n) to avoid those whole cycles of m * n operations;</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Method 1: space O(m * n)</span><br style="box-sizing: border-box;" />Since shifting right will put the last k cells in grid on the first k cells, we start from the kth cells from last, the index of which is m * n - k % (m * n).</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> List<List<Integer>> shiftGrid(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[][] grid, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> k) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> m = grid.length, n = grid[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>].length;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> start = m * n - k % (m * n);
LinkedList<List<Integer>> ans = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> LinkedList<>();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = start; i < m * n + start; ++i) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> j = i % (m * n), r = j / n, c = j % n;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> ((i - start) % n == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>)
ans.add(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> ArrayList<>());
ans.peekLast().add(grid[r][c]);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans;
}
</code></pre>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">shiftGrid</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(self, grid: List[List[int]], k: int)</span> -> List[List[int]]:</span>
m, n = len(grid), len(grid[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>])
start = m * n - k % (m * n)
ans = []
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> i <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> range(start, m * n + start):
j = i % (m * n)
r, c = j // n, j % n
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">not</span> (j - start) % n:
ans.append([])
ans[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>].append(grid[r][c])
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans
</code></pre>
<hr style="background-color: #eceff1; border-color: rgb(236, 239, 241); border-image: initial; border-style: solid; box-sizing: content-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; height: 0px; overflow: visible;" />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Method 2: space O(1) - excluding return list</span></div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<ol style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">Imagine the grid to be a <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1-D</code> array of size <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m * n</code>;</li>
<li style="box-sizing: border-box;">reverse the whole array;</li>
<li style="box-sizing: border-box;">reverse the first <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">k</code> elements</li>
<li style="box-sizing: border-box;">reverse the remaing <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m * n - k</code> element.</li>
</ol>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> List<List<Integer>> shiftGrid(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[][] grid, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> k) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> m = grid.length, n = grid[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>].length;
k %= m * n;
reverse(grid, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, m * n - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
reverse(grid, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, k - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
reverse(grid, k, m * n - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
List<List<Integer>> ans = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> ArrayList<>();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] row : grid)
ans.add(Arrays.stream(row).boxed().collect(Collectors.toList()));
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans;
}
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">private</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">reverse</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[][] g, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> lo, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> hi)</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> m = g.length, n = g[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>].length;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">while</span> (lo < hi) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> r = lo / n, c = lo++ % n, i = hi / n, j = hi-- % n,
tmp = g[r][c];
g[r][c] = g[i][j];
g[i][j] = tmp;
}
}
</code></pre>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">shiftGrid</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(self, grid: List[List[int]], k: int)</span> -> List[List[int]]:</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">reverse</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(lo: int, hi: int)</span> -> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">None</span>:</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">while</span> lo < hi:
r, c, i, j = lo // cols, lo % cols, hi // cols, hi % cols
grid[r][c], grid[i][j] = grid[i][j], grid[r][c]
lo += <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>
hi -= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>
rows, cols = len(grid), len(grid[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>])
k %= rows * cols
reverse(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, rows * cols - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>)
reverse(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, k - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>)
reverse(k, rows * cols - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>)
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> grid</code></pre>
<br />
<a href="https://leetcode.com/problems/shift-2d-grid/discuss/431111/Simple-to-understand-java">https://leetcode.com/problems/shift-2d-grid/discuss/431111/Simple-to-understand-java</a><br />
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">public</span> static List<List<<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">Integer</span>>> shiftGrid(<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span>[][] grid, <span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> k) {
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span>[][] <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">temp</span> = <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">new</span> <span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span>[grid.length][grid[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>].length]; // took <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">temp</span> grid
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> n = grid.length;
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> m = grid[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>].length;
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> mod = n * m;
k = k % mod; // minimize the k <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">value</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < n; i++) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> j = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; j < m; j++) {
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> p = j + k; // defines which col
<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> r = p / (m); // defines which <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">row</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (p < m) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">temp</span>[i][p] = grid[i][j];
} <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span> {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">temp</span>[(i + r) % n][p % m] = grid[i][j];
}
}
}
// making <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">temp</span> grid <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">into</span> list
List<List<<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">Integer</span>>> result = <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">new</span> LinkedList<>();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span>[] t : <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">temp</span>) {
LinkedList<<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">Integer</span>> c = <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">new</span> LinkedList<>();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-type" style="box-sizing: border-box; color: #5c2699;">int</span> i : t) {
c.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(i);
}
result.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">add</span>(c);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> result;
}</code></pre>
<br />
<a href="https://www.cnblogs.com/wentiliangkaihua/p/11886518.html">https://www.cnblogs.com/wentiliangkaihua/p/11886518.html</a><br />
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
首先是最小化k,从原理(画个2*2)可以得到k可以优化到k % (n*m).</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
接下来要确定每个元素要转移到的位置,首先是column因为比较简单,算当前col(j)+ k = p,然后p < m时可以直接转换temp[i][p] = grid[i][j];</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
如果p>m, column就要再模m,p % m = j。</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
然后是比较难的row,如果p < m,就可以直接用temp[i][p] = grid[i][j]</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
如果p>m, 根据题意我们需要把每个第一列的元素下移,最后一个元素登顶,因为最终决定位置的是列,所以最重要加的只有(j+k) / m,最后再+row(i)模n</div>
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
temp[(i + r) % n][p % m] = grid[i][j];</div>
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="line-height: 1.5; margin: 0px; padding: 0px;"> </span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">public</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">static</span> List<List<Integer>> shiftGrid(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>[][] grid, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> k) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>[][] temp = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>[grid.length][grid[0].length]; <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> took temp grid</span>
<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> n =<span style="line-height: 1.5; margin: 0px; padding: 0px;"> grid.length;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> m = grid[0<span style="line-height: 1.5; margin: 0px; padding: 0px;">].length;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> mod = n *<span style="line-height: 1.5; margin: 0px; padding: 0px;"> m;
k </span>= k % mod; <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> minimize the k value</span>
<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> i = 0; i < n; i++<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> j = 0; j < m; j++<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> p = j + k; <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> defines which col</span>
<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> r = p / (m); <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> defines which row,因为整个process是column在移动所以移动整数倍m是一样的</span>
<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">if</span> (p <<span style="line-height: 1.5; margin: 0px; padding: 0px;"> m) {
temp[i][p] </span>=<span style="line-height: 1.5; margin: 0px; padding: 0px;"> grid[i][j];
} </span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">else</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> {
temp[(i </span>+ r) % n][p % m] =<span style="line-height: 1.5; margin: 0px; padding: 0px;"> grid[i][j];
}
}
}
</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> making temp grid into list</span>
List<List<Integer>> result = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> LinkedList<><span style="line-height: 1.5; margin: 0px; padding: 0px;">();
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">[] t : temp) {
LinkedList</span><Integer> c = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> LinkedList<><span style="line-height: 1.5; margin: 0px; padding: 0px;">();
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> i : t) {
c.add(i);
}
result.add(c);
}
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> result;
}
</span></pre>
<div>
<span style="line-height: 1.5; margin: 0px; padding: 0px;"><br /></span></div>
<pre style="font-family: "Courier New" !important; font-size: 12px; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="line-height: 1.5; margin: 0px; padding: 0px;"> </span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">public</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">static</span> List<List<Integer>> shiftGrid(<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>[][] grid, <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> k) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>[][] temp = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span>[grid.length][grid[0].length]; <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> took temp grid</span>
<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> n =<span style="line-height: 1.5; margin: 0px; padding: 0px;"> grid.length;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> m = grid[0<span style="line-height: 1.5; margin: 0px; padding: 0px;">].length;
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> mod = n *<span style="line-height: 1.5; margin: 0px; padding: 0px;"> m;
k </span>= k % mod; <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> minimize the k value</span>
<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> i = 0; i < n; i++<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> j = 0; j < m; j++<span style="line-height: 1.5; margin: 0px; padding: 0px;">) {
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> p = j + k; <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> defines which col</span>
<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span> r = p / m; <span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> defines which row</span>
temp[(i + r) % n][p % m] =<span style="line-height: 1.5; margin: 0px; padding: 0px;"> grid[i][j];
}
}
</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;">//</span><span style="color: green; line-height: 1.5; margin: 0px; padding: 0px;"> making temp grid into list</span>
List<List<Integer>> result = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> LinkedList<><span style="line-height: 1.5; margin: 0px; padding: 0px;">();
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;">[] t : temp) {
LinkedList</span><Integer> c = <span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">new</span> LinkedList<><span style="line-height: 1.5; margin: 0px; padding: 0px;">();
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> i : t) {
c.add(i);
}
result.add(c);
}
</span><span style="color: blue; line-height: 1.5; margin: 0px; padding: 0px;">return</span><span style="line-height: 1.5; margin: 0px; padding: 0px;"> result;
}
</span></pre>
<div>
<span style="line-height: 1.5; margin: 0px; padding: 0px;"><a href="https://www.acwing.com/file_system/file/content/whole/index/content/151184/">https://www.acwing.com/file_system/file/content/whole/index/content/151184/</a></span></div>
<div>
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(数学,原地算法) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mi>m</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mi>m</mi><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">使用额外数组的算法很基础,这里考虑不使用额外数组,且只能遍历一次整个数组。</li>
<li style="line-height: 1.5em;">观察从 <code>(0, 0)</code> 位置开始的一次 <code>k</code> 迁移,<code>(0, 0)</code> 被换到 <code>(0, k)</code>,依次类推。如果 <code>k</code> 和 <code>n*m</code> 互质,则所有位置就都换了一次,这样的迁移仅需要常数的空间。</li>
<li style="line-height: 1.5em;">如果 <code>k</code> 和 <code>n*m</code> 不互质,则除了从 <code>(0, 0)</code> 开始后,还需要从 <code>(0, 1)</code>,<code>(0, 2)</code> 等位置开始,一直到 <code>(0, g - 1)</code> 的位置开始迁移,其中 <code>g</code> 为最大公约数。</li>
<li style="line-height: 1.5em;">虽然我们从多个位置开始了迁移,但每个位置的迁移序列都不相同,即每个位置仅会被换一次。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">求最大公约数的时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo>&#x2061;</mo><mo stretchy="false">(</mo><mi>n</mi><mi>m</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>log</mi><mo></mo><mo stretchy="false">(</mo><mi>n</mi><mi>m</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo></math></span></span>。</li>
<li style="line-height: 1.5em;">每个位置仅会被换一次,故总时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mi>m</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>n</mi><mi>m</mi><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">原地算法,只需要常数的额外空间。</li>
</ul>
</div>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> <span class="hljs-title" style="display: initial !important;">gcd</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-keyword" style="display: initial !important;">int</span> x, <span class="hljs-keyword" style="display: initial !important;">int</span> y)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">while</span> (y) {
<span class="hljs-keyword" style="display: initial !important;">int</span> t = x % y;
x = y;
y = t;
}
<span class="hljs-keyword" style="display: initial !important;">return</span> x;
}
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>> shiftGrid(<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>>& grid, <span class="hljs-keyword" style="display: initial !important;">int</span> k) {
<span class="hljs-keyword" style="display: initial !important;">int</span> n = grid.size(), m = grid[<span class="hljs-number" style="display: initial !important;">0</span>].size();
<span class="hljs-keyword" style="display: initial !important;">int</span> g = gcd(k, n * m);
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> s = <span class="hljs-number" style="display: initial !important;">0</span>; s < g; s++) {
<span class="hljs-keyword" style="display: initial !important;">int</span> x = s / m, y = s % m;
<span class="hljs-keyword" style="display: initial !important;">int</span> tmp = grid[x][y];
<span class="hljs-keyword" style="display: initial !important;">while</span> (<span class="hljs-number" style="display: initial !important;">1</span>) {
<span class="hljs-keyword" style="display: initial !important;">int</span> nx = x, ny = y - k;
<span class="hljs-keyword" style="display: initial !important;">while</span> (ny < <span class="hljs-number" style="display: initial !important;">0</span>) {
nx = (nx - <span class="hljs-number" style="display: initial !important;">1</span> + n) % n;
ny += m;
}
grid[x][y] = grid[nx][ny];
<span class="hljs-keyword" style="display: initial !important;">if</span> (nx == s / m && ny == s % m) {
grid[x][y] = tmp;
<span class="hljs-keyword" style="display: initial !important;">break</span>;
}
x = nx; y = ny;
}
}
<span class="hljs-keyword" style="display: initial !important;">return</span> grid;
}
</code></pre>
<div>
<code style="display: initial !important;"><br /></code></div>
<div>
<code style="display: initial !important;"><a href="https://zxi.mytechroad.com/blog/simulation/leetcode-1260-shift-2d-grid/">https://zxi.mytechroad.com/blog/simulation/leetcode-1260-shift-2d-grid/</a></code></div>
<div>
<div>
<div>
Solution 2: Rotate</div>
<div>
Shift k times is equivalent to flatten the matrix and rotate by -k or (m*n – k % (m * n)).</div>
<div>
<br /></div>
<div>
Time complexity: O(m*n)</div>
<div>
Space complexity: O(m*n)</div>
<div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5cd988941481-4" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">shiftGrid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5df40a656a5cd988941481-5" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5cd988941481-6" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5cd988941481-7" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e " style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m *</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">%</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-e " style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m*</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5cd988941481-8" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">></span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">g</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5cd988941481-9" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5cd988941481-10" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">g</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">insert</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">end</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">g</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">begin</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">end</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5cd988941481-11" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">rotate</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">begin</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">g</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">begin</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">g</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">end</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">g</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5cd988941481-12" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">auto </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">it</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">begin</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">g</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5cd988941481-13" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5cd988941481-14" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line" id="crayon-5df40a656a5cd988941481-15" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">*</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">it</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5cd988941481-16" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5cd988941481-17" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
</div>
</div>
<div>
<br /></div>
<div>
<div>
O(1) space in-place rotation</div>
<div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-4" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">:</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-5" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-6" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-7" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-8" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">operator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-9" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">++</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-10" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">*</span><span class="crayon-r" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">this</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-11" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-12" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-13" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">operator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">--</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-14" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">--</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-15" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">*</span><span class="crayon-r" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">this</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-16" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-17" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-18" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">operator</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dis</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-19" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dis</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-20" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-21" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-22" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">operator</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dis</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-23" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">dis</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-24" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-25" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-26" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">bool</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">operator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">==</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-27" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">==</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">==</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-28" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-29" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-30" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">bool</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">operator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">!=</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-31" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">!=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">||</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">!=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-32" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-33" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-34" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ptrdiff_t </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">operator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-35" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-36" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-37" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-38" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">operator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-39" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-40" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">o</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-41" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">*</span><span class="crayon-r" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">this</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-42" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-43" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-44" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e " style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">operator*</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-45" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">if</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-46" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-47" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">else</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">if</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">*</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-48" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">back</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">back</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-49" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">else</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-50" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">/</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">%</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-51" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-52" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-53" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-54" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">private</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">:</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-55" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-56" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">==</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">?</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">:</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-57" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">data_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-58" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">index_</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-59" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-60" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
</div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-61" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">namespace</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">std</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-62" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">template</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><></span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-63" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">struct</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">iterator_traits</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">></span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-64" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-r" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">typedef</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">ptrdiff_t </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">difference_type</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-65" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-r" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">typedef</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">value_type</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-66" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-r" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">typedef</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">*</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">pointer</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-67" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-r" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">typedef</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">reference</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-68" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-r" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">typedef</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">random_access_iterator_tag </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">iterator_category</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-69" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-70" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-71" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
</div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-72" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">class</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">Solution</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-73" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">public</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">:</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-74" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">shiftGrid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-75" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-76" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-77" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e " style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m *</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">%</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-e " style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m*</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-78" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">MatrixIterator </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">it</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-79" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">rotate</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">it</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">it</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">it</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">+</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e " style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m *</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5d0844259901-80" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5d0844259901-81" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
</div>
</div>
<div>
<span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><br /></span></div>
<div>
<br /></div>
<div>
Solution 1: Simulation</div>
<div>
Simulate the shift process for k times.</div>
<div>
<br /></div>
<div>
Time complexity: O(k*n*m)</div>
<div>
Space complexity: O(1) in-place</div>
<div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5c1314754713-4" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">shiftGrid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">vector</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>></span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">,</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line" id="crayon-5df40a656a5c1314754713-5" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5c1314754713-6" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-m" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">const</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">.</span><span class="crayon-e" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">size</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5c1314754713-7" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">while</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">k</span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">--</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5c1314754713-8" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">last</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5c1314754713-9" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">n</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">--</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5c1314754713-10" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">for</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-t" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">int</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">>=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">--</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">{</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line" id="crayon-5df40a656a5c1314754713-11" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">if</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">==</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">&&</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">==</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5c1314754713-12" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">last</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5c1314754713-13" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">else</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">if</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">(</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">==</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">0</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">)</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5c1314754713-14" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">m</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5c1314754713-15" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">else</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5c1314754713-16" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">=</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">i</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">[</span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">j</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-o" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">-</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-cn" style="box-sizing: border-box; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">1</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">]</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line" id="crayon-5df40a656a5c1314754713-17" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5c1314754713-18" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span></div>
<div class="crayon-line" id="crayon-5df40a656a5c1314754713-19" style="background: 0px center rgb(255, 255, 255); border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-st" style="box-sizing: border-box; color: rgb(50, 21, 235) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">return</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-v" style="box-sizing: border-box; color: rgb(0, 45, 122) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">grid</span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">;</span></div>
<div class="crayon-line crayon-striped-line" id="crayon-5df40a656a5c1314754713-20" style="background: rgb(255, 255, 255) !important; border: 0px; box-sizing: border-box; font-family: "Liberation Mono", LiberationMonoRegular, "Courier New", monospace; font-size: 12px; height: inherit; line-height: inherit !important; margin: 0px; padding: 0px 5px; white-space: pre-wrap !important; word-break: break-all;">
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span><span class="crayon-sy" style="box-sizing: border-box; color: rgb(51, 51, 51) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;">}</span><span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"> </span></div>
</div>
</div>
<div>
<span class="crayon-h" style="box-sizing: border-box; color: rgb(0, 111, 224) !important; font-family: inherit; font-size: inherit !important; font-weight: inherit !important; height: inherit; line-height: inherit !important;"><br /></span></div>
<div>
<br /></div>
Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-72494418221304574492019-12-13T22:28:00.000-08:002019-12-13T22:28:35.570-08:00LeetCode 1284 - Minimum Number of Flips to Convert Binary Matrix to Zero Matrix<a href="https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/">https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/</a>,,<br />
<div class="content__u3I1 question-content__JfgR" style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin: 1em 0px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Given a <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m x n</code> binary matrix <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">mat</code>. In one step, you can choose one cell and flip it and all the four neighbours of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighboors if they share one edge.</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Return the <em style="box-sizing: border-box;">minimum number of steps</em> required to convert <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">mat</code> to a zero matrix or <span style="box-sizing: border-box; font-weight: bolder;">-1</span> if you cannot.</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Binary matrix is a matrix with all cells equal to 0 or 1 only.</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
Zero matrix is a matrix with all cells equal to 0.</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 1:</span></div>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/28/matrix.png" style="border-style: none; box-sizing: border-box; height: 86px; max-width: 100%; vertical-align: middle; width: 409px;" /><br />
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> mat = [[0,0],[0,1]]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 3
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span> One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 2:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> mat = [[0]]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 0
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span> Given matrix is a zero matrix. We don't need to change it.
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 3:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> mat = [[1,1,1],[1,0,1],[0,0,0]]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 6
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 4:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> mat = [[1,0,0],[1,0,0]]
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> -1
<span style="box-sizing: border-box; font-weight: bolder;">Explanation:</span> Given matrix can't be a zero matrix
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Constraints:</span></div>
<ul style="box-sizing: border-box; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m == mat.length</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n == mat[0].length</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= m <= 3</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= n <= 3</code></li>
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">mat[i][j]</code> is 0 or 1.</li>
</ul>
</div>
</div>
<a href="https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/446306/C%2B%2B-Bit-vector-%2B-Regular-BFS">https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/446306/C%2B%2B-Bit-vector-%2B-Regular-BFS</a><br />
<br />
<a href="https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/446342/JavaPython-3-Convert-matrix-to-int-then-BFS-w-explanation-comments-and-analysis.">https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/446342/JavaPython-3-Convert-matrix-to-int-then-BFS-w-explanation-comments-and-analysis.</a><br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<em style="box-sizing: border-box;">Q</em>: Could you please further explain about the logic of transferring the matrix to int?<br style="box-sizing: border-box;" /><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">sum(cell << (i * n + j) for i, row in enumerate(mat) for j, cell in enumerate(row))</code><br style="box-sizing: border-box;" />I wonder how it works and why it works.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<em style="box-sizing: border-box;">A</em>: For Input: mat = [[0,0],[0,1]], map it to <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0b1000</code>, that is, mapping mat[i][j] to the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">(i * n + j)</code>th bit of an int. specifically,<br style="box-sizing: border-box;" />mat[0][0] = 0, corresponds to <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0th</code> bit, which is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code>;<br style="box-sizing: border-box;" />mat[0][1] = 0, corresponds to <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1st</code> bit, which is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code>;<br style="box-sizing: border-box;" />mat[1][0] = 0, corresponds to <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2nd</code> bit, which is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code>;<br style="box-sizing: border-box;" />mat[1][1] = 1, corresponds to <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">3rd</code> bit, which is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1</code>;</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
After mapping, <span style="box-sizing: border-box; font-weight: bolder;">any operations on the binary cells of the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">mat</code> are equals to the operations on the corresponding bits of the mapped int</span>. That's why it works.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<em style="box-sizing: border-box;">Q</em>: Why do you use "|" to initialize the matrix and use "^" to calculate the next?</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<em style="box-sizing: border-box;">A</em>:</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<ol style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">When using <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0 |= b</code>, where <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">b = 0 or 1</code>, the result is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">b</code>; you can change it to</li>
</ol>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;">start += mat[<span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">i</span>][<span class="hljs-symbol" style="box-sizing: border-box; color: #1c00cf;">j</span>] <span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="box-sizing: border-box; color: #1c00cf;"><< (<span class="hljs-attr" style="box-sizing: border-box; color: #5c2699;">i</span> * <span class="hljs-attr" style="box-sizing: border-box; color: #5c2699;">n</span> + <span class="hljs-attr" style="box-sizing: border-box; color: #5c2699;">j</span>);
</span></span></code></pre>
<ol start="2" style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">Use <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">next ^ 1 << k</code> (where k = i * n + j) to flip <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">kth</code> bit of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">next</code>, which is equal to flipping the corresponding cell (i, j) in the matrix.</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">end of Q & A</span></div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<hr style="background-color: #eceff1; border-color: rgb(236, 239, 241); border-image: initial; border-style: solid; box-sizing: content-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; height: 0px; overflow: visible;" />
<ol style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">Since <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m < 3, n < 3</code> are given as constraints, there are at most <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">9</code> cells and an int has enough bits to store their values;</li>
<li style="box-sizing: border-box;">Map the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m * n</code> cells of the initial state of the matrix to the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code> ~ <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m * n - 1</code>th bits of an int: <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">start</code>;</li>
<li style="box-sizing: border-box;">For each one of the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">m * n</code> bits, flip it and its neighbors, then BFS to check if <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code>, corresponding to an all <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code>s matrix, is among the resulting states; if yes, return the minimum steps needed;</li>
<li style="box-sizing: border-box;">Use a Set to avoid duplicates;</li>
<li style="box-sizing: border-box;">If after the traversal of all states without finding <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code>, return <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">-1</code>.</li>
</ol>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">private</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">static</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">final</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[] d = {<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, -<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>};
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">minFlips</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span>[][] mat)</span> </span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> start = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>, m = mat.length, n = mat[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>].length;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < m; ++i)
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> j = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; j < n; ++j)
start |= mat[i][j] << (i * n + j); <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// convert the matrix to an int.</span>
Queue<Integer> q = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> LinkedList<>(Arrays.asList(start));
Set<Integer> seen = <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">new</span> HashSet<>(q);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> step = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; !q.isEmpty(); ++step) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> sz = q.size(); sz > <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; --sz) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> cur = q.poll();
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (cur == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// All 0s matrix found.</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> step;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < m; ++i) { <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// traverse all m * n bits of cur.</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> j = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; j < n; ++j) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> next = cur;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> (<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> k = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; k < <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">5</span>; ++k) { <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// flip the cell (i, j) and its neighbors.</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">int</span> r = i + d[k], c = j + d[k + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>];
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (r >= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> && r < m && c >= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> && c < n)
next ^= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> << (r * n + c);
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (seen.add(next)) <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// seen it before ?</span>
q.offer(next); <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// no, put it into the Queue.</span>
}
}
}
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> -<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>; <span class="hljs-comment" style="box-sizing: border-box; color: #006a00;">// impossible to get all 0s matrix.</span>
}
</code></pre>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">minFlips</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(self, mat: List[List[int]])</span> -> int:</span>
m, n = len(mat), len(mat[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>])
start = sum(cell << (i * n + j) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> i, row <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> enumerate(mat) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> j, cell <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> enumerate(row))
dq = collections.deque([(start, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>)])
seen = {start}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">while</span> dq:
cur, step = dq.popleft()
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">not</span> cur:
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> step
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> i <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> range(m):
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> j <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> range(n):
next = cur
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> r, c <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> (i, j), (i, j + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>), (i, j - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>), (i + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, j), (i - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, j):
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> m > r >= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span> <= c < n:
next ^= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> << (r * n + c)
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> next <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">not</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> seen:
seen.add(next)
dq.append((next, step + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>))
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px;">
<span style="box-sizing: border-box; font-weight: bolder;">Analysis:</span><br style="box-sizing: border-box;" />Time & Space: <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">O(2 ^ (m * n))</code></div>
<br />
X.<br />
<a href="https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/446371/Java-Recursion-%2B-Memoization-Explained">https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/446371/Java-Recursion-%2B-Memoization-Explained</a><br />
<h5 style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: unset; font-weight: 500; margin-bottom: 0.5em; margin-top: 0px;">
Recursion + Memoization</h5>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
In this problem, we can think in a <span style="box-sizing: border-box; font-weight: bolder;">brute manner first.</span><br style="box-sizing: border-box;" />We will try to flip the element at each index of the 2D matrix once and let recursion get the answer for the flipped array.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">Now while doing this we need to take care that we don't get trapped in a cycle (I have taken care of it by using a hashset).</li>
<li style="box-sizing: border-box;">For eg consider the 2D matrix given below.<br style="box-sizing: border-box;" />[1 0]<br style="box-sizing: border-box;" />[1 0]</li>
<li style="box-sizing: border-box;">Initially the call will be made by flipping the (0, 0) indexed element and its neighbors according to what is given in question.</li>
<li style="box-sizing: border-box;">The 2D matrix changes to :-<br style="box-sizing: border-box;" />[0 1]<br style="box-sizing: border-box;" />[0 0]</li>
<li style="box-sizing: border-box;">Now this configuration of the 2D array will make a recursive call by flipping the element at index (0, 0) and its neighbors. This will again give us an array configuration we had seen before in the same recursive branh (a cycle).<br style="box-sizing: border-box;" />[1 0]<br style="box-sizing: border-box;" />[1 0]</li>
<li style="box-sizing: border-box;">To avoid cycles i have used a set.<br style="box-sizing: border-box;" />Since the constraints of the problem are very weak, so using a string to memoize the state would not cost much on the runtime though concatenating strings is expensive.<br style="box-sizing: border-box;" />In this problem I have tried all possibilities of flipping all elements in a 2D array and made recursive calls to the resulting configurations. The recursive branch which brings the minimum no of steps will be a part of the solution.<br style="box-sizing: border-box;" />In the base case we check if all elements in the array are 0 or not.<br style="box-sizing: border-box;" />(BFS is another way to come up with a solution in which you will be needing a visited array(to avoid cycle) and a queue).</li>
</ul>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">class</span> <span class="hljs-symbol" style="box-sizing: border-box; color: #1c00cf;">Solution</span> {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> static <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">bool</span>ean check(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>[][] mat, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> n, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> m){
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < n; i++){
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> j = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; j < m; j++){
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(mat[i][j] == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">false</span>;
}
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-literal" style="box-sizing: border-box; color: #aa0d91;">true</span>;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> static <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">void</span> flip(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>[][] mat, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> n, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> m, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> i, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> j){
mat[i][j] = mat[i][j] ^ <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(i - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> >= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) mat[i - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>][j] = mat[i - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>][j] ^ <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(j - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> >= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>) mat[i][j - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>] = mat[i][j - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>] ^ <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(i + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> < n) mat[i + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>][j] = mat[i + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>][j] ^ <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(j + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> < m) mat[i][j + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>] = mat[i][j + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>] ^ <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> static <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> func(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>[][] mat, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> n, <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> m, HashSet<String> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">set</span>, HashMap<String, Integer> dp){
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(check(mat, n, m)) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>;
String t = <span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">""</span>;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < n; i++){
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> j = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; j < m; j++){
t += Integer.toString(mat[i][j]);
}
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(dp.containsKey(t)) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> dp.<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">get</span>(t);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">set</span>.contains(t)) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> Integer.MAX_VALUE;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">set</span>.add(t);
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> min = Integer.MAX_VALUE;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> i = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; i < n; i++){
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span>(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> j = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>; j < m; j++){
flip(mat, n, m, i, j);
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> small = func(mat, n, m, <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">set</span>, dp);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span>(small != Integer.MAX_VALUE) min = Math.min(min, <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> + small);
flip(mat, n, m, i, j);
}
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">set</span>.remove(t);
dp.put(t, min);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> min;
}
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> minFlips(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>[][] mat) {
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> n = mat.length, m = mat[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>].length;
HashMap<String, Integer> dp = new HashMap<>();
<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> ans = func(mat, n, m, new HashSet<>(), dp);
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> ans == Integer.MAX_VALUE ? <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span> : ans;
}</code></pre>
<br />
x. <a href="https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/446456/Beat-100-No-NP-hard-O((nm)3)-linear-algebra-solution">https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/446456/Beat-100-No-NP-hard-O((nm)3)-linear-algebra-solution</a><br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
This is a simplified version of a google phone screen posted below:<br style="box-sizing: border-box;" /><a href="https://leetcode.com/discuss/interview-question/422725/google-phone-screen-lights-out-puzzle/397815" style="background-color: transparent; border-bottom: 1px solid transparent; box-sizing: border-box; color: #607d8b; cursor: pointer; outline: none; padding-bottom: 1px; pointer-events: auto; text-decoration-line: none; touch-action: manipulation; transition: border-bottom-color 0.3s ease 0s;">https://leetcode.com/discuss/interview-question/422725/google-phone-screen-lights-out-puzzle/397815</a></div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
The simplification here mainly refers to the fairly small constraints. With m and n both <=3, we can come up with brute force exponential solution.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
But this is actually not a NP-hard problem in maths. Some good explanation can be found below:<br style="box-sizing: border-box;" /><a href="https://www.youtube.com/watch?v=oCHCD_-nhg4" style="background-color: transparent; border-bottom: 1px solid transparent; box-sizing: border-box; color: #607d8b; cursor: pointer; outline: none; padding-bottom: 1px; pointer-events: auto; text-decoration-line: none; touch-action: manipulation; transition: border-bottom-color 0.3s ease 0s;">https://www.youtube.com/watch?v=oCHCD_-nhg4</a><br style="box-sizing: border-box;" /><a href="http://mathworld.wolfram.com/LightsOutPuzzle.html" style="background-color: transparent; border-bottom: 1px solid transparent; box-sizing: border-box; color: #607d8b; cursor: pointer; outline: none; padding-bottom: 1px; pointer-events: auto; text-decoration-line: none; touch-action: manipulation; transition: border-bottom-color 0.3s ease 0s;">http://mathworld.wolfram.com/LightsOutPuzzle.html</a></div>
<div>
<br /></div>
<div>
<br /></div>
<a href="https://www.acwing.com/solution/LeetCode/content/6856/">https://www.acwing.com/solution/LeetCode/content/6856/</a><br />
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(枚举第一行) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo>&#x22C5;</mo><msup><mn>2</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-6-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo>⋅</mo><msup><mn>2</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">我们发现,如果确定了第一行(或第一列)反转哪些位置,则之后反转的位置就都可以确定。</li>
<li style="line-height: 1.5em;">假设我们已经反转了第一行的某些位置,我们从第二行开始一直到最后一行,如果发现当前位置的上一行是 1,则当前位置需要进行反转。我们在之后每一行,都去填上一行留下的坑。</li>
<li style="line-height: 1.5em;">最后判断下最后一行是否都已经变成了 0。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">仅需要枚举第一行的方案,然后进行整体的判断,故时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo>&#x22C5;</mo><msup><mn>2</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-7-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo>⋅</mo><msup><mn>2</mn><mi>n</mi></msup><mo stretchy="false">)</mo></math></span></span>。</li>
<li style="line-height: 1.5em;">如果列数较小,行数较大,则可以枚举第一列。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">需要额外 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-8-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的空间记录中间过程。</li>
</ul>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> <span class="hljs-title" style="display: initial !important;">minFlips</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>>& mat)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">int</span> m = mat.size(), n = mat[<span class="hljs-number" style="display: initial !important;">0</span>].size();
<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">int</span> dx[<span class="hljs-number" style="display: initial !important;">5</span>] = {<span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">1</span>, <span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">-1</span>};
<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">int</span> dy[<span class="hljs-number" style="display: initial !important;">5</span>] = {<span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">1</span>, <span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">-1</span>, <span class="hljs-number" style="display: initial !important;">0</span>};
<span class="hljs-keyword" style="display: initial !important;">int</span> ans = m * n + <span class="hljs-number" style="display: initial !important;">1</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> S = <span class="hljs-number" style="display: initial !important;">0</span>; S < (<span class="hljs-number" style="display: initial !important;">1</span> << n); S++) {
<span class="hljs-keyword" style="display: initial !important;">int</span> tot = <span class="hljs-number" style="display: initial !important;">0</span>;
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>> tmp(m, <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>(n, <span class="hljs-number" style="display: initial !important;">0</span>));
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> j = <span class="hljs-number" style="display: initial !important;">0</span>; j < n; j++)
<span class="hljs-keyword" style="display: initial !important;">if</span> ((S >> j) & <span class="hljs-number" style="display: initial !important;">1</span>) {
tot++;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> k = <span class="hljs-number" style="display: initial !important;">0</span>; k < <span class="hljs-number" style="display: initial !important;">5</span>; k++) {
<span class="hljs-keyword" style="display: initial !important;">int</span> tx = <span class="hljs-number" style="display: initial !important;">0</span> + dx[k], ty = j + dy[k];
<span class="hljs-keyword" style="display: initial !important;">if</span> (tx < <span class="hljs-number" style="display: initial !important;">0</span> || tx >= m || ty < <span class="hljs-number" style="display: initial !important;">0</span> || ty >= n)
<span class="hljs-keyword" style="display: initial !important;">continue</span>;
tmp[tx][ty] ^= <span class="hljs-number" style="display: initial !important;">1</span>;
}
}
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">1</span>; i < m; i++)
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> j = <span class="hljs-number" style="display: initial !important;">0</span>; j < n; j++)
<span class="hljs-keyword" style="display: initial !important;">if</span> (mat[i - <span class="hljs-number" style="display: initial !important;">1</span>][j] ^ tmp[i - <span class="hljs-number" style="display: initial !important;">1</span>][j]) {
tot++;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> k = <span class="hljs-number" style="display: initial !important;">0</span>; k < <span class="hljs-number" style="display: initial !important;">5</span>; k++) {
<span class="hljs-keyword" style="display: initial !important;">int</span> tx = i + dx[k], ty = j + dy[k];
<span class="hljs-keyword" style="display: initial !important;">if</span> (tx < <span class="hljs-number" style="display: initial !important;">0</span> || tx >= m || ty < <span class="hljs-number" style="display: initial !important;">0</span> || ty >= n)
<span class="hljs-keyword" style="display: initial !important;">continue</span>;
tmp[tx][ty] ^= <span class="hljs-number" style="display: initial !important;">1</span>;
}
}
<span class="hljs-keyword" style="display: initial !important;">bool</span> flag = <span class="hljs-literal" style="display: initial !important;">true</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> j = <span class="hljs-number" style="display: initial !important;">0</span>; j < n; j++)
<span class="hljs-keyword" style="display: initial !important;">if</span> (mat[m - <span class="hljs-number" style="display: initial !important;">1</span>][j] ^ tmp[m - <span class="hljs-number" style="display: initial !important;">1</span>][j]) {
flag = <span class="hljs-literal" style="display: initial !important;">false</span>;
<span class="hljs-keyword" style="display: initial !important;">break</span>;
}
<span class="hljs-keyword" style="display: initial !important;">if</span> (flag)
ans = min(ans, tot);
}
<span class="hljs-keyword" style="display: initial !important;">if</span> (ans == m * n + <span class="hljs-number" style="display: initial !important;">1</span>)
ans = <span class="hljs-number" style="display: initial !important;">-1</span>;
<span class="hljs-keyword" style="display: initial !important;">return</span> ans;
}
</code></pre>
<div>
<code style="display: initial !important;"><br /></code></div>
<h5 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif;">
(暴力枚举) <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo>&#x22C5;</mo><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mi>m</mi><mi>n</mi></mrow></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-1-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo>⋅</mo><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mi>m</mi><mi>n</mi></mrow></msup><mo stretchy="false">)</mo></math></span></span></h5>
<ol style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">暴力枚举哪些位置需要反转,一共有 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mi>m</mi><mi>n</mi></mrow></msup></math>" id="MathJax-Element-2-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mi>m</mi><mi>n</mi></mrow></msup></math></span></span> 种方案。</li>
<li style="line-height: 1.5em;">对于每种方案,验证最终是否为全 0。</li>
<li style="line-height: 1.5em;">找到反转次数最少的方案。</li>
</ol>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
时间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">验证需要 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-3-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的时间复杂度,故总时间复杂度为 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo>&#x22C5;</mo><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mi>m</mi><mi>n</mi></mrow></msup><mo stretchy="false">)</mo></math>" id="MathJax-Element-4-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo>⋅</mo><msup><mn>2</mn><mrow class="MJX-TeXAtom-ORD"><mi>m</mi><mi>n</mi></mrow></msup><mo stretchy="false">)</mo></math></span></span>。</li>
</ul>
<h4 style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
空间复杂度</h4>
<ul style="background-color: #222233; color: #d1d1d1; font-family: Roboto, system-ui, sans-serif; font-size: 16px;">
<li style="line-height: 1.5em;">需要额外 <span class="MathJax_Preview"></span><span class="MathJax" data-mathml="<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo stretchy="false">)</mo></math>" id="MathJax-Element-5-Frame" role="presentation" tabindex="0"><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>O</mi><mo stretchy="false">(</mo><mi>m</mi><mi>n</mi><mo stretchy="false">)</mo></math></span></span> 的空间,可以通过恢复现场来省略掉这部分空间</li>
</ul>
<pre class="hljs cpp" style="background-color: #e0e0e0; color: #1f1f1f; font-size: 16px; overflow: auto; padding: 10px;"><code style="display: initial !important;"> <span class="hljs-function" style="display: initial !important;"><span class="hljs-keyword" style="display: initial !important;">int</span> <span class="hljs-title" style="display: initial !important;">minFlips</span><span class="hljs-params" style="display: initial !important;">(<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>>& mat)</span> </span>{
<span class="hljs-keyword" style="display: initial !important;">int</span> m = mat.size(), n = mat[<span class="hljs-number" style="display: initial !important;">0</span>].size();
<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">int</span> dx[<span class="hljs-number" style="display: initial !important;">5</span>] = {<span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">1</span>, <span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">-1</span>};
<span class="hljs-keyword" style="display: initial !important;">const</span> <span class="hljs-keyword" style="display: initial !important;">int</span> dy[<span class="hljs-number" style="display: initial !important;">5</span>] = {<span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">1</span>, <span class="hljs-number" style="display: initial !important;">0</span>, <span class="hljs-number" style="display: initial !important;">-1</span>, <span class="hljs-number" style="display: initial !important;">0</span>};
<span class="hljs-keyword" style="display: initial !important;">int</span> ans = m * n + <span class="hljs-number" style="display: initial !important;">1</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> S = <span class="hljs-number" style="display: initial !important;">0</span>; S < (<span class="hljs-number" style="display: initial !important;">1</span> << m * n); S++) {
<span class="hljs-keyword" style="display: initial !important;">int</span> tot = <span class="hljs-number" style="display: initial !important;">0</span>;
<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>> tmp(m, <span class="hljs-built_in" style="display: initial !important;">vector</span><<span class="hljs-keyword" style="display: initial !important;">int</span>>(n, <span class="hljs-number" style="display: initial !important;">0</span>));
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i < m * n; i++)
<span class="hljs-keyword" style="display: initial !important;">if</span> ((S >> i) & <span class="hljs-number" style="display: initial !important;">1</span>) {
tot++;
<span class="hljs-keyword" style="display: initial !important;">int</span> x = i / n, y = i % n;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> j = <span class="hljs-number" style="display: initial !important;">0</span>; j < <span class="hljs-number" style="display: initial !important;">5</span>; j++) {
<span class="hljs-keyword" style="display: initial !important;">int</span> tx = x + dx[j], ty = y + dy[j];
<span class="hljs-keyword" style="display: initial !important;">if</span> (tx < <span class="hljs-number" style="display: initial !important;">0</span> || tx >= m || ty < <span class="hljs-number" style="display: initial !important;">0</span> || ty >= n)
<span class="hljs-keyword" style="display: initial !important;">continue</span>;
tmp[tx][ty] ^= <span class="hljs-number" style="display: initial !important;">1</span>;
}
}
<span class="hljs-keyword" style="display: initial !important;">bool</span> flag = <span class="hljs-literal" style="display: initial !important;">true</span>;
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> i = <span class="hljs-number" style="display: initial !important;">0</span>; i < m && flag; i++)
<span class="hljs-keyword" style="display: initial !important;">for</span> (<span class="hljs-keyword" style="display: initial !important;">int</span> j = <span class="hljs-number" style="display: initial !important;">0</span>; j < n && flag; j++) {
<span class="hljs-keyword" style="display: initial !important;">if</span> (tmp[i][j] ^ mat[i][j])
flag = <span class="hljs-literal" style="display: initial !important;">false</span>;
}
<span class="hljs-keyword" style="display: initial !important;">if</span> (flag)
ans = min(ans, tot);
}
<span class="hljs-keyword" style="display: initial !important;">if</span> (ans == m * n + <span class="hljs-number" style="display: initial !important;">1</span>)
ans = <span class="hljs-number" style="display: initial !important;">-1</span>;
<span class="hljs-keyword" style="display: initial !important;">return</span> ans;
}
</code></pre>
<div>
<code style="display: initial !important;"><br /></code></div>
<br />
<br />
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.comtag:blogger.com,1999:blog-5621339971161142944.post-28188940149902857962019-11-04T22:41:00.003-08:002019-11-04T22:41:57.224-08:00LeetCode 1227 - Airplane Seat Assignment Probability<a href="https://leetcode.com/problems/airplane-seat-assignment-probability/">https://leetcode.com/problems/airplane-seat-assignment-probability/</a><br />
<div class="content__u3I1 question-content__JfgR" style="background-color: white; box-sizing: border-box; color: #263238; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin: 1em 0px;">
<div style="box-sizing: border-box;">
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<code data-stringify-type="code" style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code> passengers board an airplane with exactly <code data-stringify-type="code" style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code> seats. The first passenger has lost the ticket and picks a seat randomly. But after that, the rest of passengers will:</div>
<ul style="box-sizing: border-box; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">Take their own seat if it is still available, </li>
<li style="box-sizing: border-box;">Pick other seats randomly when they find their seat occupied </li>
</ul>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
What is the probability that the n-th person can get his own seat?</div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 1:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> n = 1
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 1.00000
<span style="box-sizing: border-box; font-weight: bolder;">Explanation: </span>The first person can only get the first seat.</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Example 2:</span></div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px; white-space: pre-wrap;"><span style="box-sizing: border-box; font-weight: bolder;">Input:</span> n = 2
<span style="box-sizing: border-box; font-weight: bolder;">Output:</span> 0.50000
<span style="box-sizing: border-box; font-weight: bolder;">Explanation: </span>The second person has a probability of 0.5 to get the second seat (when first person gets the first seat).
</pre>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<br /></div>
<div style="box-sizing: border-box; font-size: inherit; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Constraints:</span></div>
<ul style="box-sizing: border-box; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 <= n <= 10^5</code></li>
</ul>
</div>
</div>
<a href="https://leetcode.com/problems/airplane-seat-assignment-probability/discuss/407781/Proof-by-mathematical-induction-that-answer-is-12-when-n-greater-2.">https://leetcode.com/problems/airplane-seat-assignment-probability/discuss/407781/Proof-by-mathematical-induction-that-answer-is-12-when-n-greater-2.</a><br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Q: Say if there are <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code> passengers and the first passenger took the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">3rd</code> seat. Now, like you explained, there are <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n - 1</code> passengers and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n - 1</code> seats left. But when the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2nd</code> passenger comes in, he <em style="box-sizing: border-box;">doesn't</em> have <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2</code> options to make it possible for the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nth</code> passenger to take the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nth</code> seat. Instead, he only has one option, which is to take the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2nd</code> seat because it is not occupied by the first passenger. I don't see how that is the case of a subproblem of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">(n - 1)</code>. Could you shed some light please, thanks!<br style="box-sizing: border-box;" />A: For any case, we can get rid of those sitting on own seats (except the first passenger) and get a problem of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n'</code> (= <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n - k</code>, where <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">k</code> is the number of passengers sitting on own seats), then re-number (without changing the relative order) them as passenger <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1, 2, ..., n'</code>, hence the result is in same form, the only difference is to change <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code> to <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n'</code>.<br style="box-sizing: border-box;" />Except <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n' = 1</code>, results for <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n'</code> of other values are independent on <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n'</code>. In short, changing from <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n</code> to <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n'</code> will not influence the result.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<hr style="background-color: #eceff1; border-color: rgb(236, 239, 241); border-image: initial; border-style: solid; box-sizing: content-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; height: 0px; overflow: visible;" />
<h1 style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: unset; font-weight: 500; margin: 0px 0px 0.5em;">
Part 1: [Java] 2 liners w/ explanation and analysis.</h1>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
For the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1st</code> passenger, there are 2 cases that the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nth</code> passenger could take the right seat:<br style="box-sizing: border-box;" /><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1st</code> passenger</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<ol style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">Take his own seat, the probability is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 / n</code>;</li>
<li style="box-sizing: border-box;">Take a seat neither his own nor the one of the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nth</code> passenger, and the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">corresponding</code> probability is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">(n - 2) / n</code>; In addition, other passengers (except the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nth</code> one) should not occupy the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nth</code> seat;<br style="box-sizing: border-box;" />Now there are <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n - 1</code> passengers and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n - 1</code> seats remaining, and the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2nd</code> passenger, like the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1st</code> one, have <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2</code> options to make it possible the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nth</code> passenger take the right seat:<br style="box-sizing: border-box;" />a) take the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1st</code> passenger's seat, the probability is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1 / (n - 1)</code>;<br style="box-sizing: border-box;" />b) Take a seat that is neither the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1st</code> passenger's nor the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">nth</code> passenger's, and the <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">corresponding</code> probability is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">((n - 1) - 2) /( n - 1)</code>;<br style="box-sizing: border-box;" />Obviouly, we recurse to subproblem of <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">(n - 1)</code> .</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Combined the above <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2</code> cases, we have the following code:</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">double</span> nthPersonGetsNthSeat(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> n) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> (n == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1.0</span>d;
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>d / n + (n - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>d) / n * nthPersonGetsNthSeat(n - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>);
}
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
<span style="box-sizing: border-box; font-weight: bolder;">Analysis</span><br style="box-sizing: border-box;" />Time: O(n), space: O(1).</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<hr style="background-color: #eceff1; border-color: rgb(236, 239, 241); border-image: initial; border-style: solid; box-sizing: content-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; height: 0px; overflow: visible;" />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Based on the code in part 1, we have the following formula:</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;">f(<span class="hljs-name" style="box-sizing: border-box; color: #000088;">n</span>) = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> / n + (<span class="hljs-name" style="box-sizing: border-box; color: #000088;">n</span> - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>) / n * f(<span class="hljs-name" style="box-sizing: border-box; color: #000088;">n</span> - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>)
</code></pre>
<h1 style="background-color: white; box-sizing: border-box; color: rgba(0, 0, 0, 0.85); font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: unset; font-weight: 500; margin: 0px 0px 0.5em;">
Part2: Proof when n > 1, the f(n) is 1/2</h1>
<ol style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;"><code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n = 2</code>, we have <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">f(2) = 1/2</code>; the assumption holds;</li>
<li style="box-sizing: border-box;">Suppose <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n = k</code> we have <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">f(k) = 1/2</code>, when <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n = k + 1</code>,</li>
</ol>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;">f(k + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> / (k + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) + (k + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>) / (k + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) * f(k)
= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span> / (<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span> * (k + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>)) + (k - <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) / (k + <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) * <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>/<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>
= <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> / <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">2</span>
</code></pre>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
That is, <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">f(k + 1) = 1 / 2</code> also holds.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
From above 1 and 2, we complete the proof.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<hr style="background-color: #eceff1; border-color: rgb(236, 239, 241); border-image: initial; border-style: solid; box-sizing: content-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; height: 0px; overflow: visible;" />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
With the conclusion, it is easy to have 1 liners for Java and Python 3:</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">public</span> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">double</span> nthPersonGetsNthSeat(<span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span> n) {
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> n == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> ? <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1.0</span>d : <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">.5</span>d;
}
</code></pre>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"> def nthPersonGetsNthSeat(self, n: <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">int</span>) -> <span class="hljs-built_in" style="box-sizing: border-box; color: #5c2699;">float</span>:
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1.0</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">if</span> n == <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">else</span> <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0.5</span></code></pre>
X.<a href="https://leetcode.com/problems/airplane-seat-assignment-probability/discuss/407533/Python-from-O(n)-to-O(1)-with-detailed-explanation">https://leetcode.com/problems/airplane-seat-assignment-probability/discuss/407533/Python-from-O(n)-to-O(1)-with-detailed-explanation</a><br />
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Each round we have 3 choices:</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<ol style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em; margin-top: 0px;">
<li style="box-sizing: border-box;">the 1st person gets his/her own seat. (with probability <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1/n</code>). Then the n-th person is sure (with probability <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1</code>) to get the n-th seat.</li>
<li style="box-sizing: border-box;">the 1st person gets the n-th person's seat. (with probability <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1/n</code>). Then the n-th person cannot (with probability <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">0</code>) get the n-th seat.</li>
<li style="box-sizing: border-box;">the 1st person gets a seat between <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">2</code> and <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">n-1</code> (with probability <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">(n-2)/n</code>). Assume the 1st person gets a-th seat. Then in the next round, we have 3 choices again:<br style="box-sizing: border-box;" />3.1) if the a-th person gets 1st seat (with probability <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1/(n-1)</code>), then this is just like 1st and a-th person swap their seats, it never affect our result for the n-th person.<br style="box-sizing: border-box;" />3.2) if the a-th person gets n-th seat (with probability <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1/(n-1)</code>), game over.<br style="box-sizing: border-box;" />3.3) if the a-th person gets a seat which is not 1st or n-th, (with probability <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">(n-1-2)/(n-1)</code>), we jump into a loop.</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
Therefore the dp pattern is <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">dp[i] = 1.0 / (i+1) + 0.0 / (i+1) + dp[i-1] * (i-1) / (i+1)</code>, with <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">dp[0]=1</code> for the case there's only one person. If you manually calculate it you'll find <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">dp[i]</code> is always <code style="background-color: #f7f9fa; border-radius: 3px; box-sizing: border-box; color: #546e7a; font-size: 13px; padding: 2px 4px;">1/2</code> except the base condition.</div>
<div style="background-color: white; box-sizing: border-box; color: #424242; font-family: -apple-system, system-ui, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 14px; margin-bottom: 1em;">
</div>
<pre style="background: rgb(247, 249, 250); border-radius: 3px; box-sizing: border-box; color: #263238; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13px; line-height: 1.6; margin-bottom: 1em; overflow: auto; padding: 10px 15px;"><code style="background-color: transparent; border-radius: 3px; box-sizing: border-box; color: inherit; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; padding: 0px; tab-size: 4;"><span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: #5c2699;">Solution</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(object)</span>:</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">def</span> <span class="hljs-title" style="box-sizing: border-box; color: #1c00cf;">nthPersonGetsNthSeat</span><span class="hljs-params" style="box-sizing: border-box; color: #5c2699;">(self, n)</span>:</span>
<span class="hljs-string" style="box-sizing: border-box; color: #c41a16;">"""
:type n: int
:rtype: float
"""</span>
<span class="hljs-comment" style="box-sizing: border-box; color: #006a00;"># return 0.5 if n > 1 else 1.0</span>
dp = [<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>] * n
dp[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">0</span>] = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1.0</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">for</span> i <span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">in</span> xrange(<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>, n):
dp[i] = <span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1.0</span> / (i+<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>) + dp[i<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>] * (i<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>) / (i+<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">1</span>)
<span class="hljs-keyword" style="box-sizing: border-box; color: #aa0d91;">return</span> dp[<span class="hljs-number" style="box-sizing: border-box; color: #1c00cf;">-1</span>]</code></pre>
<br />
<a href="https://www.cnblogs.com/onePunchCoder/p/11699121.html">https://www.cnblogs.com/onePunchCoder/p/11699121.html</a><br />
<div style="background-color: #fefef2; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
n个用户依次登机就坐。第一个用户丢失了机票,将会随机选取一个座位,之后的乘客优先坐自己的座位,如果自己座位被占了则随机找寻一个座位就坐,求问第n个用户得到自己座位的概率。</div>
<div>
<div style="font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
2、分析与证明</div>
<ul style="margin: 0px 0px 0px 30px; padding: 0px; word-break: break-all;">
<li style="list-style: disc; margin: 0px 0px 1em; padding: 0px;"><span style="font-size: 14px; line-height: 1.5; margin: 0px; padding: 0px;">假设有n个用户,本问题的答案为 f(n)。</span></li>
</ul>
<ul style="margin: 0px 0px 0px 30px; padding: 0px; word-break: break-all;">
<li style="list-style: disc; margin: 0px 0px 1em; padding: 0px;"><span style="font-size: 14px; line-height: 1.5; margin: 0px; padding: 0px;">如果第一个人随机到了自己的位置,那么后面的人一定按自己机票座位号入座。</span></li>
</ul>
<ul style="margin: 0px 0px 0px 30px; padding: 0px; word-break: break-all;">
<li style="list-style: disc; margin: 0px 0px 1em; padding: 0px;"><span style="font-size: 14px; line-height: 1.5; margin: 0px; padding: 0px;">如果第一个人随机到了第n个人的位置,那么第 n 个人得到自己座位的概率为0。</span></li>
</ul>
<ul style="margin: 0px 0px 0px 30px; padding: 0px; word-break: break-all;">
<li style="list-style: disc; margin: 0px 0px 1em; padding: 0px;"><span style="font-size: 14px; line-height: 1.5; margin: 0px; padding: 0px;">如果第一个人随机到了第2个人的位置,那么第 n 个人得到自己座位的概率为f(n-1)。</span></li>
</ul>
<ul style="margin: 0px 0px 0px 30px; padding: 0px; word-break: break-all;">
<li style="list-style: disc; margin: 0px 0px 1em; padding: 0px;"><span style="font-size: 14px; line-height: 1.5; margin: 0px; padding: 0px;">依次类推可得 <em id="__mceDel" style="margin: 0px; padding: 0px;">f(n) = (1 + f(n-1) + f(n-2) + ... + f(2) + 0) / n ;</em></span></li>
</ul>
<ul style="margin: 0px 0px 0px 30px; padding: 0px; word-break: break-all;">
<li style="list-style: disc; margin: 0px 0px 1em; padding: 0px;"><span style="font-size: 14px; line-height: 1.5; margin: 0px; padding: 0px;">假设当 1< i <= k 时 f(i) = 1/2 , 容易证明f(k+1) = 1/2; 所以f(n) 在n > 1的时候恒等于 1/2 .</span></li>
</ul>
<div style="font-size: 13px; line-height: 1.5; margin: 10px auto; padding: 0px;">
<span style="font-size: 14px; line-height: 1.5; margin: 0px; padding: 0px;">论证过程的代码实现如下</span></div>
<div class="cnblogs_code" style="background-color: whitesmoke; border: 1px solid rgb(204, 204, 204); font-family: "Courier New" !important; font-size: 12px !important; margin: 5px 0px; overflow: auto; padding: 5px;">
<pre style="font-family: "Courier New" !important; overflow-wrap: break-word; padding: 0px; white-space: pre-wrap;"><span style="font-size: 14px; line-height: 1.5; margin: 0px; padding: 0px;"><span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">public</span> <span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">double</span> nthPersonGetsNthSeat(<span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">int</span><span style="font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;"> n) {
</span><span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">if</span>(n==1) <span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">return</span> 1.0<span style="font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">double</span>[] dp = <span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">new</span> <span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">double</span><span style="font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">[n];
</span><span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">double</span> sum = 0<span style="font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">;
</span><span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">for</span> (<span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">int</span> i = 1; i < n; i++<span style="font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">) {
dp[i] </span>= (1 + sum) / (i + 1<span style="font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">);
sum </span>+=<span style="font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;"> dp[i];
}
</span><span style="color: blue; font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">return</span> dp[n - 1<span style="font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;">];
}</span></span></pre>
<div>
<span style="font-size: 14px; line-height: 1.5; margin: 0px; padding: 0px;"><span style="font-size: 12px !important; line-height: 1.5; margin: 0px; padding: 0px;"><br /></span></span></div>
</div>
</div>
<a href="https://www.acwing.com/solution/LeetCode/content/5445/">https://www.acwing.com/solution/LeetCode/content/5445/</a><br />
<br />
<br />
<br />Jefferyhttp://www.blogger.com/profile/03393573469730148487noreply@blogger.com