From 98d689dd7b29e56ead7160a0fd03975797f05fd5 Mon Sep 17 00:00:00 2001 From: Aditya Nigam <75134530+Adi2209@users.noreply.github.com> Date: Sun, 21 May 2023 11:56:03 +0530 Subject: [PATCH 1/5] Create validSudoku.md Check if a sudoku is valid or not. Solved using the backtracking concept. --- Backtracking/validSudoku.md | 96 +++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Backtracking/validSudoku.md diff --git a/Backtracking/validSudoku.md b/Backtracking/validSudoku.md new file mode 100644 index 0000000..4cf3181 --- /dev/null +++ b/Backtracking/validSudoku.md @@ -0,0 +1,96 @@ +

36. Valid Sudoku

Medium


Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

+ +
    +
  1. Each row must contain the digits 1-9 without repetition.
  2. +
  3. Each column must contain the digits 1-9 without repetition.
  4. +
  5. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
  6. +
+ +

Note:

+ + + +

 

+

Example 1:

+ +
Input: 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"]]
+Output: true
+
+ +

Example 2:

+ +
Input: 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"]]
+Output: false
+Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
+
+ +

 

+

Constraints:

+ + +
+ + +

Code:

+ +```cpp +class Solution { +public: + bool isValid(vector>& 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>& 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; + } +}; + +``` From 973f965fd416a50b514dd5dc22b6302befaf1a17 Mon Sep 17 00:00:00 2001 From: Aditya Nigam <75134530+Adi2209@users.noreply.github.com> Date: Sun, 21 May 2023 12:00:33 +0530 Subject: [PATCH 2/5] Create TrappingRainWater.md Leetcode Hard Level question added. --- Arrays/TraapingRainWater.md | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Arrays/TraapingRainWater.md diff --git a/Arrays/TraapingRainWater.md b/Arrays/TraapingRainWater.md new file mode 100644 index 0000000..6e98d29 --- /dev/null +++ b/Arrays/TraapingRainWater.md @@ -0,0 +1,54 @@ +

42. Trapping Rain Water

Hard


Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

+ +

 

+

Example 1:

+ +
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
+Output: 6
+Explanation: 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.
+
+ +

Example 2:

+ +
Input: height = [4,2,0,3,2,5]
+Output: 9
+
+ +

 

+

Constraints:

+ +
    +
  • n == height.length
  • +
  • 1 <= n <= 2 * 104
  • +
  • 0 <= height[i] <= 105
  • +
+
+ +

Code:

+ +```cpp + +class Solution { +public: + int trap(vector& 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; + } +}; +``` From da848d4ee733af65924fdcd9aadb535e5c861221 Mon Sep 17 00:00:00 2001 From: Aditya Nigam <75134530+Adi2209@users.noreply.github.com> Date: Sun, 21 May 2023 12:00:52 +0530 Subject: [PATCH 3/5] Rename TraapingRainWater.md to TrappingRainWater.md --- Arrays/{TraapingRainWater.md => TrappingRainWater.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Arrays/{TraapingRainWater.md => TrappingRainWater.md} (100%) diff --git a/Arrays/TraapingRainWater.md b/Arrays/TrappingRainWater.md similarity index 100% rename from Arrays/TraapingRainWater.md rename to Arrays/TrappingRainWater.md From 0603e629175208a408cf4a9987015bc845e269d1 Mon Sep 17 00:00:00 2001 From: Aditya Nigam <75134530+Adi2209@users.noreply.github.com> Date: Sun, 21 May 2023 12:07:19 +0530 Subject: [PATCH 4/5] Create mergeIntervals.md --- Arrays/mergeIntervals.md | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Arrays/mergeIntervals.md diff --git a/Arrays/mergeIntervals.md b/Arrays/mergeIntervals.md new file mode 100644 index 0000000..9d1edcc --- /dev/null +++ b/Arrays/mergeIntervals.md @@ -0,0 +1,50 @@ +

56. Merge Intervals

Medium


Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

+ +

 

+

Example 1:

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

Example 2:

+ +
Input: intervals = [[1,4],[4,5]]
+Output: [[1,5]]
+Explanation: Intervals [1,4] and [4,5] are considered overlapping.
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= intervals.length <= 104
  • +
  • intervals[i].length == 2
  • +
  • 0 <= starti <= endi <= 104
  • +
+
+ +

Code:

+ +```cpp +class Solution { +public: + vector> merge(vector>& intervals) { + vector> ans; + sort(intervals.begin(),intervals.end()); + ans.push_back(intervals[0]); + int j=0; + for(int i=1;i=intervals[i][0]){ + ans[j][1]=max(ans[j][1],intervals[i][1]); + } + else{ + j++; + ans.push_back(intervals[i]); + } + } + return ans; + } +}; +``` From dcefb448ec3877f5f5021093db72e0f37964bd48 Mon Sep 17 00:00:00 2001 From: Aditya Nigam <75134530+Adi2209@users.noreply.github.com> Date: Sun, 21 May 2023 12:23:54 +0530 Subject: [PATCH 5/5] Create spiralMatrix.md --- Arrays/spiralMatrix.md | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Arrays/spiralMatrix.md diff --git a/Arrays/spiralMatrix.md b/Arrays/spiralMatrix.md new file mode 100644 index 0000000..da6cfa6 --- /dev/null +++ b/Arrays/spiralMatrix.md @@ -0,0 +1,66 @@ +

59. Spiral Matrix II

Medium


Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

+ +

 

+

Example 1:

+ +
Input: n = 3
+Output: [[1,2,3],[8,9,4],[7,6,5]]
+
+ +

Example 2:

+ +
Input: n = 1
+Output: [[1]]
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= n <= 20
  • +
+
+ +

Code:

+ +```cpp +class Solution { +public: + vector> generateMatrix(int n) { + + vector>m(n,vector(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; + } +}; + +```