-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Docs]: Want to Add Folder which will Contain Different questions rel…
…ated to topic And their Solution #351 (#374) * added backend of login/signup * Added solution to 3 problems and categorised them * renamed * nodemodules removed
- Loading branch information
1 parent
309a88d
commit 64e6889
Showing
3 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--- | ||
id: battleships-in-a-board | ||
title: Battleships in a Board | ||
sidebar_label: 0419-Battleships-in-a-Board | ||
tags: | ||
- Array | ||
- Depth-First Search | ||
- Matrix | ||
description: "Given an `m x n` board where each cell is a battleship 'X' or empty '.', count the number of battleships on the board." | ||
--- | ||
|
||
## Problem | ||
|
||
Given an `m x n` board where each cell is a battleship `'X'` or empty `'.'`, return the number of battleships on the board. | ||
|
||
Battleships can only be placed horizontally or vertically on the board. In other words, they can only occupy a contiguous line of cells horizontally or vertically. Each battleship must be separated by at least one empty cell (either horizontally or vertically) from any other battleship. | ||
|
||
### Examples | ||
|
||
**Example 1:** | ||
|
||
**Input:** `board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]` | ||
**Output:** `2` | ||
|
||
**Example 2:** | ||
|
||
**Input:** `board = [["."]]` | ||
**Output:** `0` | ||
|
||
### Constraints | ||
|
||
- `m == board.length` | ||
- `n == board[i].length` | ||
- `1 <= m, n <= 200` | ||
- `board[i][j]` is either `'X'` or `'.'`. | ||
|
||
### Follow-up | ||
|
||
Could you do it in one-pass, using only `O(1)` extra memory and without modifying the value of the board? | ||
|
||
--- | ||
|
||
## Approach | ||
|
||
To count the number of battleships in one pass and without extra memory, we can traverse the board and count only the top-left cell of each battleship. A cell qualifies as the top-left cell if there are no battleship cells above it or to the left of it. | ||
|
||
### Steps: | ||
|
||
1. Initialize a counter to keep track of the number of battleships. | ||
2. Traverse each cell in the board. | ||
3. For each cell, check if it is a battleship (`'X'`): | ||
- If it is, check if there is no battleship cell above it and no battleship cell to the left of it. | ||
- If both conditions are met, increment the battleship counter. | ||
4. Return the counter after the traversal. | ||
|
||
### C++ Solution | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int countBattleships(vector<vector<char>>& board) { | ||
int count = 0; | ||
for (int i = 0; i < board.size(); i++) { | ||
for (int j = 0; j < board[0].size(); j++) { | ||
if (board[i][j] == 'X') { | ||
if (i > 0 && board[i - 1][j] == 'X') continue; | ||
if (j > 0 && board[i][j - 1] == 'X') continue; | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
}; | ||
``` | ||
### Complexity Analysis | ||
**Time Complexity:** O(m * n) | ||
>Reason: We traverse each cell of the board once. | ||
**Space Complexity:** O(1) | ||
>Reason: We do not use any extra space that scales with the input size. | ||
### References | ||
**LeetCode Problem:** Battleships in a Board | ||
**Solution Link:** Battleships in a Board Solution on LeetCode |
112 changes: 112 additions & 0 deletions
112
docs/Practice_Question/String/Longest-Uncommon-Subsequence-II.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
id: longest-uncommon-subsequence-ii | ||
title: Longest Uncommon Subsequence II | ||
sidebar_label: 0522-Longest-Uncommon-Subsequence-II | ||
tags: | ||
- String | ||
- Sorting | ||
description: "Given an array of strings strs, return the length of the longest uncommon subsequence between them. If the longest uncommon subsequence does not exist, return -1." | ||
--- | ||
|
||
## Problem | ||
|
||
Given an array of strings `strs`, return the length of the longest uncommon subsequence between them. An uncommon subsequence is a subsequence that is not common to any other string in the array. | ||
|
||
A **subsequence** of a string `s` is a string that can be obtained after deleting any number of characters from `s`. | ||
|
||
### Examples | ||
|
||
**Example 1:** | ||
|
||
**Input:** `strs = ["aba","cdc","eae"]` | ||
**Output:** `3` | ||
|
||
**Example 2:** | ||
|
||
**Input:** `strs = ["aaa","aaa","aa"]` | ||
**Output:** `-1` | ||
|
||
### Constraints | ||
|
||
- `2 <= strs.length <= 50` | ||
- `1 <= strs[i].length <= 10` | ||
- `strs[i]` consists of lowercase English letters. | ||
|
||
--- | ||
|
||
## Approach | ||
|
||
To find the longest uncommon subsequence, we need to consider the following: | ||
|
||
1. If a string is unique in the list (i.e., it does not appear more than once), we need to check if it is not a subsequence of any other string. | ||
2. If a string is not unique (i.e., it appears more than once), it cannot be an uncommon subsequence. | ||
3. We can start by checking the strings in descending order of their lengths. This way, we can return the first string that meets the criteria as soon as we find it. | ||
|
||
### Steps: | ||
|
||
1. Sort the strings by length in descending order. | ||
2. Check each string: | ||
- If the string appears only once in the list. | ||
- If it is not a subsequence of any other string longer than it. | ||
3. If such a string is found, return its length. | ||
4. If no such string is found, return `-1`. | ||
|
||
### Helper Function | ||
|
||
A helper function `is_subsequence` can be used to determine if one string is a subsequence of another. | ||
|
||
### C++ Solution | ||
```cpp | ||
#include <vector> | ||
#include <string> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int findLUSlength(vector<string>& strs) { | ||
sort(strs.begin(), strs.end(), [](const string &a, const string &b) { | ||
return b.size() < a.size(); | ||
}); | ||
|
||
for (int i = 0; i < strs.size(); i++) { | ||
bool isSubsequence = false; | ||
for (int j = 0; j < strs.size(); j++) { | ||
if (i != j && isSubsequence(strs[i], strs[j])) { | ||
isSubsequence = true; | ||
break; | ||
} | ||
} | ||
if (!isSubsequence) { | ||
return strs[i].size(); | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
private: | ||
bool isSubsequence(const string &a, const string &b) { | ||
int i = 0, j = 0; | ||
while (i < a.size() && j < b.size()) { | ||
if (a[i] == b[j]) { | ||
i++; | ||
} | ||
j++; | ||
} | ||
return i == a.size(); | ||
} | ||
}; | ||
``` | ||
### Complexity Analysis | ||
**Time Complexity:** O(n^2 * l) | ||
>Reason: Sorting the list takes O(n log n). Checking if a string is a subsequence of another string takes O(l) time, and this is done for each pair of strings, leading to O(n^2 * l) in total. | ||
**Space Complexity:** O(1) | ||
>Reason: The space complexity is constant as we are not using any extra space proportional to the input size, other than the space required for sorting. | ||
### References | ||
**LeetCode Problem:** Longest Uncommon Subsequence II | ||
|
||
**Solution Link:** LeetCode Solution | ||
|
||
**Wikipedia:** Subsequence |
89 changes: 89 additions & 0 deletions
89
docs/Practice_Question/Tree/Convert-BST-to-Greater-Tree.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
--- | ||
id: convert-bst-to-greater-tree | ||
title: Convert BST to Greater Tree | ||
sidebar_label: 0538-Convert-BST-to-Greater-Tree | ||
tags: | ||
- Tree | ||
- Depth-First Search | ||
- Binary Search Tree | ||
- Binary Tree | ||
description: "Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST." | ||
--- | ||
|
||
## Problem | ||
|
||
Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST. | ||
|
||
### Examples | ||
|
||
**Example 1:** | ||
|
||
**Input:** `root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]` | ||
**Output:** `[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]` | ||
|
||
**Example 2:** | ||
|
||
**Input:** `root = [0,null,1]` | ||
**Output:** `[1,null,1]` | ||
|
||
### Constraints | ||
|
||
- The number of nodes in the tree is in the range `[0, 10^4]`. | ||
- `-10^4 <= Node.val <= 10^4` | ||
- All the values in the tree are unique. | ||
- `root` is guaranteed to be a valid binary search tree. | ||
|
||
--- | ||
|
||
## Approach | ||
|
||
To convert a BST to a Greater Tree, we need to accumulate the sum of all nodes that are greater than the current node. This can be efficiently done using a reverse in-order traversal (right-root-left), which processes nodes from the largest to the smallest. | ||
|
||
### Steps: | ||
|
||
1. Initialize a variable to keep track of the running sum. | ||
2. Perform a reverse in-order traversal of the tree. | ||
3. During the traversal, update the value of each node to include the running sum. | ||
4. Update the running sum with the value of the current node. | ||
|
||
### C++ Solution | ||
|
||
```cpp | ||
#include <iostream> | ||
using namespace std; | ||
|
||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
}; | ||
|
||
class Solution { | ||
private: | ||
int sum = 0; | ||
public: | ||
TreeNode* convertBST(TreeNode* root) { | ||
if (root != nullptr) { | ||
convertBST(root->right); | ||
sum += root->val; | ||
root->val = sum; | ||
convertBST(root->left); | ||
} | ||
return root; | ||
} | ||
}; | ||
``` | ||
### Complexity Analysis | ||
**Time Complexity:** O(n) | ||
>Reason: Each node is visited once during the traversal. | ||
**Space Complexity:** O(h) | ||
>Reason: The space complexity is determined by the recursion stack, which in the worst case (unbalanced tree) is O(n), but on average (balanced tree) is O(log n). | ||
### References | ||
**LeetCode Problem:** Convert BST to Greater Tree | ||
**Solution Link:** LeetCode Solution | ||
**Wikipedia:** Binary Search Tree |