最多跳过 K 格的网格 PnL 路径最大化(仅右/下行)
Maximum Grid PnL Path With At-Most-K Cell Skips (Right/Down)
开始编码实现 solution(pnl_grid: list[list[int]], max_skips: int) -> int。给定 R x C 的二维带符号整数 PnL 贡献网格 pnl_grid,要找一条从 (0, 0) 到 (R-1, C-1) 的路径,仅使用 RIGHT(c -> c+1)与 DOWN(r -> r+1)两种走法,使得所访问格子的累加和最大,且额外允许"跳过"(置 0)所访问格子中至多 max_skips 个。被跳过的格子贡献为 0(而非其原始值);未被跳过的格子贡献其带符号原值。两个端点 (0, 0) 与 (R-1, C-1) 必被访问,且任意一个都可以被跳过。返回这个最大累加和。
每条路径恰好访问 R + C - 1 个格子。当 max_skips >= R + C - 1 时整条路径都可置 0,答案至少为 0。当 max_skips = 0 时不允许跳过,问题退化为标准的右/下最大和网格路径(经典 LC64 风格 DP),其答案在所有路径都被负值压制时可严格为负。
例
solution([[2, -10, 4], [-10, 5, -10], [3, -10, 6]], 1) 返回 3。该网格 R = C = 3,所以每条路径恰好访问 R + C - 1 = 5 个格子。从 (0, 0) 到 (2, 2) 共有六条右/下路径:
(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2),访问[2, -10, 4, -10, 6],无跳过和为-8。(0,0) -> (0,1) -> (1,1) -> (1,2) -> (2,2),访问[2, -10, 5, -10, 6],无跳过和为-7。(0,0) -> (0,1) -> (1,1) -> (2,1) -> (2,2),访问[2, -10, 5, -10, 6],无跳过和为-7。(0,0) -> (1,0) -> (1,1) -> (1,2) -> (2,2),访问[2, -10, 5, -10, 6],无跳过和为-7。(0,0) -> (1,0) -> (1,1) -> (2,1) -> (2,2),访问[2, -10, 5, -10, 6],无跳过和为-7。(0,0) -> (1,0) -> (2,0) -> (2,1) -> (2,2),访问[2, -10, 3, -10, 6],无跳过和为-9。
当 max_skips = 0 时最优路径和为 -7。当 max_skips = 1 时最优做法是选任一经过中心 5 的路径(如 [2, -10, 5, -10, 6]),跳过其中一个 -10,得到 2 + 0 + 5 + (-10) + 6 = 3(或对称地 2 + (-10) + 5 + 0 + 6 = 3);把这一次跳过用在"不经过 5"的单 -10 路径上严格更差。当 max_skips = 2 时取 [2, -10, 5, -10, 6] 并跳过两个 -10,得 2 + 0 + 5 + 0 + 6 = 13。当 max_skips >= 5(完整路径长度)时全部置 0 的退化路径可得 0,但最优方案仍按需用跳过预算并返回 13(跳过一个正值格子严格变差)。
实践背景
某资金台在 R x C 的 PnL 网格上做风险预算分配——行是交易时段,列是顺序的策略切片,每格是该 (时段, 切片) 在固定分配策略下的 PnL 贡献。它想知道:在受约束的执行序列下(必访 (0, 0)、每步只能行号或列号 +1、终点 (R-1, C-1)),且**允许将至多 max_skips 个被访格子置平**(贡献置 0,例如预定的交易休假、单一品种黑名单、预先公告的 risk-off 切片),可达到的最大总 PnL 是多少。max_skips 参数刻画了交易台对"被吞掉的平仓时段"的容忍度,也是该问题相对原版 LC64 最大和路径的真正区分点:没有跳过预算时优化器无法绕过单格灾难性回撤;有充足预算时可以。这个数字是事后视角的上界——要知道哪 max_skips 个格子最该置平需要事后可见性——通常被作为某个候选"平仓日识别模型"评估其 跳过捕获率(skip-capture ratio) 的分子或分母。"网格上的路径 + 跳过预算"形态与单维序列跳过、纯子集背包都不同:路径约束禁止任意挑格子,跳过预算又禁止任意挑子集,二者在 O(R * C * (max_skips + 1)) DP 中非平凡地耦合。
约束条件
- 1 <= R, C <= 100,其中 R = len(pnl_grid)、C = len(pnl_grid[0]);pnl_grid 每一行长度都为 C
- 0 <= max_skips <= R + C;超过路径长度 R + C - 1 的部分将被截断为 R + C - 1(不报错——超额的跳过预算用不完,因为路径恰好访问 R + C - 1 个格子)
- 每个 pnl_grid[r][c] 是带符号 Python int,|pnl_grid[r][c]| <= 1_000_000
- 输出为 Python int(精确比较器,ordered=true);当 max_skips < R + C - 1 且所有路径的保留和均为负时,答案可以为负
- 两个端点 (0, 0) 与 (R-1, C-1) 必经过;任意一个**都可以**被跳过(跳过预算适用于任意访问到的格子,包含端点)
样例
Case 1 · typical: statement example, max_skips=1
输入: [[[2,-10,4],[-10,5,-10],[3,-10,6]],1]
期望: 3
最优路径访问 [2, -10, 5, -10, 6],跳过一个 -10:2 + 0 + 5 + (-10) + 6 = 3。
Case 2 · typical: statement example, max_skips=0 — LC64 reduction
输入: [[[2,-10,4],[-10,5,-10],[3,-10,6]],0]
期望: -7
无跳过时最佳右/下路径访问 [2, -10, 5, -10, 6],和为 -7。
Case 3 · typical: statement example, max_skips=2 — both -10s skipped
输入: [[[2,-10,4],[-10,5,-10],[3,-10,6]],2]
期望: 13
路径 [2, -10, 5, -10, 6] 跳过两个 -10:2 + 0 + 5 + 0 + 6 = 13。
Case 4 · typical: 2x2 all-positive, max_skips=0
输入: [[[1,2],[3,4]],0]
期望: 8
两条路径 1+2+4=7 或 1+3+4=8;取最大 8。
Case 5 · typical: 2x2 all-positive, max_skips=1 — skip never helps
输入: [[[1,2],[3,4]],1]
期望: 8
跳过任何正值格子都会减少和,所以答案与 max_skips=0 相同:8。
Case 6 · typical: 1x1 grid positive, max_skips=0
输入: [[[7]],0]
期望: 7
唯一一格,不跳过;答案 7。
Case 7 · typical: 1x1 grid negative, max_skips=1 — skip the only cell
输入: [[[-5]],1]
期望: 0
跳过这唯一一个负值格子贡献 0;答案 0。
Case 8 · typical: 1x1 grid negative, max_skips=0 — locked into negative
输入: [[[-5]],0]
期望: -5
不允许跳过,必须保留唯一格子的 -5。
Case 9 · typical: 1xN corridor, max_skips=2 — drop the two most negative
输入: [[[3,-2,4,-1,5,-3]],2]
期望: 11
1xN 走廊访问所有格子;删去两个最负的 (-3 和 -2):3+0+4-1+5+0=11。
Case 10 · typical: 1xN corridor, max_skips=0 — sum all
输入: [[[3,-2,4,-1,5,-3]],0]
期望: 6
1xN 走廊不跳过返回全和 3-2+4-1+5-3=6。
Case 11 · typical: Nx1 corridor, max_skips=2
输入: [[[3],[-2],[4],[-1],[5],[-3]],2]
期望: 11
Nx1 列依次访问所有格子;删去两个最负值得 3+0+4-1+5+0=11。
Case 12 · typical: 3x3 mixed signs, max_skips=2
输入: [[[5,-3,2],[-3,8,-3],[2,-3,5]],2]
期望: 18
最佳路径经过中心 8 并跳过两个 -3,得 18。
Case 13 · typical: 3x3 mixed signs, max_skips=0
输入: [[[5,-3,2],[-3,8,-3],[2,-3,5]],0]
期望: 12
无跳过——最佳右/下路径 5+(-3)+8+(-3)+5 = 12。
Case 14 · typical: 5x5 all-zero grid, any skip count returns 0
输入: [[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]],3]
期望: 0
所有格子均为 0;无论跳过几次和都为 0。
Case 15 · typical: 2x3 grid with single hot cell, max_skips=0
输入: [[[1,1,1],[1,100,1]],0]
期望: 103
最佳路径经过 (1,1)=100:1+1+100+1 = 103。
Case 16 · boundary: max_skips equals path length — every cell skippable, answer 0
输入: [[[-1,-2,-3],[-4,-5,-6]],4]
期望: 0
max_skips=4 等于路径长度 R+C-1=4;任意路径上每个格子都可跳过,故答案至少为 0。所有值为负,最优全跳过。
最近提交
还没有提交记录。
编码区
实现 solution(...)。本地运行当前支持 Python 可见样例;服务端提交会运行可见样例和隐藏测试。
默认展示公开样例。点击「运行样例」后会在这里显示实际输出;点击「提交评测」会进入隐藏测试。
Case 1 · typical: statement example, max_skips=1
输入: [[[2,-10,4],[-10,5,-10],[3,-10,6]],1]
期望: 3
最优路径访问 [2, -10, 5, -10, 6],跳过一个 -10:2 + 0 + 5 + (-10) + 6 = 3。
Case 2 · typical: statement example, max_skips=0 — LC64 reduction
输入: [[[2,-10,4],[-10,5,-10],[3,-10,6]],0]
期望: -7
无跳过时最佳右/下路径访问 [2, -10, 5, -10, 6],和为 -7。
Case 3 · typical: statement example, max_skips=2 — both -10s skipped
输入: [[[2,-10,4],[-10,5,-10],[3,-10,6]],2]
期望: 13
路径 [2, -10, 5, -10, 6] 跳过两个 -10:2 + 0 + 5 + 0 + 6 = 13。
Case 4 · typical: 2x2 all-positive, max_skips=0
输入: [[[1,2],[3,4]],0]
期望: 8
两条路径 1+2+4=7 或 1+3+4=8;取最大 8。
Case 5 · typical: 2x2 all-positive, max_skips=1 — skip never helps
输入: [[[1,2],[3,4]],1]
期望: 8
跳过任何正值格子都会减少和,所以答案与 max_skips=0 相同:8。
Case 6 · typical: 1x1 grid positive, max_skips=0
输入: [[[7]],0]
期望: 7
唯一一格,不跳过;答案 7。
Case 7 · typical: 1x1 grid negative, max_skips=1 — skip the only cell
输入: [[[-5]],1]
期望: 0
跳过这唯一一个负值格子贡献 0;答案 0。
Case 8 · typical: 1x1 grid negative, max_skips=0 — locked into negative
输入: [[[-5]],0]
期望: -5
不允许跳过,必须保留唯一格子的 -5。
Case 9 · typical: 1xN corridor, max_skips=2 — drop the two most negative
输入: [[[3,-2,4,-1,5,-3]],2]
期望: 11
1xN 走廊访问所有格子;删去两个最负的 (-3 和 -2):3+0+4-1+5+0=11。
Case 10 · typical: 1xN corridor, max_skips=0 — sum all
输入: [[[3,-2,4,-1,5,-3]],0]
期望: 6
1xN 走廊不跳过返回全和 3-2+4-1+5-3=6。
Case 11 · typical: Nx1 corridor, max_skips=2
输入: [[[3],[-2],[4],[-1],[5],[-3]],2]
期望: 11
Nx1 列依次访问所有格子;删去两个最负值得 3+0+4-1+5+0=11。
Case 12 · typical: 3x3 mixed signs, max_skips=2
输入: [[[5,-3,2],[-3,8,-3],[2,-3,5]],2]
期望: 18
最佳路径经过中心 8 并跳过两个 -3,得 18。
Case 13 · typical: 3x3 mixed signs, max_skips=0
输入: [[[5,-3,2],[-3,8,-3],[2,-3,5]],0]
期望: 12
无跳过——最佳右/下路径 5+(-3)+8+(-3)+5 = 12。
Case 14 · typical: 5x5 all-zero grid, any skip count returns 0
输入: [[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]],3]
期望: 0
所有格子均为 0;无论跳过几次和都为 0。
Case 15 · typical: 2x3 grid with single hot cell, max_skips=0
输入: [[[1,1,1],[1,100,1]],0]
期望: 103
最佳路径经过 (1,1)=100:1+1+100+1 = 103。
Case 16 · boundary: max_skips equals path length — every cell skippable, answer 0
输入: [[[-1,-2,-3],[-4,-5,-6]],4]
期望: 0
max_skips=4 等于路径长度 R+C-1=4;任意路径上每个格子都可跳过,故答案至少为 0。所有值为负,最优全跳过。