cs: add «2661. First Completely Painted Row or Column»

URL:	https://leetcode.com/problems/first-completely-painted-row-or-column/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2025-01-20 11:41:36 +01:00
parent 5b21547fd7
commit 834ffcec73
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,36 @@
public class Solution {
private bool InBounds(int[][] mat, int x, int y)
=> y >= 0 && y < mat.Length && x >= 0 && x < mat[y].Length;
private int Check(int[][] mat, Dictionary<int, int> indices, (int, int) start, (int, int) d) {
var lastToPaint = int.MinValue;
var (dx, dy) = d;
for (var (x, y) = start; InBounds(mat, x, y); x += dx, y += dy) {
lastToPaint = Math.Max(lastToPaint, indices[mat[y][x]]);
}
return lastToPaint;
}
public int FirstCompleteIndex(int[] arr, int[][] mat) {
var indices = new Dictionary<int, int>();
for (var i = 0; i < arr.Length; ++i) {
indices[arr[i]] = i;
}
var first = int.MaxValue;
// Check through rows
for (var y = 0; y < mat.Length; ++y) {
first = Math.Min(first, Check(mat, indices, (0, y), (1, 0)));
}
// Check through columns
for (var x = 0; x < mat[0].Length; ++x) {
first = Math.Min(first, Check(mat, indices, (x, 0), (0, 1)));
}
return first;
}
}