Fewest Arrow Changes Creating a Corner-to-Corner Path

Quick Overview

Fewest Arrow Changes Creating a Corner-to-Corner Path. Each grid cell has an arrow:1 right,2 left,3 down,4 up.

Fewest Arrow Changes Creating a Corner-to-Corner Path

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Each grid cell has an arrow:1 right,2 left,3 down,4 up. Some arrows may point outside the grid. Starting at the top-left, follow arrows to the bottom-right. You may change each cell's arrow at most once for cost 1. Return the minimum total change cost that permits at least one such path; the path need not minimize steps. ```cpp class Solution { public: int minCost(vector<vector<int>>& grid); }; ``` ### Examples **Example 1:** ```text Input: grid = [[1,3],[1,4]] Output: 0 Explanation: Follow right,then down to reach the bottom-right without changing any arrow. ``` **Example 2:** ```text Input: grid = [[2,2]] Output: 1 Explanation: Change the first arrow from left to right; the destination's outgoing arrow does not matter. ``` ### Constraints - The rectangular grid has 1 to 100 rows and 1 to 100 columns. - Every sign is an integer from 1 through 4 with the stated directions. ### Contract considerations Only the minimum cost is returned. Multiple routes do not create output freedom in this numeric result. ```hint Read the boundary condition Distinguish modification cost from path length. Check outward-pointing arrows and a grid whose start is already the destination. ``` ### Boundary reminders Only the minimum cost is returned. Multiple routes do not create output freedom in this numeric result.

Overview: Fewest Arrow Changes Creating a Corner-to-Corner Path. Each grid cell has an arrow:1 right,2 left,3 down,4 up.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each grid cell has an arrow:1 right,2 left,3 down,4 up. Some arrows may point outside the grid. Starting at the top-left, follow arrows to the bottom-right. You may change each cell's arrow at most once for cost 1. Return the minimum total change cost that permits at least one such path; the path need not minimize steps.

class Solution {
public:
    int minCost(vector<vector<int>>& grid);
};

Examples

Example 1:

Input: grid = [[1,3],[1,4]]
Output: 0
Explanation: Follow right,then down to reach the bottom-right without changing any arrow.

Example 2:

Input: grid = [[2,2]]
Output: 1
Explanation: Change the first arrow from left to right; the destination's outgoing arrow does not matter.

Constraints

  • The rectangular grid has 1 to 100 rows and 1 to 100 columns.
  • Every sign is an integer from 1 through 4 with the stated directions.

Contract considerations

Only the minimum cost is returned. Multiple routes do not create output freedom in this numeric result.

Boundary reminders

Only the minimum cost is returned. Multiple routes do not create output freedom in this numeric result.