Skip to content

Commit a5bc0a4

Browse files
author
JenilGajjar20
committed
merge change
2 parents 1b1ad7f + b0da29f commit a5bc0a4

File tree

123 files changed

+5035
-55
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+5035
-55
lines changed

.DS_Store

6 KB
Binary file not shown.

Cpp/.DS_Store

20 KB
Binary file not shown.

Cpp/0051-N-Queen-I/problem.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# N-Queens
2+
3+
## Problem Description
4+
The **N-Queens** puzzle requires placing `n` queens on an `n x n` chessboard such that no two queens threaten each other. A queen can attack any piece in the same row, column, or diagonal.
5+
6+
Given an integer `n`, the task is to find all distinct solutions to the N-Queens puzzle. Each solution should be represented as a distinct board configuration where `'Q'` denotes a queen, and `'.'` denotes an empty square.
7+
8+
## Objective
9+
Return all possible configurations of the N-Queens puzzle in any order, where each configuration is a list of strings representing the chessboard.
10+
11+
---
12+
13+
### Constraints
14+
- `1 <= n <= 9`
15+
16+
### Example
17+
**Input**
18+
```plaintext
19+
n = 4
20+
```
21+
22+
23+
**Output**
24+
```plaintext
25+
[[".Q..","...Q","Q...","..Q."],
26+
["..Q.","Q...","...Q",".Q.."]]
27+
```

Cpp/0051-N-Queen-I/solution.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
public:
3+
// Recursive function to place queens row by row
4+
void rec(int r, int n, vector<string>& ve, vector<pair<int, int>>& queens, vector<vector<string>>& out) {
5+
// Base case: if we've placed queens in all rows, add current board configuration to output
6+
if (r == n) {
7+
out.push_back(ve);
8+
return;
9+
}
10+
11+
// Try placing a queen in each column of the current row
12+
for (int c = 0; c < n; c++) {
13+
bool poss = true;
14+
15+
// Check against previously placed queens to see if (r, c) is a valid position
16+
for (auto& p : queens) {
17+
int xr = p.first;
18+
int xc = p.second;
19+
20+
// A queen can attack if in the same row, column, or diagonal
21+
if (xr == r || xc == c || abs(xr - r) == abs(xc - c)) {
22+
poss = false;
23+
break;
24+
}
25+
}
26+
27+
// If (r, c) is not a valid position, skip to the next column
28+
if (!poss) continue;
29+
30+
// Place the queen at (r, c) and mark it on the board
31+
queens.push_back({r, c});
32+
ve[r][c] = 'Q';
33+
34+
// Recurse to the next row to place the next queen
35+
rec(r + 1, n, ve, queens, out);
36+
37+
// Backtrack: remove the queen from (r, c) and unmark it on the board
38+
ve[r][c] = '.';
39+
queens.pop_back();
40+
}
41+
}
42+
43+
// Main function to initiate the N-Queens solution finding
44+
vector<vector<string>> solveNQueens(int n) {
45+
vector<vector<string>> out;
46+
vector<string> ve(n, string(n, '.'));
47+
vector<pair<int, int>> queens;
48+
rec(0, n, ve, queens, out);
49+
return out;
50+
}
51+
};

Cpp/0052-N-Queen-II/problem.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# N-Queens II
2+
3+
## Problem Description
4+
The **N-Queens puzzle** is the problem of placing `n` queens on an `n x n` chessboard so that no two queens can attack each other (no same row, column, or diagonal). This variation of the problem, N-Queens II, requires counting the number of distinct solutions rather than returning each configuration.
5+
6+
Given an integer `n`, return the number of unique ways to arrange the queens on the chessboard.
7+
8+
## Objective
9+
Return the count of all valid configurations for the N-Queens puzzle, where each configuration ensures that no two queens attack each other.
10+
11+
---
12+
13+
### Constraints
14+
- `1 <= n <= 9`
15+
16+
### Examples
17+
18+
**Example 1**
19+
20+
**Input**
21+
```plaintext
22+
n = 4
23+
```
24+
25+
**Output**
26+
```plaintext
27+
2
28+
```
29+
30+
**Example 2**
31+
32+
**Input**
33+
```plaintext
34+
n = 1
35+
```
36+
37+
**Output**
38+
```plaintext
39+
1
40+
```

Cpp/0052-N-Queen-II/solution.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public:
3+
// Recursive function to count valid queen placements row by row
4+
int rec(int r, int n, vector<pair<int, int>>& queens) {
5+
// Base case: if all queens are placed, count this configuration as valid
6+
if (r == n) return 1;
7+
8+
int ans = 0;
9+
10+
// Try placing a queen in each column of the current row
11+
for (int c = 0; c < n; c++) {
12+
bool poss = true;
13+
14+
// Check against previously placed queens to see if (r, c) is a valid position
15+
for (auto& p : queens) {
16+
int xr = p.first;
17+
int xc = p.second;
18+
19+
// A queen can attack if in the same row, column, or diagonal
20+
if (xr == r || xc == c || abs(xr - r) == abs(xc - c)) {
21+
poss = false;
22+
break;
23+
}
24+
}
25+
26+
// If (r, c) is not a valid position, skip to the next column
27+
if (!poss) continue;
28+
29+
// Place the queen at (r, c) and mark it in the queens vector
30+
queens.push_back({r, c});
31+
32+
// Recurse to the next row to continue placing queens
33+
ans += rec(r + 1, n, queens);
34+
35+
// Backtrack: remove the queen from (r, c)
36+
queens.pop_back();
37+
}
38+
39+
return ans;
40+
}
41+
42+
// Main function to initiate the N-Queens solution count
43+
int totalNQueens(int n) {
44+
vector<pair<int, int>> queens;
45+
return rec(0, n, queens);
46+
}
47+
};

Cpp/0136-Single-Number/Problem.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [136. Single Number](https://leetcode.com/problems/single-number/)
2+
3+
<div class="elfjS" data-track-load="description_content"><p>Given a <strong>non-empty</strong>&nbsp;array of integers <code>nums</code>, every element appears <em>twice</em> except for one. Find that single one.</p>
4+
5+
<p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra space.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<pre><strong>Input:</strong> nums = [2,2,1]
10+
<strong>Output:</strong> 1
11+
</pre><p><strong class="example">Example 2:</strong></p>
12+
<pre><strong>Input:</strong> nums = [4,1,2,1,2]
13+
<strong>Output:</strong> 4
14+
</pre><p><strong class="example">Example 3:</strong></p>
15+
<pre><strong>Input:</strong> nums = [1]
16+
<strong>Output:</strong> 1
17+
</pre>
18+
<p>&nbsp;</p>
19+
<p><strong>Constraints:</strong></p>
20+
21+
<ul>
22+
<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
23+
<li><code>-3 * 10<sup>4</sup> &lt;= nums[i] &lt;= 3 * 10<sup>4</sup></code></li>
24+
<li>Each element in the array appears twice except for one element which appears only once.</li>
25+
</ul>
26+
</div>

Cpp/0136-Single-Number/Solution.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution
2+
{
3+
public:
4+
int singleNumber(vector<int> &nums)
5+
{
6+
// Initialize a variable `xorr` to 0, which will be used to store the XOR result.
7+
int xorr = 0;
8+
9+
// Iterate through each element in the array `nums`.
10+
for (int i = 0; i < nums.size(); i++)
11+
{
12+
// Apply XOR operation between `xorr` and the current element `nums[i]`.
13+
// This will cancel out all elements that appear twice and leave only the single element.
14+
xorr = xorr ^ nums[i];
15+
}
16+
17+
// Return the remaining element, which is the single element that does not have a duplicate.
18+
return xorr;
19+
}
20+
};

Cpp/0213-house-robber-II/problem.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<h1><a href = "https://leetcode.com/problems/house-robber-ii/description/">213. House Robber II</h1>
2+
3+
## Problem Statement
4+
5+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses are broken into on the same night.
6+
7+
Given an integer array `nums` representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<pre>
12+
<strong>Input: </strong> nums = [2,3,2]
13+
<strong>Output:</strong> 3
14+
<strong>Explanation:</strong> You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
15+
because they are adjacent houses.
16+
</pre>
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
<pre>
20+
<strong>Input:</strong> nums = [1,2,3,1]
21+
<strong>Output:</strong> 4
22+
<strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3).
23+
Total amount you can rob = 1 + 3 = 4.
24+
</pre>
25+
26+
<p><strong class="example">Example 3:</strong></p>
27+
<pre>
28+
<strong>Input:</strong> nums = [1,2,3]
29+
<strong>Output:</strong> 3
30+
</pre>
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
- `1 <= nums.length <= 100`
35+
- `0 <= nums[i] <= 1000`

Cpp/0213-house-robber-II/solution.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int rob(vector<int>& nums) {
4+
int n = nums.size();
5+
6+
// Handle edge cases
7+
if (n == 0) return 0; // No houses
8+
if (n == 1) return nums[0]; // Only one house
9+
10+
// Helper function to calculate max loot for a linear array
11+
auto robLinear = [](const vector<int>& nums, int start, int end) {
12+
int prev1 = 0, prev2 = 0; // Initialize previous two max values
13+
for (int i = start; i < end; ++i) {
14+
int pick = nums[i] + prev1; // Value if we pick current house
15+
int notPick = prev2; // Value if we do not pick current house
16+
int curr = max(pick, notPick); // Max of both options
17+
prev1 = prev2; // Update previous for the next iteration
18+
prev2 = curr;
19+
}
20+
return prev2; // Return the maximum value obtained
21+
};
22+
23+
// Rob houses excluding the last house and then excluding the first house
24+
int maxLoot1 = robLinear(nums, 0, n - 1); // From house 0 to house n-2
25+
int maxLoot2 = robLinear(nums, 1, n); // From house 1 to house n-1
26+
27+
// Return the maximum of both scenarios
28+
return max(maxLoot1, maxLoot2);
29+
}
30+
};

0 commit comments

Comments
 (0)