ocaml: phase 5.1 stock_two.ml baseline (best of 2 transactions = 6)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Two-pass partition DP for max profit with at most 2 transactions:
left[i] = max single-trans profit in prices[0..i]
(forward scan tracking running min)
right[i] = max single-trans profit in prices[i..n-1]
(backward scan tracking running max)
answer = max over i of (left[i] + right[i])
For [3; 3; 5; 0; 0; 3; 1; 4]:
optimal partition i = 2:
left[2] = sell@5 after buy@3 = 2
right[2] = sell@4 after buy@0 in [2..7] = 4
total = 6
Tests parallel forward + backward passes on parallel DP arrays,
mixed ref + array state, for downto + for ascending scans on
the same data.
190 baseline programs total.
This commit is contained in:
@@ -166,6 +166,7 @@
|
||||
"shuffle.ml": 55,
|
||||
"simpson_int.ml": 10000,
|
||||
"stable_unique.ml": 46,
|
||||
"stock_two.ml": 6,
|
||||
"subseq_check.ml": 3,
|
||||
"tail_factorial.ml": 479001600,
|
||||
"tarjan_scc.ml": 4,
|
||||
|
||||
29
lib/ocaml/baseline/stock_two.ml
Normal file
29
lib/ocaml/baseline/stock_two.ml
Normal file
@@ -0,0 +1,29 @@
|
||||
let max_profit_two prices =
|
||||
let n = Array.length prices in
|
||||
if n < 2 then 0
|
||||
else begin
|
||||
let left = Array.make n 0 in
|
||||
let min_p = ref prices.(0) in
|
||||
for i = 1 to n - 1 do
|
||||
if prices.(i) < !min_p then min_p := prices.(i);
|
||||
let p = prices.(i) - !min_p in
|
||||
left.(i) <- if p > left.(i - 1) then p else left.(i - 1)
|
||||
done;
|
||||
let right = Array.make n 0 in
|
||||
let max_p = ref prices.(n - 1) in
|
||||
for i = n - 2 downto 0 do
|
||||
if prices.(i) > !max_p then max_p := prices.(i);
|
||||
let p = !max_p - prices.(i) in
|
||||
right.(i) <- if p > right.(i + 1) then p else right.(i + 1)
|
||||
done;
|
||||
let best = ref 0 in
|
||||
for i = 0 to n - 1 do
|
||||
let total = left.(i) + right.(i) in
|
||||
if total > !best then best := total
|
||||
done;
|
||||
!best
|
||||
end
|
||||
|
||||
;;
|
||||
|
||||
max_profit_two [| 3; 3; 5; 0; 0; 3; 1; 4 |]
|
||||
@@ -407,6 +407,16 @@ _Newest first._
|
||||
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
||||
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
||||
recursive match, List.append, List.fold_left.
|
||||
- 2026-05-11 Phase 5.1 — stock_two.ml baseline (max stock profit
|
||||
with at most 2 transactions on [3;3;5;0;0;3;1;4] = 6). Two-pass
|
||||
DP: left[i] = max single-transaction profit in prices[0..i]
|
||||
(forward, tracking running min), right[i] = max single-transaction
|
||||
profit in prices[i..n-1] (backward, tracking running max). Final
|
||||
answer = max over i of left[i]+right[i], partitioning at the
|
||||
best split. Optimal: buy@3, sell@5 (profit 2); buy@0, sell@4
|
||||
(profit 4); total = 6. Tests parallel forward + backward passes
|
||||
on parallel DP arrays, mixed ref+array state, downto + ascending
|
||||
scans on same data. 190 baseline programs total.
|
||||
- 2026-05-11 Phase 5.1 — house_robber.ml baseline (linear-DP
|
||||
max non-adjacent-sum on [2;7;9;3;1;5;8;6] = 22). dp[i] =
|
||||
max(dp[i-2]+houses[i], dp[i-1]). Optimal pick {7, 9, 5, 8} but
|
||||
|
||||
Reference in New Issue
Block a user