diff --git a/Arrays/TrappingRainWater.md b/Arrays/TrappingRainWater.md new file mode 100644 index 0000000..6e98d29 --- /dev/null +++ b/Arrays/TrappingRainWater.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:

+ + +
+ +

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; + } +}; +``` 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:

+ + +
+ +

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; + } +}; +``` 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; + } +}; + +``` 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:

+ +
    +
  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • +
  • Only the filled cells need to be validated according to the mentioned rules.
  • +
+ +

 

+

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:

+ +
    +
  • board.length == 9
  • +
  • board[i].length == 9
  • +
  • board[i][j] is a digit 1-9 or '.'.
  • +
+
+ + +

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; + } +}; + +```