problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::grid_illumination::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static vector< int > gridIllumination (int n, vector< vector< int > > &lamps, vector< vector< int > > &queries)
 

详细描述

在文件 leetcode.h1031 行定义.

成员函数说明

◆ gridIllumination()

vector< int > leetcode::grid_illumination::Solution::gridIllumination ( int  n,
vector< vector< int > > &  lamps,
vector< vector< int > > &  queries 
)
static

< 从左上到右下的对角线

< 从右上到左下的对角线

在文件 leetcode.cpp2584 行定义.

2584 {
2585 auto ls = unordered_set<pair<int, int>, pair_hash>();
2586 auto row = unordered_map<int, int>();
2587 auto col = unordered_map<int, int>();
2588 auto diag_down = unordered_map<int, int>();
2589 auto diag_up = unordered_map<int, int>();
2590 for(auto lamp: lamps) {
2591 int x = lamp[0];
2592 int y = lamp[1];
2593 auto p = make_pair(x, y);
2594 if(!ls.contains(p)) {
2595 ls.insert(p);
2596 row[x]++;
2597 col[y]++;
2598 diag_down[n - x + y]++;
2599 diag_up[x + y]++;
2600 }
2601 }
2602 auto ans = vector<int>();
2603 for(auto query: queries) {
2604 int query_x = query[0];
2605 int query_y = query[1];
2606 if(row[query_x] > 0 || col[query_y] > 0 || diag_down[n - query_x + query_y] > 0 || diag_up[query_x + query_y] > 0) {
2607 ans.push_back(1);
2608 } else {
2609 ans.push_back(0);
2610 }
2611 pair<int, int> adjacents[9] = {make_pair(query_x + 1, query_y + 1), make_pair(query_x + 1, query_y), make_pair(query_x + 1, query_y - 1),
2612 make_pair(query_x, query_y + 1), make_pair(query_x, query_y), make_pair(query_x, query_y - 1),
2613 make_pair(query_x - 1, query_y + 1), make_pair(query_x - 1, query_y), make_pair(query_x - 1, query_y - 1)};
2614 for(auto adjacent: adjacents) {
2615 if(ls.contains(adjacent)) {
2616 ls.erase(adjacent);
2617 auto [x, y] = adjacent;
2618 row[x]--;
2619 col[y]--;
2620 diag_down[n - x + y]--;
2621 diag_up[x + y]--;
2622 }
2623 }
2624 }
2625 return ans;
2626 }

被这些函数引用 leetcode::grid_illumination::TEST().


该类的文档由以下文件生成: