From 87c808a61bb6ce46dcacf3907c4f5ebe29e60aca Mon Sep 17 00:00:00 2001 From: eat-swap Date: Sat, 30 Apr 2022 01:04:28 +0800 Subject: [PATCH] add: 220429-CN [cpp] --- cpp/2204/220429-CN.cpp | 49 +++++++++++++++++++++++++++++++++++++++++ cpp/2204/CMakeLists.txt | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 cpp/2204/220429-CN.cpp diff --git a/cpp/2204/220429-CN.cpp b/cpp/2204/220429-CN.cpp new file mode 100644 index 0000000..9f70298 --- /dev/null +++ b/cpp/2204/220429-CN.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +// Definition for a QuadTree node. +class Node { +public: + bool val, isLeaf; + Node* topLeft; + Node* topRight; + Node* bottomLeft; + Node* bottomRight; + + explicit Node(bool val = false, bool isLeaf = false, Node* topLeft = nullptr, Node* topRight = nullptr, Node* bottomLeft = nullptr, Node* bottomRight = nullptr) : val(val), isLeaf(isLeaf), topLeft(topLeft), topRight(topRight), bottomLeft(bottomLeft), bottomRight(bottomRight) {} +}; + +/** + * 427. Construct Quad Tree + * Given a n * n matrix grid of 0's and 1's only. We want to represent the grid with a Quad-Tree. + * Return the root of the Quad-Tree representing the grid. + */ + +class Solution { +public: + Node* construct(std::vector>& G) { + const int n = G.size(); + + int* prefixSum = new int[(n + 1) * (1 + n)]{}; + for (int i = 1; i <= n; ++i) + for (int j = 1; j <= n; ++j) + prefixSum[i * n + j] = prefixSum[(i - 1) * n + j] + prefixSum[i * n + j - 1] - prefixSum[(i - 1) * n + j - 1] + G[i - 1][j - 1]; + + auto sum = [&](int x0, int y0, int x1, int y1) { + return prefixSum[x1 * n + y1] - prefixSum[x0 * n + y1] - prefixSum[x1 * n + y0] + prefixSum[x0 * n + y0]; + }; + + std::function dfs = [&](int x0, int y0, int x1, int y1) { + int s = sum(x0, y0, x1, y1); + if (!s) + return new Node(false, true); + if (s == (y1 - y0) * (x1 - x0)) + return new Node(true, true); + const int mx = (x0 + x1) >> 1, my = (y0 + y1) >> 1; + return new Node(std::rand() & 1, false, dfs(x0, y0, mx, my), dfs(x0, my, mx, y1), dfs(mx, y0, x1, my), dfs(mx, my, x1, y1)); + }; + + return dfs(0, 0, n, n); + } +}; diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 994dad8..d89f49e 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220429.cpp) +ADD_EXECUTABLE(2204 220429-CN.cpp)