Algorithm optimization: Added early termination check in the solve function to skip expensive computations when the current cost already exceeds the best known cost. This represents the optimization from commit 81bf47c "Best cost optimization, 10% faster". Key changes: - Added `if cost >= best_cost { continue; }` check before updating best_cost - This allows the algorithm to skip entire branches of subproblems that cannot possibly result in a better solution - The optimization is particularly effective in pruning the search space This pattern is common in optimization algorithms where early termination can significantly improve performance.

Code view:

A painting is a person's account of this edit as text, recorded independently of the node mapping - a rendering often has several equally defensible answers where the mapping has one. Deletions and insertions carry no position on the opposite side in a painting, so that panel draws no caret for them.

Before

1fn solve(subtrees: &[usize]) -> u64 {
2 let mut best_cost = u64::MAX;
3
4 // Original approach: explore all possibilities without early termination
5 for i in 0..subtrees.len() {
6 let cost = calculate_cost(subtrees, i);
7
8 if cost < best_cost {
9 best_cost = cost;
10 }
11 }
12
13 best_cost
…lines 14–29 unchanged (16 lines)

After

1fn solve(subtrees: &[usize]) -> u64 {
2 let mut best_cost = u64::MAX;
3
4 // Optimized approach: early termination when current cost exceeds best
5 for i in 0..subtrees.len() {
6 let cost = calculate_cost(subtrees, i);
7
8 // Early termination optimization
9 if cost >= best_cost {
10 continue;
11 }
12
13 if cost < best_cost {
14 best_cost = cost;
15 }
16 }
17
…lines 18–34 unchanged (17 lines)

Before

source_file
function_item
fn "fn"
identifier "solve"
parameters
( "("
parameter
identifier "subtrees"
: ":"
reference_type
& "&"
array_type
[ "["
primitive_type "usize"
] "]"
) ")"
-> "->"
primitive_type "u64"
block
{ "{"
let_declaration
let "let"
mutable_specifier "mut"
identifier "best_cost"
= "="
scoped_identifier
identifier "u64"
:: "::"
identifier "MAX"
; ";"
line_comment
// "//"
expression_statement
for_expression
for "for"
identifier "i"
in "in"
range_expression
integer_literal "0"
.. ".."
call_expression
field_expression
identifier "subtrees"
. "."
field_identifier "len"
arguments
( "("
) ")"
block
{ "{"
let_declaration
let "let"
identifier "cost"
= "="
call_expression
identifier "calculate_cost"
arguments
( "("
identifier "subtrees"
, ","
identifier "i"
) ")"
; ";"
expression_statement
if_expression
if "if"
binary_expression
identifier "cost"
< "<"
identifier "best_cost"
block
{ "{"
expression_statement
assignment_expression
identifier "best_cost"
= "="
identifier "cost"
; ";"
} "}"
} "}"
identifier "best_cost"
} "}"
function_item (+63 nodes collapsed)
function_item (+56 nodes collapsed)

After

source_file
function_item
fn "fn"
identifier "solve"
parameters
( "("
parameter
identifier "subtrees"
: ":"
reference_type
& "&"
array_type
[ "["
primitive_type "usize"
] "]"
) ")"
-> "->"
primitive_type "u64"
block
{ "{"
let_declaration
let "let"
mutable_specifier "mut"
identifier "best_cost"
= "="
scoped_identifier
identifier "u64"
:: "::"
identifier "MAX"
; ";"
line_comment
// "//"
expression_statement
for_expression
for "for"
identifier "i"
in "in"
range_expression
integer_literal "0"
.. ".."
call_expression
field_expression
identifier "subtrees"
. "."
field_identifier "len"
arguments
( "("
) ")"
block
{ "{"
let_declaration
let "let"
identifier "cost"
= "="
call_expression
identifier "calculate_cost"
arguments
( "("
identifier "subtrees"
, ","
identifier "i"
) ")"
; ";"
line_comment
// "//"
expression_statement
if_expression
if "if"
binary_expression
identifier "cost"
>= ">="
identifier "best_cost"
block
{ "{"
expression_statement
continue_expression
continue "continue"
; ";"
} "}"
expression_statement
if_expression
if "if"
binary_expression
identifier "cost"
< "<"
identifier "best_cost"
block
{ "{"
expression_statement
assignment_expression
identifier "best_cost"
= "="
identifier "cost"
; ";"
} "}"
} "}"
identifier "best_cost"
} "}"
function_item (+63 nodes collapsed)
function_item (+56 nodes collapsed)