Skip to content

leetcode2 #4491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 82 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
1f176e7
Added README.md file for Keep Multiplying Found Values by Two
achuatchaya May 23, 2025
08ab33a
Time: 0 ms (100.00%) | Memory: 14 MB (68.68%) - LeetSync
achuatchaya May 23, 2025
d281d85
Added README.md file for Keep Multiplying Found Values by Two
achuatchaya May 23, 2025
27e5f3c
Time: 0 ms (100.00%) | Memory: 13.9 MB (90.19%) - LeetSync
achuatchaya May 23, 2025
71db466
Added README.md file for Count Equal and Divisible Pairs in an Array
achuatchaya May 23, 2025
0eadf16
Time: 0 ms (100.00%) | Memory: 15.4 MB (69.34%) - LeetSync
achuatchaya May 23, 2025
69259ac
Added README.md file for Count Equal and Divisible Pairs in an Array
achuatchaya May 23, 2025
325a439
Time: 0 ms (100.00%) | Memory: 15.4 MB (69.34%) - LeetSync
achuatchaya May 23, 2025
90ea5b4
Added README.md file for Maximum Product of Two Digits
achuatchaya May 23, 2025
dc29977
Time: 0 ms (100.00%) | Memory: 9.2 MB (28.02%) - LeetSync
achuatchaya May 23, 2025
37130b7
Added README.md file for Truncate Sentence
achuatchaya May 23, 2025
b6e663f
Time: 4 ms (1.89%) | Memory: 10.8 MB (5.30%) - LeetSync
achuatchaya May 23, 2025
2e00896
Added README.md file for Minimum Cost of Buying Candies With Discount
achuatchaya May 23, 2025
bd476dd
Time: 0 ms (100.00%) | Memory: 14.1 MB (79.94%) - LeetSync
achuatchaya May 23, 2025
36ab70c
Added README.md file for Maximum Units on a Truck
achuatchaya May 23, 2025
3a125dd
Time: 0 ms (100.00%) | Memory: 19.9 MB (45.43%) - LeetSync
achuatchaya May 23, 2025
e8c1df3
Added README.md file for Balanced Binary Tree
achuatchaya May 23, 2025
ac3ede2
Time: 0 ms (100.00%) | Memory: 23 MB (56.66%) - LeetSync
achuatchaya May 23, 2025
287c451
Added README.md file for Apple Redistribution into Boxes
achuatchaya May 24, 2025
f2c8118
Time: 0 ms (100.00%) | Memory: 33 MB (73.18%) - LeetSync
achuatchaya May 24, 2025
22e9d15
Added README.md file for Average of Levels in Binary Tree
achuatchaya May 24, 2025
36ebd2d
Time: 8 ms (4.42%) | Memory: 23.9 MB (26.08%) - LeetSync
achuatchaya May 24, 2025
f2868e9
Added README.md file for Average of Levels in Binary Tree
achuatchaya May 24, 2025
7b63edd
Time: 0 ms (100.00%) | Memory: 23.8 MB (53.99%) - LeetSync
achuatchaya May 24, 2025
eb01345
Added README.md file for Distance Between Bus Stops
achuatchaya May 25, 2025
07437b5
Time: 3 ms (61.11%) | Memory: 12.5 MB (83.84%) - LeetSync
achuatchaya May 25, 2025
6a6ab8f
Added README.md file for Minimum Absolute Difference
achuatchaya May 25, 2025
3cea204
Time: 11 ms (98.67%) | Memory: 36.6 MB (89.89%) - LeetSync
achuatchaya May 25, 2025
fa16cf8
Added README.md file for Buy Two Chocolates
achuatchaya May 25, 2025
a18eaa3
Time: 0 ms (100.00%) | Memory: 52.3 MB (31.59%) - LeetSync
achuatchaya May 25, 2025
80e6833
Added README.md file for Buy Two Chocolates
achuatchaya May 25, 2025
dd70bb7
Time: 1 ms (23.80%) | Memory: 52.3 MB (4.45%) - LeetSync
achuatchaya May 25, 2025
8cbf610
Added README.md file for Delete Greatest Value in Each Row
achuatchaya May 26, 2025
e6797dd
Time: 0 ms (100.00%) | Memory: 13.1 MB (58.00%) - LeetSync
achuatchaya May 26, 2025
1dd0a50
Added README.md file for Lexicographically Smallest String After a Swap
achuatchaya May 26, 2025
a0854f2
Time: 0 ms (100.00%) | Memory: 8.4 MB (23.38%) - LeetSync
achuatchaya May 26, 2025
cc1cfd9
Added README.md file for Find Center of Star Graph
achuatchaya May 27, 2025
159a8f0
Time: 0 ms (100.00%) | Memory: 99.2 MB (72.50%) - LeetSync
achuatchaya May 27, 2025
43a8b09
Added README.md file for Alternating Digit Sum
achuatchaya May 28, 2025
641fcce
Time: 0 ms (100.00%) | Memory: 8 MB (50.25%) - LeetSync
achuatchaya May 28, 2025
96151db
Added README.md file for Minimum Number Game
achuatchaya May 28, 2025
65288c2
Time: 0 ms (100.00%) | Memory: 28.4 MB (26.83%) - LeetSync
achuatchaya May 28, 2025
1a101ac
Added README.md file for Minimum Number Game
achuatchaya May 28, 2025
8795e58
Time: 4 ms (13.93%) | Memory: 28.4 MB (43.82%) - LeetSync
achuatchaya May 28, 2025
ddf96bf
Added README.md file for Reverse Integer
achuatchaya May 30, 2025
0238bdb
Time: 5 ms (19.58%) | Memory: 8.6 MB (21.26%) - LeetSync
achuatchaya May 30, 2025
3fcedeb
Added README.md file for Palindrome Number
achuatchaya May 30, 2025
a6d5c71
Time: 0 ms (100.00%) | Memory: 8.5 MB (92.18%) - LeetSync
achuatchaya May 30, 2025
0275866
Added README.md file for N-th Tribonacci Number
achuatchaya May 30, 2025
64f19bb
Time: 0 ms (100.00%) | Memory: 8 MB (51.87%) - LeetSync
achuatchaya May 30, 2025
d23ef76
Added README.md file for N-th Tribonacci Number
achuatchaya May 30, 2025
ae05860
Time: 0 ms (100.00%) | Memory: 7.7 MB (97.98%) - LeetSync
achuatchaya May 30, 2025
2c8aa97
Added README.md file for Three Divisors
achuatchaya May 30, 2025
05bfe08
Time: 0 ms (100.00%) | Memory: 7.8 MB (24.31%) - LeetSync
achuatchaya May 30, 2025
651b349
Added README.md file for Number of Common Factors
achuatchaya May 30, 2025
8744205
Time: 0 ms (100.00%) | Memory: 7.8 MB (46.37%) - LeetSync
achuatchaya May 30, 2025
60606dc
Added README.md file for Power of Two
achuatchaya May 30, 2025
3366f2d
Time: 0 ms (100.00%) | Memory: 7.9 MB (18.55%) - LeetSync
achuatchaya May 30, 2025
b2a9eef
Added README.md file for Power of Three
achuatchaya May 30, 2025
36fbcbd
Time: 8 ms (16.59%) | Memory: 9 MB (46.81%) - LeetSync
achuatchaya May 30, 2025
b2dd903
Added README.md file for Power of Four
achuatchaya May 30, 2025
8edb773
Time: 0 ms (100.00%) | Memory: 7.9 MB (18.25%) - LeetSync
achuatchaya May 30, 2025
957df22
Added README.md file for Number Complement
achuatchaya May 30, 2025
cdfe891
Time: 0 ms (100.00%) | Memory: 8.2 MB (7.69%) - LeetSync
achuatchaya May 30, 2025
6b0e682
Added README.md file for Monotonic Array
achuatchaya Jun 8, 2025
3c5e94c
Time: 0 ms (100.00%) | Memory: 100.2 MB (90.84%) - LeetSync
achuatchaya Jun 8, 2025
2201851
Added README.md file for Monotonic Array
achuatchaya Jun 8, 2025
8d57004
Time: 1 ms (40.43%) | Memory: 100.4 MB (31.65%) - LeetSync
achuatchaya Jun 8, 2025
1e827f9
Added README.md file for Shift 2D Grid
achuatchaya Jun 8, 2025
bb5ea06
Time: 1 ms (54.80%) | Memory: 18.2 MB (66.74%) - LeetSync
achuatchaya Jun 8, 2025
fbebe27
Added README.md file for Shift 2D Grid
achuatchaya Jun 8, 2025
3bf314c
Time: 0 ms (100.00%) | Memory: 18.2 MB (66.74%) - LeetSync
achuatchaya Jun 8, 2025
d2abc42
Added README.md file for Monotonic Array
achuatchaya Jun 9, 2025
c8db5a3
Time: 1 ms (40.25%) | Memory: 100.4 MB (31.38%) - LeetSync
achuatchaya Jun 9, 2025
da5512e
Added README.md file for Shift 2D Grid
achuatchaya Jun 9, 2025
ac6f0b5
Time: 4 ms (30.77%) | Memory: 18.1 MB (66.40%) - LeetSync
achuatchaya Jun 9, 2025
be714aa
Added README.md file for Row With Maximum Ones
achuatchaya Jun 9, 2025
86dd5b6
Time: 0 ms (100.00%) | Memory: 64.6 MB (89.53%) - LeetSync
achuatchaya Jun 9, 2025
4d32368
Added README.md file for Toeplitz Matrix
achuatchaya Jun 9, 2025
be33784
Time: 0 ms (100.00%) | Memory: 21.2 MB (37.10%) - LeetSync
achuatchaya Jun 9, 2025
58858b6
Added README.md file for Matrix Diagonal Sum
achuatchaya Jun 9, 2025
6fd12fd
Time: 0 ms (100.00%) | Memory: 15 MB (53.49%) - LeetSync
achuatchaya Jun 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions 110-balanced-binary-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h2><a href="https://leetcode.com/problems/balanced-binary-tree">Balanced Binary Tree</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Given a binary tree, determine if it is <span data-keyword="height-balanced"><strong>height-balanced</strong></span>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg" style="width: 342px; height: 221px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/06/balance_2.jpg" style="width: 452px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [1,2,2,3,3,null,null,4,4]
<strong>Output:</strong> false
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> true
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li>The number of nodes in the tree is in the range <code>[0, 5000]</code>.</li>
<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul>
27 changes: 27 additions & 0 deletions 110-balanced-binary-tree/balanced-binary-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
return check(root)!=-1;
}
int check(TreeNode* root){
if(root==NULL) return 0;
int lh=check(root->left);
if(lh==-1) return -1;
int rh=check(root->right);
if(rh==-1) return -1;
if(abs(lh-rh)>1) return -1;

return max(lh,rh)+1;
}
};
31 changes: 31 additions & 0 deletions 1236-n-th-tribonacci-number/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h2><a href="https://leetcode.com/problems/n-th-tribonacci-number">N-th Tribonacci Number</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>The Tribonacci sequence T<sub>n</sub> is defined as follows:&nbsp;</p>

<p>T<sub>0</sub> = 0, T<sub>1</sub> = 1, T<sub>2</sub> = 1, and T<sub>n+3</sub> = T<sub>n</sub> + T<sub>n+1</sub> + T<sub>n+2</sub> for n &gt;= 0.</p>

<p>Given <code>n</code>, return the value of T<sub>n</sub>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 4
<strong>Explanation:</strong>
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 25
<strong>Output:</strong> 1389537
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>0 &lt;= n &lt;= 37</code></li>
<li>The answer is guaranteed to fit within a 32-bit integer, ie. <code>answer &lt;= 2^31 - 1</code>.</li>
</ul>
15 changes: 15 additions & 0 deletions 1236-n-th-tribonacci-number/n-th-tribonacci-number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int tribonacci(int n) {
if(n==0) return 0;
if(n==1 || n==2) return 1;
int a=0, b=1, c=1, result=0;
for(int i=3; i<=n; i++){
result = a+b+c;
a=b;
b=c;
c=result;
}
return result;
}
};
49 changes: 49 additions & 0 deletions 1287-distance-between-bus-stops/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<h2><a href="https://leetcode.com/problems/distance-between-bus-stops">Distance Between Bus Stops</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>A bus&nbsp;has <code>n</code> stops numbered from <code>0</code> to <code>n - 1</code> that form&nbsp;a circle. We know the distance between all pairs of neighboring stops where <code>distance[i]</code> is the distance between the stops number&nbsp;<code>i</code> and <code>(i + 1) % n</code>.</p>

<p>The bus goes along both directions&nbsp;i.e. clockwise and counterclockwise.</p>

<p>Return the shortest distance between the given&nbsp;<code>start</code>&nbsp;and <code>destination</code>&nbsp;stops.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/03/untitled-diagram-1.jpg" style="width: 388px; height: 240px;" /></p>

<pre>
<strong>Input:</strong> distance = [1,2,3,4], start = 0, destination = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> Distance between 0 and 1 is 1 or 9, minimum is 1.</pre>

<p>&nbsp;</p>

<p><strong class="example">Example 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/03/untitled-diagram-1-1.jpg" style="width: 388px; height: 240px;" /></p>

<pre>
<strong>Input:</strong> distance = [1,2,3,4], start = 0, destination = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Distance between 0 and 2 is 3 or 7, minimum is 3.
</pre>

<p>&nbsp;</p>

<p><strong class="example">Example 3:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2019/09/03/untitled-diagram-1-2.jpg" style="width: 388px; height: 240px;" /></p>

<pre>
<strong>Input:</strong> distance = [1,2,3,4], start = 0, destination = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> Distance between 0 and 3 is 6 or 4, minimum is 4.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= n&nbsp;&lt;= 10^4</code></li>
<li><code>distance.length == n</code></li>
<li><code>0 &lt;= start, destination &lt; n</code></li>
<li><code>0 &lt;= distance[i] &lt;= 10^4</code></li>
</ul>
27 changes: 27 additions & 0 deletions 1287-distance-between-bus-stops/distance-between-bus-stops.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

class Solution {
public:
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {

int front=0;
int back=0;

int n=distance.size();
for(int i=start;i!=destination;i=(i+1)%n)
{
front+=distance[i];
}

for(int i=destination;i!=start;i=(i+1)%n)
{
//i=i%n;
back+=distance[i];
}


return min(front,back);

}
};


39 changes: 39 additions & 0 deletions 1306-minimum-absolute-difference/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<h2><a href="https://leetcode.com/problems/minimum-absolute-difference">Minimum Absolute Difference</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Given an array of <strong>distinct</strong> integers <code>arr</code>, find all pairs of elements with the minimum absolute difference of any two elements.</p>

<p>Return a list of pairs in ascending order(with respect to pairs), each pair <code>[a, b]</code> follows</p>

<ul>
<li><code>a, b</code> are from <code>arr</code></li>
<li><code>a &lt; b</code></li>
<li><code>b - a</code> equals to the minimum absolute difference of any two elements in <code>arr</code></li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [4,2,1,3]
<strong>Output:</strong> [[1,2],[2,3],[3,4]]
<strong>Explanation: </strong>The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,3,6,10,15]
<strong>Output:</strong> [[1,3]]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [3,8,-10,23,19,-4,-14,27]
<strong>Output:</strong> [[-14,-10],[19,23],[23,27]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>6</sup> &lt;= arr[i] &lt;= 10<sup>6</sup></code></li>
</ul>
17 changes: 17 additions & 0 deletions 1306-minimum-absolute-difference/minimum-absolute-difference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
sort(arr.begin(), arr.end());
vector<vector<int>> res;
int min = INT_MAX;
for (int i = 1, n = arr.size(); i < n; ++i) {
int diff = arr[i] - arr[i - 1];
if (diff < min) {
min = diff;
res.clear();
}
if (diff == min) res.push_back({arr[i - 1], arr[i]});
}
return res;
}
};
45 changes: 45 additions & 0 deletions 1386-shift-2d-grid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<h2><a href="https://leetcode.com/problems/shift-2d-grid">Shift 2D Grid</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Given a 2D <code>grid</code> of size <code>m x n</code>&nbsp;and an integer <code>k</code>. You need to shift the <code>grid</code>&nbsp;<code>k</code> times.</p>

<p>In one shift operation:</p>

<ul>
<li>Element at <code>grid[i][j]</code> moves to <code>grid[i][j + 1]</code>.</li>
<li>Element at <code>grid[i][n - 1]</code> moves to <code>grid[i + 1][0]</code>.</li>
<li>Element at <code>grid[m&nbsp;- 1][n - 1]</code> moves to <code>grid[0][0]</code>.</li>
</ul>

<p>Return the <em>2D grid</em> after applying shift operation <code>k</code> times.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/05/e1.png" style="width: 400px; height: 178px;" />
<pre>
<strong>Input:</strong> <code>grid</code> = [[1,2,3],[4,5,6],[7,8,9]], k = 1
<strong>Output:</strong> [[9,1,2],[3,4,5],[6,7,8]]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/05/e2.png" style="width: 400px; height: 166px;" />
<pre>
<strong>Input:</strong> <code>grid</code> = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
<strong>Output:</strong> [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> <code>grid</code> = [[1,2,3],[4,5,6],[7,8,9]], k = 9
<strong>Output:</strong> [[1,2,3],[4,5,6],[7,8,9]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>m ==&nbsp;grid.length</code></li>
<li><code>n ==&nbsp;grid[i].length</code></li>
<li><code>1 &lt;= m &lt;= 50</code></li>
<li><code>1 &lt;= n &lt;= 50</code></li>
<li><code>-1000 &lt;= grid[i][j] &lt;= 1000</code></li>
<li><code>0 &lt;= k &lt;= 100</code></li>
</ul>
19 changes: 19 additions & 0 deletions 1386-shift-2d-grid/shift-2d-grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
int n = grid.size();
int m = grid[0].size();
int total = n * m;
k = k%total;
vector<vector<int>>res(n,vector<int>(m));
for(int i = 0;i<n;i++){
for(int j = 0;j<m;j++){
int p = (i*m+j+k)%total;
int newRow = p/m;
int newCol = p%m;
res[newRow][newCol] = grid[i][j];
}
}
return res;
}
};
41 changes: 41 additions & 0 deletions 1677-matrix-diagonal-sum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<h2><a href="https://leetcode.com/problems/matrix-diagonal-sum">Matrix Diagonal Sum</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Given a&nbsp;square&nbsp;matrix&nbsp;<code>mat</code>, return the sum of the matrix diagonals.</p>

<p>Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png" style="width: 336px; height: 174px;" />
<pre>
<strong>Input:</strong> mat = [[<strong>1</strong>,2,<strong>3</strong>],
&nbsp; [4,<strong>5</strong>,6],
&nbsp; [<strong>7</strong>,8,<strong>9</strong>]]
<strong>Output:</strong> 25
<strong>Explanation: </strong>Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
Notice that element mat[1][1] = 5 is counted only once.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> mat = [[<strong>1</strong>,1,1,<strong>1</strong>],
&nbsp; [1,<strong>1</strong>,<strong>1</strong>,1],
&nbsp; [1,<strong>1</strong>,<strong>1</strong>,1],
&nbsp; [<strong>1</strong>,1,1,<strong>1</strong>]]
<strong>Output:</strong> 8
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> mat = [[<strong>5</strong>]]
<strong>Output:</strong> 5
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>n == mat.length == mat[i].length</code></li>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>1 &lt;= mat[i][j] &lt;= 100</code></li>
</ul>
20 changes: 20 additions & 0 deletions 1677-matrix-diagonal-sum/matrix-diagonal-sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int diagonalSum(vector<vector<int>>& mat) {
int n = mat.size();
int sum = 0;

for (int i = 0; i < n; i++) {

sum = sum+ mat[i][i];

int m = n - 1 - i;
if (i != m) {
sum += mat[i][m];
}
}

return sum;
}
};

Loading