Skip to content

Commit 6917c56

Browse files
committed
created README.md
Signed-off-by: rajput-hemant <[email protected]>
1 parent a8bb597 commit 6917c56

File tree

23 files changed

+1903
-0
lines changed
  • src
    • 0001-0100
    • 0101-0200
    • 0201-0300
    • 0301-0400/369 - Plus One Linked List
    • 0501-0600/543 - Diameter of Binary Tree
    • 0601-0700/653 - Two Sum IV - Input is a BST
    • 1001-1100/1047 - Remove All Adjacent Duplicates In String
    • 1401-1500
      • 1461 - Check If a String Contains All Binary Codes of Size K
      • 1480 - Running Sum of 1d Array
    • 1501-1600/1537 - Get the Maximum Score
    • 2201-2300/2236 - Root Equals Sum of Children

23 files changed

+1903
-0
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 4. Median of Two Sorted Arrays [![share]](https://leetcode.com/problems/median-of-two-sorted-arrays)
2+
3+
![][hard]
4+
5+
## Problem Statement:
6+
7+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
8+
9+
The overall run time complexity should be `O(log (m+n))`.
10+
11+
### Example 1:
12+
13+
```
14+
Input: nums1 = [1,3], nums2 = [2]
15+
Output: 2.00000
16+
Explanation: merged array = [1,2,3] and median is 2.
17+
```
18+
19+
### Example 2:
20+
21+
```
22+
Input: nums1 = [1,2], nums2 = [3,4]
23+
Output: 2.50000
24+
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
25+
```
26+
27+
### Constraints:
28+
29+
- nums1.length == m
30+
- nums2.length == n
31+
- 0 <= m <= 1000
32+
- 0 <= n <= 1000
33+
- 1 <= m + n <= 2000
34+
- -10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup>
35+
36+
## Solutions
37+
38+
### [_Java_](./MedianOfTwoSortedArrays.java)
39+
40+
```java
41+
public class MedianOfTwoSortedArrays {
42+
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
43+
int[] merged = new int[nums1.length + nums2.length];
44+
int startnums1 = 0, startnums2 = 0, i = 0;
45+
while (startnums1 < nums1.length && startnums2 < nums2.length) {
46+
if (nums1[startnums1] < nums2[startnums2])
47+
merged[i++] = nums1[startnums1++];
48+
else
49+
merged[i++] = nums2[startnums2++];
50+
}
51+
while (startnums1 < nums1.length)
52+
merged[i++] = nums1[startnums1++];
53+
while (startnums2 < nums2.length)
54+
merged[i++] = nums2[startnums2++];
55+
double median = 0;
56+
int mid = merged.length / 2;
57+
if (merged.length % 2 == 0)
58+
median = (double) (merged[mid] + merged[mid - 1]) / 2;
59+
else
60+
median = (double) merged[mid];
61+
return median;
62+
}
63+
}
64+
```
65+
66+
### [_..._]()
67+
68+
```
69+
70+
```
71+
72+
<!----------------------------------{ link }--------------------------------->
73+
74+
[share]: https://img.icons8.com/external-anggara-blue-anggara-putra/20/000000/external-share-user-interface-basic-anggara-blue-anggara-putra-2.png
75+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg

Diff for: src/0001-0100/024 - Swap Nodes in Pairs/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# 24. Swap Nodes in Pairs [![share]](https://leetcode.com/problems/swap-nodes-in-pairs)
2+
3+
![][medium]
4+
5+
## Problem Statement:
6+
7+
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
8+
9+
![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)
10+
11+
### Example 1:
12+
13+
```
14+
Input: head = [1,2,3,4]
15+
Output: [2,1,4,3]
16+
```
17+
18+
### Example 2:
19+
20+
```
21+
Input: head = []
22+
Output: []
23+
```
24+
25+
### Example 3:
26+
27+
```
28+
Input: head = [1]
29+
Output: [1]
30+
```
31+
32+
### Constraints:
33+
34+
- The number of nodes in the list is in the range `[0, 100]`.
35+
- 0 <= Node.val <= 100
36+
37+
## Solutions:
38+
39+
### [_Java_](./SwapNodesInPairs.java)
40+
41+
```java
42+
public class SwapNodesInPairs {
43+
44+
public ListNode swapPairs(ListNode head) {
45+
ListNode newNode = head;
46+
while (newNode != null && newNode.next != null) {
47+
int temp = newNode.val;
48+
newNode.val = newNode.next.val;
49+
newNode.next.val = temp;
50+
newNode = newNode.next.next;
51+
}
52+
return head;
53+
}
54+
}
55+
```
56+
57+
### [_..._]()
58+
59+
```
60+
61+
```
62+
63+
<!----------------------------------{ link }--------------------------------->
64+
65+
[share]: https://img.icons8.com/external-anggara-blue-anggara-putra/20/000000/external-share-user-interface-basic-anggara-blue-anggara-putra-2.png
66+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# 26. Remove Duplicates from Sorted Array [![share]](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)
2+
3+
![][easy]
4+
5+
## Problem Statement:
6+
7+
Given an integer array `nums` sorted in **non-decreasing order**, remove the duplicates in-place such that each unique element appears only once. The **relative order** of the elements should be kept the **same**.
8+
9+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
10+
11+
Return `k` after placing the final result in the first `k` slots of `nums`.
12+
13+
Do **not** allocate extra space for another array. You must do this by **modifying the input array** in-place with O(1) extra memory.
14+
15+
### Custom Judge:
16+
17+
```
18+
The judge will test your solution with the following code:
19+
20+
int[] nums = [...]; // Input array
21+
int[] expectedNums = [...]; // The expected answer with correct length
22+
23+
int k = removeDuplicates(nums); // Calls your implementation
24+
25+
assert k == expectedNums.length;
26+
for (int i = 0; i < k; i++) {
27+
assert nums[i] == expectedNums[i];
28+
}
29+
```
30+
31+
If all assertions pass, then your solution will be **accepted**.
32+
33+
### Example 1:
34+
35+
```
36+
Input: nums = [1,1,2]
37+
Output: 2, nums = [1,2,_]
38+
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
39+
It does not matter what you leave beyond the returned k (hence they are underscores).
40+
```
41+
42+
### Example 2:
43+
44+
```
45+
Input: nums = [0,0,1,1,1,2,2,3,3,4]
46+
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
47+
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
48+
It does not matter what you leave beyond the returned k (hence they are underscores).
49+
```
50+
51+
### Constraints:
52+
53+
- 1 <= nums.length <= 3 \* 10<sup>4</sup>
54+
- -100 <= nums[i] <= 100
55+
- nums is sorted in non-decreasing order.
56+
57+
## Solutions:
58+
59+
### [_Python_](./RemoveDuplicatesFromSortedArray.py)
60+
61+
```python
62+
def removeDuplicates(nums: list[int]) -> int:
63+
k = 0
64+
for i, item in enumerate(nums):
65+
if i < len(nums) - 1 and item == nums[i + 1]:
66+
continue
67+
nums[k] = item
68+
k += 1
69+
return k
70+
```
71+
72+
### [_..._]()
73+
74+
```
75+
76+
```
77+
78+
<!----------------------------------{ link }--------------------------------->
79+
80+
[share]: https://img.icons8.com/external-anggara-blue-anggara-putra/20/000000/external-share-user-interface-basic-anggara-blue-anggara-putra-2.png
81+
[easy]: https://img.shields.io/badge/Difficulty-Easy-green.svg

Diff for: src/0001-0100/037 - Sudoku Solver/README.md

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# 37. Sudoku Solver [![share]](https://leetcode.com/problems/sudoku-solver/)
2+
3+
![][hard]
4+
5+
## Problem Statement:
6+
7+
Write a program to solve a Sudoku puzzle by filling the empty cells.
8+
9+
A sudoku solution must satisfy **all of the following rules:**
10+
11+
- Each of the digits `1-9` must occur exactly once in each row.
12+
- Each of the digits `1-9` must occur exactly once in each column.
13+
- Each of the digits `1-9` must occur exactly once in each of the 9 `3x3` sub-boxes of the grid.
14+
- The '.' character indicates empty cells.
15+
16+
### Example 1:
17+
18+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
19+
20+
```
21+
Input: board =
22+
[["5","3",".",".","7",".",".",".","."],
23+
["6",".",".","1","9","5",".",".","."],
24+
[".","9","8",".",".",".",".","6","."],
25+
["8",".",".",".","6",".",".",".","3"],
26+
["4",".",".","8",".","3",".",".","1"],
27+
["7",".",".",".","2",".",".",".","6"],
28+
[".","6",".",".",".",".","2","8","."],
29+
[".",".",".","4","1","9",".",".","5"],
30+
[".",".",".",".","8",".",".","7","9"]]
31+
32+
Output:
33+
[["5","3","4","6","7","8","9","1","2"],
34+
["6","7","2","1","9","5","3","4","8"],
35+
["1","9","8","3","4","2","5","6","7"],
36+
["8","5","9","7","6","1","4","2","3"],
37+
["4","2","6","8","5","3","7","9","1"],
38+
["7","1","3","9","2","4","8","5","6"],
39+
["9","6","1","5","3","7","2","8","4"],
40+
["2","8","7","4","1","9","6","3","5"],
41+
["3","4","5","2","8","6","1","7","9"]]
42+
```
43+
44+
**Explanation**: The input board is shown above and the only valid solution is shown below:
45+
46+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png)
47+
48+
### Constraints:
49+
50+
- board.length == 9
51+
- board[i].length == 9
52+
- board[i][j] is a digit or '.'.
53+
- It is **guaranteed** that the input board has only one solution.
54+
55+
## Solutions:
56+
57+
### [_Java_](./SudokuSolver2.java)
58+
59+
```java
60+
/*
61+
A sudoku solution must satisfy all of the following rules:
62+
-> Each of the digits 1-9 must occur exactly once in each row.
63+
-> Each of the digits 1-9 must occur exactly once in each column.
64+
-> Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
65+
The "." character indicates empty cells.
66+
*/
67+
68+
public class SudokuSolver2 {
69+
public void solveSudoku(char[][] board) {
70+
helper(board, 0, 0);
71+
}
72+
73+
// a utility function to solve the board
74+
boolean helper(char[][] board, int row, int col) {
75+
// base case -> recursion stops at the 10th row
76+
if (row == board.length)
77+
return true;
78+
int newRow = 0, newCol = 0;
79+
// when its the last col of the board, goto next row & first col
80+
if (col == board.length - 1) {
81+
newRow = row + 1;
82+
newCol = 0;
83+
}
84+
// else, keep increasing the col by one & the row remains the same
85+
else {
86+
newRow = row;
87+
newCol = col + 1;
88+
}
89+
// if the cell isn't empty, i.e. there is a number present in that cell
90+
if (board[row][col] != '.') {
91+
// do a recursive call, if the fn returns true, i.e. the board is solved,
92+
// we'll also return true
93+
if (helper(board, newRow, newCol))
94+
return true;
95+
}
96+
// if cell is empty
97+
else {
98+
for (int i = 1; i <= 9; i++) {
99+
// if it's safe to place 'i' at that cell
100+
if (isValidPlacement(board, row, col, i)) {
101+
board[row][col] = (char) (i + '0');
102+
// after placing the number, do a recursive call,
103+
// if the fn returns true, we'll also return true
104+
if (helper(board, newRow, newCol))
105+
return true;
106+
// if the recursive call returns false, i.e. it wasn't safe to place 'i',
107+
// we'll empty the cell & and will try for the next value of 'i' (backtracking)
108+
else
109+
board[row][col] = '.';
110+
}
111+
}
112+
}
113+
// return false if it isn't posssible to place a number in the cell
114+
return false;
115+
}
116+
117+
// a utility fn to check for valid placement of a number in cell of Sudoku Board
118+
boolean isValidPlacement(char[][] board, int row, int col, int number) {
119+
// to check if 'number' is present in the row or the col
120+
for (int i = 0; i < board.length; i++) {
121+
// return false if 'number' is present in the col
122+
if (board[i][col] == (char) (number + '0'))
123+
return false;
124+
// return false if 'number' is present in the row
125+
if (board[row][i] == (char) (number + '0'))
126+
return false;
127+
}
128+
// to check if the 'number' is present in the 3X3 grid
129+
// There are two ways to get the initial row and column of 3X3 grid
130+
// 1
131+
int startingRow = (row / 3) * 3;
132+
int startingCol = (col / 3) * 3;
133+
// 2
134+
// int startingRow = (row % 3) - row;
135+
// int startingCol = (col % 3) - col;
136+
for (int i = startingRow; i < startingRow + 3; i++)
137+
for (int j = startingCol; j < startingCol + 3; j++)
138+
// return false if 'number' is present in the 3X3 grid or matrix
139+
if (board[i][j] == (char) (number + '0'))
140+
return false;
141+
// return true if 'number' isn't present in row, col & grid
142+
return true;
143+
}
144+
}
145+
```
146+
147+
### [_..._]()
148+
149+
```
150+
151+
```
152+
153+
<!----------------------------------{ link }--------------------------------->
154+
155+
[share]: https://img.icons8.com/external-anggara-blue-anggara-putra/20/000000/external-share-user-interface-basic-anggara-blue-anggara-putra-2.png
156+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg

0 commit comments

Comments
 (0)