class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int n = obstacleGrid[0].size();
vector<int> f(n);
f[0] = 1;
for (auto& row: obstacleGrid) {
for (int j = 0; j < n; j++) {
if (row[j] == 1) {
f[j] = 0;
}
else if (j) f[j] += f[j - 1];
}
}
return f[n - 1];
}
};