Skip to content

Create validSudoku.md #441

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
54 changes: 54 additions & 0 deletions Arrays/TrappingRainWater.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<h2><a href="https://leetcode.com/problems/trapping-rain-water/">42. Trapping Rain Water</a></h2><h3>Hard</h3><hr><div><p>Given <code>n</code> non-negative integers representing an elevation map where the width of each bar is <code>1</code>, compute how much water it can trap after raining.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png" style="width: 412px; height: 161px;">
<pre><strong>Input:</strong> height = [0,1,0,2,1,0,1,3,2,1,2,1]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
</pre>

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

<pre><strong>Input:</strong> height = [4,2,0,3,2,5]
<strong>Output:</strong> 9
</pre>

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

<ul>
<li><code>n == height.length</code></li>
<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= height[i] &lt;= 10<sup>5</sup></code></li>
</ul>
</div>

<h2>Code:</h2>

```cpp

class Solution {
public:
int trap(vector<int>& height) {
int rightMax=-1,leftMax=-1,n=height.size();
int left=0,right=n-1;
int ans;

while(left<=right){
if(height[left]<=height[right]){
if(height[left]>=leftMax) leftMax=height[left];
else ans+=leftMax-height[left];
left++;
}
else{
//height[right]>height[left]
if(height[right]>=rightMax) rightMax=height[right];
else ans+=rightMax-height[right];
right--;
}
}
return ans;
}
};
```
50 changes: 50 additions & 0 deletions Arrays/mergeIntervals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<h2><a href="https://leetcode.com/problems/merge-intervals/">56. Merge Intervals</a></h2><h3>Medium</h3><hr><div><p>Given an array&nbsp;of <code>intervals</code>&nbsp;where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return <em>an array of the non-overlapping intervals that cover all the intervals in the input</em>.</p>

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

<pre><strong>Input:</strong> intervals = [[1,3],[2,6],[8,10],[15,18]]
<strong>Output:</strong> [[1,6],[8,10],[15,18]]
<strong>Explanation:</strong> Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
</pre>

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

<pre><strong>Input:</strong> intervals = [[1,4],[4,5]]
<strong>Output:</strong> [[1,5]]
<strong>Explanation:</strong> Intervals [1,4] and [4,5] are considered overlapping.
</pre>

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

<ul>
<li><code>1 &lt;= intervals.length &lt;= 10<sup>4</sup></code></li>
<li><code>intervals[i].length == 2</code></li>
<li><code>0 &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt;= 10<sup>4</sup></code></li>
</ul>
</div>

<h2>Code:</h2>

```cpp
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> ans;
sort(intervals.begin(),intervals.end());
ans.push_back(intervals[0]);
int j=0;
for(int i=1;i<intervals.size();i++){
if(ans[j][1]>=intervals[i][0]){
ans[j][1]=max(ans[j][1],intervals[i][1]);
}
else{
j++;
ans.push_back(intervals[i]);
}
}
return ans;
}
};
```
66 changes: 66 additions & 0 deletions Arrays/spiralMatrix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<h2><a href="https://leetcode.com/problems/spiral-matrix-ii/">59. Spiral Matrix II</a></h2><h3>Medium</h3><hr><div><p>Given a positive integer <code>n</code>, generate an <code>n x n</code> <code>matrix</code> filled with elements from <code>1</code> to <code>n<sup>2</sup></code> in spiral order.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg" style="width: 242px; height: 242px;">
<pre><strong>Input:</strong> n = 3
<strong>Output:</strong> [[1,2,3],[8,9,4],[7,6,5]]
</pre>

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

<pre><strong>Input:</strong> n = 1
<strong>Output:</strong> [[1]]
</pre>

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

<ul>
<li><code>1 &lt;= n &lt;= 20</code></li>
</ul>
</div>

<h2>Code:</h2>

```cpp
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {

vector<vector<int>>m(n,vector<int>(n,0));
int c=1;
int left=0,right=n-1,top=0,bottom=n-1;
while(left<=right && top<=bottom){

for(int i=left;i<=right;i++){
m[top][i]=c;
c++;
}
top++;

for(int i=top;i<=bottom;i++){
m[i][right]=c;
c++;
}
right--;

for(int i=right;i>=left;i--){
m[bottom][i]=c;
c++;
}
bottom--;


for(int i=bottom;i>=top;i--){
m[i][left]=c;
c++;
}
left++;

}
return m;
}
};

```
96 changes: 96 additions & 0 deletions Backtracking/validSudoku.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<h2><a href="https://leetcode.com/problems/valid-sudoku/">36. Valid Sudoku</a></h2><h3>Medium</h3><hr><div><p>Determine if a&nbsp;<code>9 x 9</code> Sudoku board&nbsp;is valid.&nbsp;Only the filled cells need to be validated&nbsp;<strong>according to the following rules</strong>:</p>

<ol>
<li>Each row&nbsp;must contain the&nbsp;digits&nbsp;<code>1-9</code> without repetition.</li>
<li>Each column must contain the digits&nbsp;<code>1-9</code>&nbsp;without repetition.</li>
<li>Each of the nine&nbsp;<code>3 x 3</code> sub-boxes of the grid must contain the digits&nbsp;<code>1-9</code>&nbsp;without repetition.</li>
</ol>

<p><strong>Note:</strong></p>

<ul>
<li>A Sudoku board (partially filled) could be valid but is not necessarily solvable.</li>
<li>Only the filled cells need to be validated according to the mentioned&nbsp;rules.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png" style="height:250px; width:250px">
<pre><strong>Input:</strong> board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
<strong>Output:</strong> true
</pre>

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

<pre><strong>Input:</strong> board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
<strong>Output:</strong> false
<strong>Explanation:</strong> Same as Example 1, except with the <strong>5</strong> in the top left corner being modified to <strong>8</strong>. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
</pre>

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

<ul>
<li><code>board.length == 9</code></li>
<li><code>board[i].length == 9</code></li>
<li><code>board[i][j]</code> is a digit <code>1-9</code> or <code>'.'</code>.</li>
</ul>
</div>


<h2>Code: </h2>

```cpp
class Solution {
public:
bool isValid(vector<vector<char>>& board, int row, int col){
int c = board[row][col];
board[row][col] = '.';
for(int i=0;i<9;i++)
if(board[row][i] == c) return false;
for(int i=0;i<9;i++)
if(board[i][col] == c) return false;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(board[row/3*3 + i][col/3*3 + j] == c)
return false;

board[row][col] = c;
return true;
}

bool isValidSudoku(vector<vector<char>>& board) {

// Go for all the rows & columns looking for an invalid number
// If you find an invalid entry, return false
// If you don not find an invalid entry till the end, it has to be good, return true.
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j]!='.' && !isValid(board,i,j)){
return false;
}
}
}
return true;
}
};

```