/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 返回一个整数,表示答案 * @param a int整型二维数组 * @param val int整型 * @return int整型 * * red * red * red * * ["red","edr","dre"],1 * red * edr * dre */
functionmatrixCount(a, val) { // write code here const n = a.length; let dp = Array.from({ length: n + 2 }).map(() => Array.from({ length: n + 2 }).fill({ r: 0, e: 0, d: 0 }) );
for (let i = 1; i <= n; i++) { for (let j = 1; j <= n; j++) { let char = a[i - 1][j - 1]; // 重点是中间 // red // edr // dre // ^ // dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1] let leftItem = dp[i][j - 1], topItem = dp[i - 1][j], leftTopItem = dp[i - 1][j - 1]; constget = (e) => leftItem[e] + topItem[e] - leftTopItem[e]; let item = { r: get("r"), e: get("e"), d: get("d"), }; item[char]++; dp[i][j] = item; } }
// console.log(dp); let ans = 0;
constgetBlankRED = () => ({ r: 0, e: 0, d: 0 });
consthelper = (win) => { for (let i = 1; i <= n; i++) { for (let j = 1; j <= n; j++) { // console.log(win, i, j); if (i - win < 0 || j - win < 0) { // 需要在原始矩阵内 continue; } // if (win >= n && (i !== 1 || j !== 1)) { // // 只能是对角最大的 // // console.log(win, i, j); // continue; // } let target = dp[i][j]; let leftItem = dp[i][j - win], topItem = dp[i - win][j], leftTopItem = dp[i - win][j - win]; constget = (e) => target[e] - leftItem[e] - topItem[e] + leftTopItem[e]; // console.log(win, i, j, get("r")); // , target, leftItem, topItem, leftTopItem let obj = { r: get("r"), e: get("e"), d: get("d"), }; let thisMyval = Math.min(...Object.values(obj)); if (thisMyval >= val) ans++; // console.log( // win, // i, // j, // obj, // thisMyval, // val, // thisMyval >= val, // ans // ); } } }; // helper(2); // return;/ for (let matrixWindow = 1; matrixWindow <= n; matrixWindow++) { helper(matrixWindow); }