
You are given an m x n grid grid. Each cell contains either:
'+'
or
'-'
, or
'0'
…
'9'
.
You start at the top-left cell (0,0) and must reach the bottom-right cell (m-1,n-1) by moving only:
(r, c) -> (r, c+1)
, or
(r, c) -> (r+1, c)
.
As you traverse a path, you concatenate the visited cells (in order) to form an expression.
An expression is valid if it alternates strictly between numbers and operators:
digit, operator, digit, operator, digit, ...
0 ++ 1
,
3 +- 4
), or
1 2 + 3
,
5 - 6 3
).
Evaluation rule: Evaluate the expression strictly left-to-right (since only + and - are present).
Return the maximum possible evaluated value among all valid paths from (0,0) to (m-1,n-1).
1 <= m, n <= 50
grid[r][c]
is one of
{'+','-','0'..'9'}
null
/
None
/
-inf
) as specified by the interviewer.
A sawtooth sequence is a sequence of integers where adjacent elements have different parity (even/odd), i.e., it alternates between even and odd.
Given an integer array arr, count the number of contiguous subarrays that are sawtooth sequences.
arr = [1, 3, 5, 7, 9]
→ output
5
(only the 5 single-element subarrays).
arr = [1, 2, 1, 2, 1]
→ output
15
(all subarrays alternate parity).
arr = [1, 2, 3, 7, 6, 5]
→ output
12
.
1 <= len(arr) <= 2 * 10^5
|arr[i]| <= 10^9
Return the total number of contiguous sawtooth subarrays.