Skip to content

Commit ffa9a03

Browse files
author
Sanajit Jana
committed
Add questions
1 parent 597fe2c commit ffa9a03

File tree

20 files changed

+1809
-96
lines changed

20 files changed

+1809
-96
lines changed

Hard/0042-trapping-rain-water.java

Lines changed: 0 additions & 1 deletion
This file was deleted.

Medium/0036-valid-sudoku.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

p/0036-valid-sudoku/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/)
2+
3+
## Question Description
4+
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to these rules:
5+
1. Each row must contain the digits 1-9 without repetition.
6+
2. Each column must contain the digits 1-9 without repetition.
7+
3. Each of the nine 3 x 3 sub-boxes must contain the digits 1-9 without repetition.
8+
9+
Note: A Sudoku board (partially filled) could be valid but is not necessarily solvable.
10+
11+
---
12+
13+
## Constraints
14+
- `board.length == 9`
15+
- `board[i].length == 9`
16+
- `board[i][j]` is a digit `'1'-'9'` or `'.'`
17+
- The board may be partially filled
18+
19+
---
20+
21+
## Approach
22+
Use HashSet to track digits in each row, column, and 3x3 sub-box.
23+
24+
**Why this approach works:**
25+
- HashSet provides O(1) average time complexity for lookup and insertion operations
26+
- We need to ensure no duplicate digits appear in rows, columns, or 3x3 boxes
27+
- By using separate HashSets for each validation, we can efficiently track seen digits
28+
29+
**Alternative approaches considered:**
30+
- Could use arrays instead of HashSets for digit tracking, but HashSets are more readable and handle the logic more elegantly
31+
- Could use bitwise operations for memory optimization, but HashSet approach is clearer and sufficient for this problem
32+
33+
---
34+
35+
## Dry Run
36+
Example Input:
37+
```
38+
board = [
39+
["5","3",".",".","7",".",".",".","."],
40+
["6",".",".","1","9","5",".",".","."],
41+
[".","9","8",".",".",".",".","6","."],
42+
["8",".",".",".","6",".",".",".","3"],
43+
["4",".",".","8",".","3",".",".","1"],
44+
["7",".",".",".","2",".",".",".","6"],
45+
[".","6",".",".",".",".","2","8","."],
46+
[".",".",".","4","1","9",".",".","5"],
47+
[".",".",".",".","8",".",".","7","9"]
48+
]
49+
```
50+
51+
Step-by-step execution:
52+
- Step 1: Validate all rows - check each row has unique digits (skip '.')
53+
- Step 2: Validate all columns - check each column has unique digits (skip '.')
54+
- Step 3: Validate all 3x3 sub-boxes - check each 3x3 block has unique digits (skip '.')
55+
56+
Final Answer = `true` (this is a valid Sudoku board)
57+
58+
---
59+
60+
## Solution
61+
```java
62+
import java.util.HashSet;
63+
import java.util.Set;
64+
65+
class Solution {
66+
public boolean isValidSudoku(char[][] board) {
67+
68+
int m = board.length;
69+
int n = board[0].length;
70+
71+
// Check rows
72+
for (int i = 0; i < m; i++) {
73+
Set<Character> set = new HashSet<>();
74+
for (int j = 0; j < n; j++) {
75+
char ch = board[i][j];
76+
if (ch != '.') {
77+
if (set.contains(ch))
78+
return false;
79+
else
80+
set.add(ch);
81+
}
82+
}
83+
}
84+
85+
// Check cols
86+
for (int i = 0; i < m; i++) {
87+
Set<Character> set = new HashSet<>();
88+
for (int k = 0; k < m; k++) {
89+
char ch = board[k][i];
90+
if (ch != '.') {
91+
if (set.contains(ch))
92+
return false;
93+
else
94+
set.add(ch);
95+
}
96+
}
97+
}
98+
99+
// Check 3x3 sub-boxes
100+
for (int blockRow = 0; blockRow < 3; blockRow++) {
101+
for (int blockCol = 0; blockCol < 3; blockCol++) {
102+
Set<Character> set = new HashSet<>();
103+
for (int i = 0; i < 3; i++) {
104+
for (int j = 0; j < 3; j++) {
105+
char ch = board[blockRow * 3 + i][blockCol * 3 + j];
106+
if (ch != '.') {
107+
if (set.contains(ch))
108+
return false;
109+
set.add(ch);
110+
}
111+
}
112+
}
113+
}
114+
}
115+
116+
return true;
117+
}
118+
}
119+
```
120+
121+
---
122+
123+
## Time and Space Complexity
124+
- **Time Complexity:** O(1) - We scan each cell at most 3 times (row, column, box validation), board size is fixed at 9x9
125+
- **Space Complexity:** O(1) - Extra space comes from HashSets used in validation, but board size is fixed so memory usage is constant

p/0073-set-matrix-zeroes/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/)
2+
3+
## Question Description
4+
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
5+
6+
You must do it in place.
7+
8+
---
9+
10+
## Constraints
11+
- `m == matrix.length`
12+
- `n == matrix[0].length`
13+
- `1 <= m, n <= 200`
14+
- `-2^31 <= matrix[i][j] <= 2^31 - 1`
15+
16+
---
17+
18+
## Approach
19+
Use the first row and first column as markers to record which rows/columns should be zeroed. Track separately if the first row or first column need to be zeroed to avoid losing marker info. In the second pass, set entire rows/columns to zero based on these markers.
20+
21+
**Why this approach works:**
22+
- Uses O(1) extra space by utilizing the matrix itself as storage
23+
- First pass marks which rows and columns need to be zeroed
24+
- Second pass actually sets the zeros
25+
- Handles edge cases for first row and first column separately
26+
27+
**Alternative approaches considered:**
28+
- Could use separate arrays to track rows and columns, but that would use O(m+n) space
29+
- Could use a set to store positions, but that would also use extra space
30+
31+
---
32+
33+
## Dry Run
34+
Example Input: `matrix = [[1,1,1],[1,0,1],[1,1,1]]`
35+
36+
Step-by-step execution:
37+
- Step 1: Find 0 at position [1][1], mark row 1 and column 1
38+
- Step 2: Set matrix[0][1] = 0 and matrix[1][0] = 0 as markers
39+
- Step 3: Set entire column 1 to 0 (except already processed cells)
40+
- Step 4: Set entire row 1 to 0 (except already processed cells)
41+
- Step 5: Handle first row and first column if needed
42+
43+
Final Answer = `[[1,0,1],[0,0,0],[1,0,1]]`
44+
45+
---
46+
47+
## Solution
48+
```java
49+
class Solution {
50+
public void setZeroes(int[][] matrix) {
51+
int m = matrix.length;
52+
int n = matrix[0].length;
53+
54+
boolean r0F = false;
55+
boolean c0F = false;
56+
57+
for (int i = 0; i < m; i++) {
58+
for (int j = 0; j < n; j++) {
59+
if (i == 0 && matrix[i][j] == 0) r0F = true;
60+
if (j == 0 && matrix[i][j] == 0) c0F = true;
61+
if (matrix[i][j] == 0) {
62+
matrix[0][j] = 0;
63+
matrix[i][0] = 0;
64+
}
65+
}
66+
}
67+
68+
for (int i = 1; i < n; i++) {
69+
if (matrix[0][i] == 0) {
70+
for (int j = 0; j < m; j++) {
71+
matrix[j][i] = 0;
72+
}
73+
}
74+
}
75+
76+
for (int i = 1; i < m; i++) {
77+
if (matrix[i][0] == 0) {
78+
for (int j = 0; j < n; j++) {
79+
matrix[i][j] = 0;
80+
}
81+
}
82+
}
83+
84+
if (r0F) {
85+
for (int i = 0; i < n; i++) {
86+
matrix[0][i] = 0;
87+
}
88+
}
89+
90+
if (c0F) {
91+
for (int i = 0; i < m; i++) {
92+
matrix[i][0] = 0;
93+
}
94+
}
95+
}
96+
}
97+
```
98+
99+
---
100+
101+
## Time and Space Complexity
102+
- **Time Complexity:** O(m * n) - each cell is visited a constant number of times
103+
- **Space Complexity:** O(1) - in-place, only a few extra variables

0 commit comments

Comments
 (0)