八皇后变题hash|网格dp

lc2975

计算水平/垂直栅栏(补全外围后)的所有间距

auto f = \&(vector<int>& a,int mv) -> unordered_set<int>

for (int d : vs)

if (hs.count(d) && d > max_l) {

集合存储水平间距,遍历垂直间距找到++两者共有的最大间距作为正方形边长++

class Solution {

public:

int maximizeSquareArea(int m, int n, vector<int>& h, vector<int>& v) {

const int mod = 1e9 + 7;

++auto f = \&(vector<int>& a,int mv) -> unordered_set<int> {++

a.push_back(1);

a.push_back(mv);

sort(a.begin(), a.end());

unordered_set<int> s;

for (int i = 0; i < a.size(); ++i) {

for (int j = i + 1; j < a.size(); ++j) {

s.insert(aj - ai);

}

}

return s;

};

auto hs = f(h,m);

auto vs = f(v,n);

int max_l = -1;

++for (int d : vs) {++

++if (hs.count(d) && d > max_l) {++

max_l = d;

}

}

if (max_l == -1) return -1;

long long res = (long long)max_l * max_l % mod;

return (int)res;

}

};

lc1301

先将起点终点置0初始化DP和路径数数组,从左上到右下遍历网格

跳过障碍后取上、左、左上方向的最大得分并累加对应路径数

最终返回右下角的最大得分和路径数(无路径则返回0,0

class Solution {

public:

vector<int> pathsWithMaxScore(vector<string>& board) {

int mod = 1e9+7;

int n = board.size(), m = board0.size();

vector f(n+10,vector<long long>(m+10,LONG_MIN/2)), g(n+10,vector<long long>(m+10,0));

board00 = boardn-1m-1 = '0';

// 这个初始化!! 一定要左上角位置初始化 不然是会影响 (1,1) 相邻点结果,导致最终结果不对

f00 = 0;

g00 = 1;

for(int i=1;i<=n;i++){

for(int j=1;j<=m;j++){

if(boardi-1j-1 == 'X') continue;

int val = boardi-1j-1-'0';

// 先更新最大值 再判断

long long mx_f = max({fi-1j, fij-1, fi-1j-1});

if(mx_f == LONG_MIN/2) continue; // 不可达判断方法!!

fij = mx_f + val; // 当前i,j 最大值;

long long cnt = 0;

if(fi-1j == mx_f) cnt += gi-1j;

if(fij-1 == mx_f) cnt += gij-1;

if(fi-1j-1 == mx_f) cnt += gi-1j-1;

gij = cnt % mod; // 必要

}

}

if(fnm == LONG_MIN/2){

return {0,0};

}

return {(int)fnm, (int)gnm % mod};

}

};

lc1001

hash计灯在行列、正负对角线的覆盖次数,查询时判断目标格是否被照亮,随后关闭查询格周围3×3区域的灯并更新统计

class Solution {

public:

unordered_map<int, int> ver, hor;

unordered_map<int, int> d1, d2;

set<pair<int,int>> st;

void add(pair<int,int> pr) {

if(st.count(pr))return;

verpr.first++;

horpr.second++;

d1pr.first+pr.second++;

d2pr.first-pr.second++;

st.insert(pr);

}

void close(pair<int,int> pr) {

verpr.first--;

horpr.second--;

d1pr.first+pr.second--;

d2pr.first-pr.second--;

st.erase(pr);

}

int query(pair<int,int> pr) {

return verpr.first > 0 || horpr.second > 0 || d1pr.first+pr.second > 0 || d2pr.first-pr.second > 0;

}

vector<int> gridIllumination(int N, vector<vector<int>>& lamps, vector<vector<int>>& queries) {

for(auto e: lamps)

++add(make_pair(e0, e1));++

vector<int> ans;

for(auto e: queries)

{

int x = e0, y = e1;

++ans.push_back(query(make_pair(x,y)));++

for(int i = -1 ; i <= 1 ; i++) {

for(int j = -1 ; j <= 1 ; j++) {

++if(st.count(make_pair(x+i, y+j)))
close(make_pair(x+i,y+j));
++

}

}

}

return ans;

}

};

相关推荐
Yvonne爱编码9 小时前
JAVA EE初阶---DAY 2 计算机网络
java·开发语言·计算机网络·算法·java-ee·php
workflower9 小时前
基于机器学习的设备故障预测分析方法
人工智能·算法·机器学习·设计模式·语言模型·自然语言处理·重构
格发许可优化管理系统9 小时前
Mentor许可证与其他软件许可证的深度比较
java·大数据·运维·c语言·c++·算法
wjcroom10 小时前
时空和电子7-泡力模型含罗量
人工智能·算法·机器学习
KaMeidebaby10 小时前
卡梅德生物技术快报 | Fab 合成文库构建与抗体筛选实验流程及数据解析
人工智能·python·tcp/ip·算法·机器学习
金融小师妹10 小时前
基于AI事件驱动模型与验证溢价框架的市场分析:从预期交易到事实验证,原油与黄金面临关键定价重构
大数据·人工智能·算法·均值算法·线性回归
xxwl58510 小时前
工作室小测的部分记录
c++·学习·算法
智者知已应修善业10 小时前
【51单片机串口通信甲机四个按键模拟四位二进制值发送乙机以十进制显示2位数码管】2024-6-14
c++·经验分享·笔记·算法·51单片机
KobeSacre10 小时前
划分为k个相等的子集
算法·leetcode·深度优先
不会就选b10 小时前
算法日常・每日刷题--<二分查找>2
算法