{"id":"house-robber-ii","title":"House Robber II","difficulty":"medium","patterns":["dp-1d"],"primaryConcept":"dp-1d-decision","concepts":["dp-1d-decision"],"problemForm":"core","companies":["Amazon","Google","Microsoft"],"category":"dynamic-programming","executionStyle":"solution-method","compareMode":"exact","syntheticTestConfig":{"inputShape":"array","primaryParam":"nums","sizeMetric":"length","constraints":["non-negative"],"valueRange":{"min":0,"max":1000},"sizes":[8,16,32,64]},"description":"You are given an array `nums` where `nums[i]` is the amount of money in the ith house. Houses are arranged in a circle, so the first and last houses are adjacent. Return the maximum amount you can rob without robbing adjacent houses.","examples":[{"input":"nums = [2,3,2]","output":"3","explanation":"Rob the middle house. The two houses with value 2 are adjacent in the circle."},{"input":"nums = [1,2,3,1]","output":"4","explanation":"Rob houses with values 1 and 3."},{"input":"nums = [1,2,3]","output":"3","explanation":"Rob only the house with value 3."}],"constraints":[{"id":"1-len-nums-100","text":"1 <= len(nums) <= 100","check":{"kind":"sizeRange","target":"nums","measure":"arrayLength","min":1,"max":100}},{"id":"0-nums-i-1000","text":"0 <= nums[i] <= 1000","check":{"kind":"valueRange","target":"nums","selector":"arrayItems","min":0,"max":1000}}],"starterCode":"class Solution:\n    def rob(self, nums: list[int]) -> int:\n        # Your code here\n        pass","functionName":"rob","testCases":[{"id":"1","input":{"nums":[2,3,2]},"expected":3,"description":"Cannot rob both circular endpoints."},{"id":"2","input":{"nums":[1,2,3,1]},"expected":4,"description":"Best plan excludes the last endpoint."},{"id":"3","input":{"nums":[1,2,3]},"expected":3,"description":"Choose the largest single non-adjacent option."},{"id":"4","input":{"nums":[1]},"expected":1,"description":"Single house edge case."},{"id":"5","input":{"nums":[200,3,140,20,10]},"expected":340,"description":"Best plan uses the first house and excludes the last."},{"id":"h1","input":{"nums":[2,7,9,3,1]},"expected":11,"description":"Classic line example adjusted for circular endpoints."},{"id":"h2","input":{"nums":[1,3,1,3,100]},"expected":103,"description":"Best plan excludes the first house."},{"id":"h3","input":{"nums":[5,1,1,5]},"expected":6,"description":"Both endpoint exclusions tie."},{"id":"h4","input":{"nums":[1,2]},"expected":2},{"id":"h5","input":{"nums":[2,1,1,2]},"expected":3},{"id":"topup-1","input":{"nums":[4,1,2,7,5,3,1]},"expected":14,"description":"Top-up case 1: circular exclusion allows the best interior combination without both endpoints."},{"id":"topup-2","input":{"nums":[10,1,1,10,1,1,10]},"expected":21,"description":"Top-up case 2: equal endpoint values cannot both be taken in the circular layout."}],"hints":[{"level":1,"content":"Because the first and last houses touch, a valid plan cannot include both.","revealPattern":false},{"level":2,"content":"Solve two linear House Robber problems: exclude the last house, or exclude the first house.","revealPattern":true},{"level":3,"content":"Use the same rolling recurrence as House Robber on each bounded range.","revealPattern":true}],"guidedFlow":{"version":2,"approachSelection":{"contract":"approach-selection-v1","question":"Which shape should we build?","decisionPrompt":"Choose the approach whose state and complexity match the constraints.","approaches":[{"id":"enumerate-subsets","name":"Enumerate every legal subset","summary":"List all house selections, throw out any that pick two neighbors, and keep the richest.","decisionReason":"The number of subsets doubles with each house, so a long street is hopeless.","complexity":{"time":"O(2^n)","space":"O(n)"},"tradeoffs":["Runs in O(2^n) time and uses O(n) space."],"isOptimal":false},{"id":"two-linear-dp","name":"Two rolling sweeps, one per dropped endpoint","summary":"Run the straight-row rolling recurrence twice, once dropping the last house and once dropping the first, then keep the larger result.","decisionReason":"Each sweep is linear with two rolling totals, and running two of them still costs O(n) time and O(1) space.","complexity":{"time":"O(n)","space":"O(1)"},"tradeoffs":["Runs in O(n) time and uses O(1) space."],"isOptimal":true}]},"codingCheckpoints":[{"id":"split-circle","title":"Split the circle into two rows","description":"Build two bounded views of the houses: one that drops the last house, one that drops the first.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"selects_opposite_endpoint_bounded_sequences","scope":"split-circle","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Solving the whole circle as one straight row","Dropping both endpoints in a single range"],"hint":"One range is houses 0 through n-2, the other is houses 1 through n-1.","semanticSpec":{"intent":"Create two bounded linear views of the circular input, each omitting one endpoint.","doneWhen":"The solution selects two bounded ranges that omit opposite endpoints.","shouldFailWhen":"This step should fail if the whole array is solved as one straight row without bounded ranges.","roles":["input houses","bounded ranges"],"mustProve":["The solution selects bounded reference sequences."],"mustNotAccept":["A single unbounded linear pass over all houses."]}},{"id":"init-rolling-states","title":"Initialize the two rolling totals","description":"Inside the linear helper, start the best-through-previous and best-through-two-back totals before scanning a row.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"initializes_recurrence_state","scope":"init-rolling-states","readMode":"dp-value-query-v1"}]},"commonMistakes":["Starting the rolling totals from arbitrary constants","Keeping only one rolling total"],"hint":"Each row needs the best through the previous house and the best through the one before that.","semanticSpec":{"intent":"Initialize recurrence state for the linear robbery helper.","doneWhen":"Two rolling recurrence states are initialized before scanning a row.","shouldFailWhen":"This step should fail if no recurrence state is initialized.","roles":["previous best","two-back best"],"mustProve":["The DP recurrence state is initialized."],"mustNotAccept":["A single running sum without recurrence state."]}},{"id":"scan-houses","title":"Scan one bounded row","description":"Walk every house in the current bounded row so the helper works for any slice.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"scans_indexed_state_space","scope":"iterate-houses","readMode":"dp-value-query-v1"}]},"commonMistakes":["Skipping the first house of a bounded row","Stopping before the row ends"],"hint":"The helper should loop over whatever slice it is handed.","semanticSpec":{"intent":"Scan the DP state space for one bounded row.","doneWhen":"The helper iterates through the bounded house values.","shouldFailWhen":"This step should fail if the row is not scanned.","roles":["bounded row","current house"],"mustProve":["The DP scans indexed state space."],"mustNotAccept":["A fixed two-house comparison."]}},{"id":"compute-choice","title":"Take the better of skip or rob","description":"At each house, compare keeping the previous best with adding this house to the best two back.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"computes_recurrence_transition","scope":"compute-next-states","readMode":"dp-value-query-v1"}]},"commonMistakes":["Adding the current house to the previous best instead of the two-back best","Dropping the skip option"],"hint":"The new best is max(previous best, two-back best + current amount).","semanticSpec":{"intent":"Compute the DP recurrence transition for one house.","doneWhen":"The next best value is derived from skip and take choices.","shouldFailWhen":"This step should fail if adjacent houses can both be included.","roles":["current house","skip total","take total"],"mustProve":["The solution computes a recurrence transition."],"mustNotAccept":["A simple running sum."]}},{"id":"write-rolling-state","title":"Slide the totals forward","description":"Write the new best back into the rolling totals so the next house reads fresh state.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"writes_recurrence_state","scope":"keep-better-outcome","readMode":"dp-value-query-v1"}]},"commonMistakes":["Computing the new best but never storing it","Sliding the two totals in the wrong order"],"hint":"After computing the new best, the previous best becomes two-back and the new best becomes previous.","semanticSpec":{"intent":"Persist recurrence progress between houses.","doneWhen":"The rolling totals advance using the computed recurrence value.","shouldFailWhen":"This step should fail if the recurrence value is not stored.","roles":["rolling totals","current best"],"mustProve":["The solution writes recurrence state."],"mustNotAccept":["A discarded recurrence calculation."]}},{"id":"return-best","title":"Return the better of the two rows","description":"Return the larger result of the two bounded linear runs as the circular answer.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"returns_indexed_terminal_state","scope":"return-final-best","readMode":"dp-value-query-v1"}]},"commonMistakes":["Returning the result of only one row","Returning a two-back total instead of the final best"],"hint":"The circular answer is the max of the two linear answers.","semanticSpec":{"intent":"Return the terminal DP outcome across both rows.","doneWhen":"The function returns the larger of the two completed linear results.","shouldFailWhen":"This step should fail if the returned value is not the final DP state.","roles":["linear DP result","circular answer"],"mustProve":["The solution returns indexed terminal state."],"mustNotAccept":["Returning an intermediate rolling total."]}}],"slides":[{"id":"read-the-signal","kind":"info","chapter":"The circle","eyebrow":"Read the problem","title":"Houses in a ring, no two neighbors","workspacePanel":"problem","body":["The houses sit in a circle, so house 0 and the last house are neighbors just like any other adjacent pair. You can rob any houses you like, as long as no two of them touch.","On a straight row this is a solved sweep: each house takes the larger of skipping it, or its money plus the best from two houses back. Watch what the ring does to that sweep."]},{"id":"reuse-line-sweep","kind":"code-trace","chapter":"The obvious way","eyebrow":"The obvious way","title":"Run the straight-row sweep on the whole ring","summary":"Running the recurrence we already trust on the whole ring keeps it linear, but the ends now touch. Watch which two houses the sweep robs together.","workspacePanel":"trace","body":["Two rolling totals slide along the houses, each one taking the larger of skipping the house or adding it to the best from two back."],"codeTrace":{"contract":"code-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"prev2 = 0\nprev1 = 0\nfor amount in nums:\n    current = max(prev1, prev2 + amount)\n    prev2 = prev1\n    prev1 = current\nreturn prev1","steps":[{"id":"start","title":"House 0 holds 5","codeLine":4,"summary":"nums = [5,1,1,5]. current = max(0, 0 + 5) = 5, so prev1 becomes 5.","state":{"nums":[5,1,1,5],"i":0,"prev2":0,"prev1":5}},{"id":"middle","title":"The two interior 1s barely move it","codeLine":6,"summary":"House 1 keeps 5, house 2 lifts it to max(5, 5 + 1) = 6. prev1 is 6, prev2 is 5.","state":{"nums":[5,1,1,5],"i":2,"prev2":5,"prev1":6}},{"id":"last","title":"House 3 holds the other 5","codeLine":4,"summary":"current = max(6, prev2 + 5) = max(6, 5 + 5) = 10. The sweep banks both 5s.","state":{"nums":[5,1,1,5],"i":3,"prev2":6,"prev1":10}},{"id":"return","title":"It returns 10","codeLine":7,"summary":"prev1 ends at 10, which is house 0 plus house 3.","state":{"nums":[5,1,1,5],"i":3,"prev2":6,"prev1":10}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"let prev2 = 0;\nlet prev1 = 0;\nfor (const amount of nums) {\n    const current = Math.max(prev1, (prev2 + amount));\n    prev2 = prev1;\n    prev1 = current;\n}\nreturn prev1;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8},"statementBundles":{"6":{"startLine":6,"endLine":7}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"let prev2 = 0;\nlet prev1 = 0;\nfor (const amount of nums) {\n    const current = Math.max(prev1, (prev2 + amount));\n    prev2 = prev1;\n    prev1 = current;\n}\nreturn prev1;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8},"statementBundles":{"6":{"startLine":6,"endLine":7}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"int prev2 = 0;\nint prev1 = 0;\nfor (Integer amount : nums) {\n    int current = Math.max(prev1, (prev2 + amount));\n    prev2 = prev1;\n    prev1 = current;\n}\nreturn prev1;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8},"statementBundles":{"6":{"startLine":6,"endLine":7}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"int prev2 = 0;\nint prev1 = 0;\nforeach (int amount in nums) {\n    int current = Math.Max(prev1, prev2 + amount);\n    prev2 = prev1;\n    prev1 = current;\n}\nreturn prev1;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8},"statementBundles":{"6":{"startLine":6,"endLine":7}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"int prev2 = 0;\nint prev1 = 0;\nfor (int amount : nums) {\n    int current = max(prev1, (prev2 + amount));\n    prev2 = prev1;\n    prev1 = current;\n}\nreturn prev1;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8},"statementBundles":{"6":{"startLine":6,"endLine":7}}}}}},{"id":"what-breaks","kind":"case-probe","chapter":"The obvious way","eyebrow":"Name the flaw","title":"Which two houses did the straight-row sweep just rob together?","summary":"The sweep returned 10 on [5,1,1,5].","workspacePanel":"visualizer","options":[{"id":"endpoints","label":"House 0 and house 3, the two ends, which are neighbors in the ring","rationale":"On a straight row the ends never touch, so the sweep happily takes both. In the ring they are adjacent, so banking both trips the alarm.","isCorrect":true},{"id":"middles","label":"The two interior 1s","rationale":"The 1s were never worth taking together; the sweep skipped past them. The 10 is the two 5s.","isCorrect":false},{"id":"none","label":"No two neighbors, the sweep is already correct","rationale":"The legal best here is 6. A result of 10 can only come from robbing both 5s, which are neighbors in the ring.","isCorrect":false}],"answer":"House 0 and house 3. They are the ends of the row, which never touch on a line, but in the ring they are neighbors, so the sweep overcounts by taking both.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"what-breaks-state","state":{"nums":[5,1,1,5],"i":3,"prev2":6,"prev1":10,"j":0,"labels":{"nums":"houses"}}}]}},{"id":"how-to-fix","kind":"case-probe","chapter":"The trick","eyebrow":"Find the fix","title":"House 0 and the last house cannot both be robbed. How does that turn the ring into a line?","summary":"Any valid ring plan either leaves out house 0, or leaves out the last house, or both.","workspacePanel":"visualizer","options":[{"id":"two-rows","label":"Solve two straight rows: one without the last house, one without the first, then keep the larger","rationale":"Dropping either endpoint breaks the ring open into a plain row the trusted sweep already handles. The best legal plan lives in one of those two rows.","isCorrect":true},{"id":"subtract","label":"Run the ring sweep, then subtract the smaller endpoint","rationale":"Subtracting a value does not undo an illegal pairing; the rest of the chosen plan may already depend on having taken both ends.","isCorrect":false},{"id":"rotate","label":"Rotate the array so the endpoints are no longer at the ends","rationale":"Rotation moves the seam but keeps it a ring; house 0 and the last house are still neighbors wherever they land.","isCorrect":false}],"answer":"Run two straight-row sweeps: houses 0..n-2 (drop the last) and houses 1..n-1 (drop the first). Each drops one endpoint, so neither row can pair the two neighbors, and the answer is the larger of the two.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"how-to-fix-state","state":{"nums":[5,1,1,5],"i":0,"labels":{"nums":"houses"}}}]}},{"id":"approach-selection","kind":"choice","source":"approach-selection","chapter":"The trick","eyebrow":"Pick the approach","title":"Which shape should we build?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"approach-structure","state":{"rows":[{"label":"drop last","values":[5,1,1]},{"label":"drop first","values":[1,1,5]}],"i":0}}]}},{"id":"dry-run-setup","kind":"info","chapter":"Trace","eyebrow":"Watch it move","title":"Two rows, one shared sweep","workspacePanel":"visualizer","body":["Take nums = [1, 2, 3, 1]. Dropping the last house leaves the row [1, 2, 3]; dropping the first leaves [2, 3, 1].","Run the same rolling sweep on each row, read its best, and keep the larger. Watch the two rows finish at different totals."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"dry-run-start","state":{"rows":[{"label":"drop last","values":[1,2,3]},{"label":"drop first","values":[2,3,1]}],"i":0}}]}},{"id":"trace","kind":"dry-run","source":"trace-example","chapter":"Trace","eyebrow":"Trace the two rows","title":"Sweep both rows of [1, 2, 3, 1]","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"row-drop-last","state":{"nums":[1,2,3],"i":2,"prev2":1,"prev1":2,"labels":{"nums":"houses"}}},{"id":"row-drop-first","state":{"nums":[2,3,1],"i":2,"prev2":2,"prev1":3,"labels":{"nums":"houses"}}},{"id":"combine","state":{"bestDropLast":4,"bestDropFirst":3,"result":4}}]},"beats":[{"id":"row-drop-last","title":"Dropping the last house, what is the best on [1, 2, 3]?","summary":"prev2 is 1 (best through house 0) and prev1 is 2 (best through house 1). House 2 holds 3.","prompt":"House 2 can pair with the best from two back.","options":[{"id":"four","label":"4, robbing the 1 and the 3","rationale":"Rob = prev2 + 3 = 1 + 3 = 4, which beats skipping (2). The best on this row is 4.","isCorrect":true},{"id":"five","label":"5, robbing the 2 and the 3","rationale":"Houses holding 2 and 3 are neighbors, so they cannot both be robbed.","isCorrect":false},{"id":"three","label":"3, robbing only the 3","rationale":"Robbing the 3 still leaves room for the 1 two houses back, lifting the total to 4.","isCorrect":false}],"answer":"Rob = 1 + 3 = 4 beats skip = 2, so dropping the last house gives 4."},{"id":"row-drop-first","title":"Dropping the first house, what is the best on [2, 3, 1]?","summary":"prev2 is 2 (best through the 2) and prev1 is 3 (best through the 3). The last value is 1.","prompt":"The final house in this row holds 1.","options":[{"id":"three","label":"3, robbing only the 3","rationale":"Rob = prev2 + 1 = 2 + 1 = 3, which ties skipping (3). Either way this row tops out at 3.","isCorrect":true},{"id":"six","label":"6, robbing the 2, the 3, and the 1","rationale":"The 2 and 3 are neighbors and the 3 and 1 are neighbors, so no plan takes all three.","isCorrect":false},{"id":"four","label":"4, robbing the 3 and the 1","rationale":"The 3 and the 1 sit side by side in this row, so banking both is illegal.","isCorrect":false}],"answer":"Rob = 2 + 1 = 3 ties skip = 3, so dropping the first house gives 3."},{"id":"combine","title":"What does the ring return?","summary":"Dropping the last house gave 4; dropping the first gave 3.","prompt":"The two rows finished at 4 and 3.","options":[{"id":"four","label":"4, the larger of the two rows","rationale":"The best legal plan lives in whichever row scored higher, and the drop-last row gave 4.","isCorrect":true},{"id":"seven","label":"7, the two rows added together","rationale":"The rows are two alternative worlds, not parts of one plan; adding them double counts houses.","isCorrect":false},{"id":"three","label":"3, the smaller of the two rows","rationale":"The circular answer keeps the larger legal row result, which is 4 here.","isCorrect":false}],"answer":"max(4, 3) = 4. The ring answer is the better of the two endpoint-dropping rows."}]},{"id":"same-endpoint-pitfall","kind":"pitfall","chapter":"Trace","eyebrow":"Spot the trap","title":"Could both rows just drop the last house?","summary":"Two sweeps, take the max: the two ranges look interchangeable, so reusing the drop-the-last-house row for both reads cleanly and still bans the two endpoints together. Watch it on a ring whose best plan needs the last house.","workspacePanel":"trace","pitfall":"The code below feeds the same drop-the-last-house row into both sweeps instead of dropping opposite endpoints.","whyItMatters":"Both rows are the drop-the-last-house row, so the final house is never in either sweep. Any ring whose best plan robs that last house is undercounted. The second row has to be the drop-the-first-house row instead, so the two rows together cover every legal plan.","negativeTrace":{"contract":"negative-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"def rob(nums: list[int]) -> int:\n    def rob_line(values: list[int]) -> int:\n        prev2 = 0\n        prev1 = 0\n        for amount in values:\n            current = max(prev1, prev2 + amount)\n            prev2 = prev1\n            prev1 = current\n        return prev1\n    row_a = rob_line(nums[:-1])\n    row_b = rob_line(nums[:-1])\n    return max(row_a, row_b)","steps":[{"id":"row-a","title":"Row A is the drop-last row [1, 3, 1]","codeLine":10,"summary":"On nums = [1, 3, 1, 3], the drop-last row is [1, 3, 1]. Mid-sweep at house 1 it already sits at 3, just the middle 3, and the last house keeps it there.","state":{"nums":[1,3,1],"i":1,"prev2":1,"prev1":3,"row":"A","labels":{"nums":"houses"}}},{"id":"row-b","title":"Row B reuses the same drop-last row","codeLine":11,"summary":"It sweeps [1, 3, 1] a second time and the finished sweep lands on the identical 3 at house 2. The last house, holding 3, is never in either row.","state":{"nums":[1,3,1],"i":2,"prev2":3,"prev1":3,"row":"B","labels":{"nums":"houses"}}},{"id":"wrong","title":"Returns 3, missing the last house","codeLine":12,"summary":"max(3, 3) = 3. The drop-first row [3, 1, 3] would have scored 6 by robbing both 3s.","state":{"rowA":3,"rowB":3,"result":3}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"function robLine(values) {\n    let prev2 = 0;\n    let prev1 = 0;\n    for (const amount of values) {\n        const current = Math.max(prev1, (prev2 + amount));\n        prev2 = prev1;\n        prev1 = current;\n    }\n    return prev1;\n}\nconst rowA = robLine(nums.slice(0, -1));\nconst rowB = robLine(nums.slice(0, -1));\nreturn Math.max(rowA, rowB);","lineMap":{"3":2,"4":3,"5":4,"6":5,"7":6,"8":7,"9":9,"10":11,"11":12,"12":13},"statementBundles":{"8":{"startLine":7,"endLine":8},"9":{"startLine":9,"endLine":10}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"function robLine(values: Array<number>): number {\n    let prev2 = 0;\n    let prev1 = 0;\n    for (const amount of values) {\n        const current = Math.max(prev1, (prev2 + amount));\n        prev2 = prev1;\n        prev1 = current;\n    }\n    return prev1;\n}\nconst rowA = robLine(nums.slice(0, -1));\nconst rowB = robLine(nums.slice(0, -1));\nreturn Math.max(rowA, rowB);","lineMap":{"3":2,"4":3,"5":4,"6":5,"7":6,"8":7,"9":9,"10":11,"11":12,"12":13},"statementBundles":{"8":{"startLine":7,"endLine":8},"9":{"startLine":9,"endLine":10}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"int RobLine(List<int> values) {\n    int prev2 = 0;\n    int prev1 = 0;\n    foreach (int amount in values) {\n        int current = Math.Max(prev1, prev2 + amount);\n        prev2 = prev1;\n        prev1 = current;\n    }\n    return prev1;\n    return 0;\n}\nint rowA = RobLine(nums.GetRange(0, Convert.ToInt32(Math.Max(0, nums.Count - 1))));\nint rowB = RobLine(nums.GetRange(0, Convert.ToInt32(Math.Max(0, nums.Count - 1))));\nreturn Math.Max(rowA, rowB);","lineMap":{"3":2,"4":3,"5":4,"6":5,"7":6,"8":7,"9":9,"10":12,"11":13,"12":14},"statementBundles":{"8":{"startLine":7,"endLine":8},"9":{"startLine":9,"endLine":11}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"auto rob_line = [&]( vector<int> values ) -> int {\n    int prev2 = 0;\n    int prev1 = 0;\n    for (int amount : values) {\n        int current = max(prev1, (prev2 + amount));\n        prev2 = prev1;\n        prev1 = current;\n    }\n    return prev1;\n};\nint row_a = rob_line(vector<int>(nums.begin() + 0, nums.begin() + max(0, static_cast<int>(nums.size()) - 1)));\nint row_b = rob_line(vector<int>(nums.begin() + 0, nums.begin() + max(0, static_cast<int>(nums.size()) - 1)));\nreturn max(row_a, row_b);","lineMap":{"3":2,"4":3,"5":4,"6":5,"7":6,"8":7,"9":9,"10":11,"11":12,"12":13},"statementBundles":{"8":{"startLine":7,"endLine":8},"9":{"startLine":9,"endLine":10}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"private static int robLine(List<Integer> values) {\n    int prev2 = 0;\n    int prev1 = 0;\n    for (Integer amount : values) {\n        int current = Math.max(prev1, (prev2 + amount));\n        prev2 = prev1;\n        prev1 = current;\n    }\n    return prev1;\n}\n\nint rowA = robLine(new ArrayList<Integer>(nums.subList(0, Math.max(0, nums.size() - 1))));\nint rowB = robLine(new ArrayList<Integer>(nums.subList(0, Math.max(0, nums.size() - 1))));\nreturn Math.max(rowA, rowB);","lineMap":{"3":2,"4":3,"5":4,"6":5,"7":6,"8":7,"9":9,"10":12,"11":13,"12":14},"statementBundles":{"8":{"startLine":7,"endLine":8},"9":{"startLine":9,"endLine":10}}}}}},{"id":"edge-case-intro","kind":"info","chapter":"Edge cases","eyebrow":"Try to break it","title":"Which inputs could break this?","workspacePanel":"visualizer","body":["The two-row split leans on having a last house to drop and a first house to drop. The shortest rings are where that assumption gets thin.","Try a single house, where dropping either endpoint empties the row, and a two-house ring, where each row shrinks to one house."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"edge-intro-state","state":{"nums":[1,2],"i":0,"labels":{"nums":"houses"}}}]}},{"id":"single-house-edge","kind":"case-probe","chapter":"Edge cases","eyebrow":"Stress the rule","title":"On a single house, what should the split return?","summary":"nums = [5]: dropping the last house leaves nothing, and dropping the first house leaves nothing.","workspacePanel":"visualizer","options":[{"id":"return-direct","label":"5, returned before the split runs","rationale":"Both rows are empty here, so each sweep would return 0. A one-house ring has to short-circuit and return that house.","isCorrect":true},{"id":"zero","label":"0, since both rows are empty","rationale":"Both rows being empty is exactly why the split fails here; the lone house is still robbable for its full value.","isCorrect":false},{"id":"double","label":"10, the house counted in both rows","rationale":"The two rows are alternative worlds, never summed, and neither row even contains this house.","isCorrect":false}],"answer":"5. With one house both rows are empty and would each return 0, so the solution returns that single house directly before splitting.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"single-house-state","state":{"nums":[5],"i":0,"labels":{"nums":"houses"}}}]}},{"id":"big-ends-edge","kind":"case-probe","chapter":"Edge cases","eyebrow":"Stress the rule","title":"On a two-house ring [1, 2], what does the split return?","summary":"Dropping the last house leaves the row [1]; dropping the first leaves [2].","workspacePanel":"visualizer","options":[{"id":"two","label":"2, the better of the two single-house rows","rationale":"Each row collapses to one house: drop-last gives 1, drop-first gives 2. The larger is 2.","isCorrect":true},{"id":"three","label":"3, robbing both houses","rationale":"The two houses are neighbors in the ring, so no legal plan keeps both; the split never lets a row hold the two endpoints together.","isCorrect":false},{"id":"one","label":"1, the first house","rationale":"Drop-first leaves [2], whose best is 2, and the split keeps the larger row, so 2 wins over 1.","isCorrect":false}],"answer":"2. Each row shrinks to a single house, 1 or 2, and the split keeps the larger, so the two-house ring returns 2 without ever pairing the neighbors.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"big-ends-state","state":{"rows":[{"label":"drop last","values":[1]},{"label":"drop first","values":[2]}],"i":0}}]}},{"id":"ready-to-code","kind":"info","chapter":"Code","eyebrow":"Get ready to code","title":"Turn the rule into code","summary":"One sweep, run twice on opposite endpoint-dropping rows.","ctaLabel":"Start coding","workspacePanel":"visualizer","body":["Handle the lone house first, then build the two rows: houses 0..n-2 and houses 1..n-1. Run the same rolling sweep on each, where every house takes the larger of skipping it or adding its money to the best two back.","Keep the larger of the two row results as the ring answer."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"ready-state","state":{"rows":[{"label":"drop last","values":[1,2,3]},{"label":"drop first","values":[2,3,1]}],"i":0}}]}},{"id":"coding","kind":"coding","source":"coding-checkpoints","chapter":"Code","eyebrow":"Code the rule","title":"Build the two-row sweep","summary":"Now build it: split the circle into the drop-last and drop-first rows, then for each row start the two rolling totals, walk the houses taking the better of skipping or robbing at every step, slide the totals forward, and return the larger of the two finished rows.","workspacePanel":"code","checkpoints":[{"id":"split-circle","title":"Split the circle into two rows","description":"Build two bounded views of the houses: one that drops the last house, one that drops the first.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"selects_opposite_endpoint_bounded_sequences","scope":"split-circle","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Solving the whole circle as one straight row","Dropping both endpoints in a single range"],"hint":"One range is houses 0 through n-2, the other is houses 1 through n-1.","semanticSpec":{"intent":"Create two bounded linear views of the circular input, each omitting one endpoint.","doneWhen":"The solution selects two bounded ranges that omit opposite endpoints.","shouldFailWhen":"This step should fail if the whole array is solved as one straight row without bounded ranges.","roles":["input houses","bounded ranges"],"mustProve":["The solution selects bounded reference sequences."],"mustNotAccept":["A single unbounded linear pass over all houses."]}},{"id":"init-rolling-states","title":"Initialize the two rolling totals","description":"Inside the linear helper, start the best-through-previous and best-through-two-back totals before scanning a row.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"initializes_recurrence_state","scope":"init-rolling-states","readMode":"dp-value-query-v1"}]},"commonMistakes":["Starting the rolling totals from arbitrary constants","Keeping only one rolling total"],"hint":"Each row needs the best through the previous house and the best through the one before that.","semanticSpec":{"intent":"Initialize recurrence state for the linear robbery helper.","doneWhen":"Two rolling recurrence states are initialized before scanning a row.","shouldFailWhen":"This step should fail if no recurrence state is initialized.","roles":["previous best","two-back best"],"mustProve":["The DP recurrence state is initialized."],"mustNotAccept":["A single running sum without recurrence state."]}},{"id":"scan-houses","title":"Scan one bounded row","description":"Walk every house in the current bounded row so the helper works for any slice.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"scans_indexed_state_space","scope":"iterate-houses","readMode":"dp-value-query-v1"}]},"commonMistakes":["Skipping the first house of a bounded row","Stopping before the row ends"],"hint":"The helper should loop over whatever slice it is handed.","semanticSpec":{"intent":"Scan the DP state space for one bounded row.","doneWhen":"The helper iterates through the bounded house values.","shouldFailWhen":"This step should fail if the row is not scanned.","roles":["bounded row","current house"],"mustProve":["The DP scans indexed state space."],"mustNotAccept":["A fixed two-house comparison."]}},{"id":"compute-choice","title":"Take the better of skip or rob","description":"At each house, compare keeping the previous best with adding this house to the best two back.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"computes_recurrence_transition","scope":"compute-next-states","readMode":"dp-value-query-v1"}]},"commonMistakes":["Adding the current house to the previous best instead of the two-back best","Dropping the skip option"],"hint":"The new best is max(previous best, two-back best + current amount).","semanticSpec":{"intent":"Compute the DP recurrence transition for one house.","doneWhen":"The next best value is derived from skip and take choices.","shouldFailWhen":"This step should fail if adjacent houses can both be included.","roles":["current house","skip total","take total"],"mustProve":["The solution computes a recurrence transition."],"mustNotAccept":["A simple running sum."]}},{"id":"write-rolling-state","title":"Slide the totals forward","description":"Write the new best back into the rolling totals so the next house reads fresh state.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"writes_recurrence_state","scope":"keep-better-outcome","readMode":"dp-value-query-v1"}]},"commonMistakes":["Computing the new best but never storing it","Sliding the two totals in the wrong order"],"hint":"After computing the new best, the previous best becomes two-back and the new best becomes previous.","semanticSpec":{"intent":"Persist recurrence progress between houses.","doneWhen":"The rolling totals advance using the computed recurrence value.","shouldFailWhen":"This step should fail if the recurrence value is not stored.","roles":["rolling totals","current best"],"mustProve":["The solution writes recurrence state."],"mustNotAccept":["A discarded recurrence calculation."]}},{"id":"return-best","title":"Return the better of the two rows","description":"Return the larger result of the two bounded linear runs as the circular answer.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"dp","fact":"returns_indexed_terminal_state","scope":"return-final-best","readMode":"dp-value-query-v1"}]},"commonMistakes":["Returning the result of only one row","Returning a two-back total instead of the final best"],"hint":"The circular answer is the max of the two linear answers.","semanticSpec":{"intent":"Return the terminal DP outcome across both rows.","doneWhen":"The function returns the larger of the two completed linear results.","shouldFailWhen":"This step should fail if the returned value is not the final DP state.","roles":["linear DP result","circular answer"],"mustProve":["The solution returns indexed terminal state."],"mustNotAccept":["Returning an intermediate rolling total."]}}]},{"id":"complete","kind":"complete","chapter":"Code","eyebrow":"Take it with you","title":"What to carry forward","workspacePanel":"code","body":["The circle adds one rule over a straight row: house 0 and the last house are neighbors, so no plan keeps both.","Dropping each endpoint in turn opens the ring into two plain rows the rolling sweep already solves, and the better row is the answer."],"takeaways":[{"title":"A ring is two rows","body":"Dropping the first house, or dropping the last, removes the one new conflict, so the best legal plan is the larger of those two straight-row sweeps."},{"title":"Drop opposite endpoints","body":"The two rows must drop different ends; dropping the same end twice never lets a plan keep the other endpoint."},{"title":"One house skips the split","body":"With a single house both rows are empty, so the solution returns that house directly before splitting."}],"nextAction":"Submit when you are ready. TraceCode will run the final checks against the code you wrote."}],"slidesOrigin":"authored"}}
