Cherry Dual Paths Problem

Cherry Dual Paths Problem — ExecCode Hard DSA Practice

Solve the Cherry Dual Paths problem on ExecCode. Free online hard DSA practice in DP. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Two collectors walk from (0,0) to (n-1,n-1) moving only right or down. Cells hold cherries (1), empty (0), or thorns (-1, impassable). Cherries are collected when first visited; if both paths step on the same cell it counts once. Maximize total cherries. If unreachable, 0. Return the maximum cherries the pair can collect. You are given: grid — n×n grid of -1/0/1 Return integer >= 0. Thinking of one path going and one returning is equivalent to two simultaneous down/right walks.

Examples

Input {'grid': [[0, 1, -1], [1, 0, -1], [1, 1, 1]]}; Output 5. Input {'grid': [[1, 1], [1, 1]]}; Output 4. Input {'grid': [[1, -1], [-1, 1]]}; Output 0

Constraints

Both paths must be valid or the answer is 0 Shared cells count once

Practice Cherry Dual Paths free on ExecCode. Browse DSA problems, topic map, and placement guides.