-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
87 lines (78 loc) · 2.55 KB
/
main.cpp
File metadata and controls
87 lines (78 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Source: https://leetcode.com/problems/equal-sum-grid-partition-i
// Title: Equal Sum Grid Partition I
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given an `m x n` matrix `grid` of positive integers. Your task is to determine if it is possible to make **either one horizontal or one vertical cut** on the grid such that:
//
// - Each of the two resulting sections formed by the cut is **non-empty**.
// - The sum of the elements in both sections is **equal**.
//
// Return `true` if such a partition exists; otherwise return `false`.
//
// **Example 1:**
//
// ```
// Input: grid = [[1,4],[2,3]]
// Output: true
// Explanation:
// https://assets.leetcode.com/uploads/2025/03/30/lc.pnghttps://assets.leetcode.com/uploads/2025/03/30/lc.jpeg
// A horizontal cut between row 0 and row 1 results in two non-empty sections, each with a sum of 5. Thus, the answer is `true`.
// ```
//
// **Example 2:**
//
// ```
// Input: grid = [[1,3],[2,4]]
// Output: false
// Explanation:
// No horizontal or vertical cut results in two non-empty sections with equal sums. Thus, the answer is `false`.
// ```
//
// **Constraints:**
//
// - `1 <= m == grid.length <= 10^5`
// - `1 <= n == grid[i].length <= 10^5`
// - `2 <= m * n <= 10^5`
// - `1 <= grid[i][j] <= 10^5`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cstdint>
#include <vector>
using namespace std;
// Loop
//
// First Compute the total sum.
// Next loop row-wise and column-wise and check if the sum is half of the total sum.
class Solution {
public:
bool canPartitionGrid(const vector<vector<int>>& grid) {
const int m = grid.size(), n = grid[0].size();
// Total Sum
int64_t totalSum = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
totalSum += grid[i][j];
}
}
// Total is odd
if (totalSum % 2) return false;
// Row-wise
int64_t rowSum = 0;
for (int i = 0; i < m - 1; ++i) { // exclude last row to avoid empty section
for (int j = 0; j < n; ++j) {
rowSum += grid[i][j];
}
if (2 * rowSum == totalSum) return true;
}
// Column-wise
int64_t colSum = 0;
for (int j = 0; j < n - 1; ++j) { // exclude last column to avoid empty section
for (int i = 0; i < m; ++i) {
colSum += grid[i][j];
}
if (2 * colSum == totalSum) return true;
}
return false;
}
};