Skip to content

Commit b066885

Browse files
committed
leetcode
1 parent 4933a2f commit b066885

File tree

2 files changed

+67
-27
lines changed

2 files changed

+67
-27
lines changed

LeetCode/2536. Increment Submatrices by One.cpp.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@
22
written by Pankaj Kumar.
33
country:-INDIA
44
*/
5-
typedef long long ll ;
6-
const ll INF=1e18;
7-
const ll mod1=1e9+7;
8-
const ll mod2=998244353;
9-
//Add main code here
5+
typedef long long ll;
6+
const ll INF = 1e18;
7+
const ll mod1 = 1e9 + 7;
8+
const ll mod2 = 998244353;
9+
// Add main code here
1010

1111
class Solution
1212
{
1313
public:
14-
long long minOperations(vector<int> &nums1, vector<int> &nums2, int k)
14+
vector<vector<int>> rangeAddQueries(int n, vector<vector<int>> &queries)
1515
{
16-
if(k==0){
17-
return nums1==nums2?0:-1;
16+
vector<vector<int>> s(n, vector<int>(n, 0));
17+
for (auto &q : queries)
18+
{
19+
++q[2], ++q[3];
20+
s[q[0]][q[1]]++;
21+
if (q[2] < n)
22+
s[q[2]][q[1]]--;
23+
if (q[3] < n)
24+
s[q[0]][q[3]]--;
25+
if (q[2] < n && q[3] < n)
26+
s[q[2]][q[3]]++;
1827
}
19-
long long countPos=0,countNeg=0;
20-
for(int i=0;i<nums1.size();i++){
21-
long long diff=nums1[i]-nums2[i];
22-
if(diff%k){
23-
return -1;
28+
for (int i = 0; i < n; ++i)
29+
for (int j = 0; j < n; ++j)
30+
{
31+
if (i > 0)
32+
s[i][j] += s[i - 1][j];
33+
if (j > 0)
34+
s[i][j] += s[i][j - 1];
35+
if (i > 0 && j > 0)
36+
s[i][j] -= s[i - 1][j - 1];
2437
}
25-
else if(diff>0){
26-
countPos+=diff/k;
27-
}
28-
else{
29-
countNeg+=diff/k;
30-
}
31-
32-
}
33-
if(countPos+countNeg==0){
34-
return countPos;
35-
}
36-
else{
37-
return -1;
38-
}
38+
return s;
3939
}
4040
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll ;
6+
const ll INF=1e18;
7+
const ll mod1=1e9+7;
8+
const ll mod2=998244353;
9+
//Add main code here
10+
11+
class Solution
12+
{
13+
public:
14+
long long minOperations(vector<int> &nums1, vector<int> &nums2, int k)
15+
{
16+
if(k==0){
17+
return nums1==nums2?0:-1;
18+
}
19+
long long countPos=0,countNeg=0;
20+
for(int i=0;i<nums1.size();i++){
21+
long long diff=nums1[i]-nums2[i];
22+
if(diff%k){
23+
return -1;
24+
}
25+
else if(diff>0){
26+
countPos+=diff/k;
27+
}
28+
else{
29+
countNeg+=diff/k;
30+
}
31+
32+
}
33+
if(countPos+countNeg==0){
34+
return countPos;
35+
}
36+
else{
37+
return -1;
38+
}
39+
}
40+
};

0 commit comments

Comments
 (0)