codekofi
← All problems

Problem 117

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1struct Board {
2
3 map<pair<int, int>, int> seen;
4
5 void add(int x, int y) {
6 seen[{x, y}]++;
7 }
8
9 int count(int x, int y) {
10
11 int total = 0;
12
13 for (const auto& [point, times] : seen) {
14
15 int px = point.first;
16 int py = point.second;
17
18 if (px == x || py == y) {
19 continue;
20 }
21
22 if (px - x != py - y && px - x != y - py) {
23 continue;
24 }
25
26 auto a = seen.find({px, y});
27 auto b = seen.find({x, py});
28
29 if (a == seen.end() || b == seen.end()) {
30 continue;
31 }
32
33 total += times * a->second * b->second;
34 }
35
36 return total;
37 }
38};
39
40vector<string> solution(const vector<string>& ops,
41 const vector<int>& xs,
42 const vector<int>& ys) {
43
44 Board board;
45 vector<string> ans;
46
47 int n = ops.size();
48
49 for (int i = 0; i < n; i++) {
50
51 if (ops[i] == "add") {
52 board.add(xs[i], ys[i]);
53 ans.push_back("null");
54
55 } else {
56 int found = board.count(xs[i], ys[i]);
57 ans.push_back(to_string(found));
58 }
59 }
60
61 return ans;
62}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.