-
-
Notifications
You must be signed in to change notification settings - Fork 155
added some solution between 3000-3010 #928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ajay-dhangar
merged 17 commits into
codeharborhub:main
from
agarwalhimanshugaya:dsa-ques
Jun 11, 2024
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d2516da
added some solution between 3000-3010
agarwalhimanshugaya 4781711
change on suggestion
agarwalhimanshugaya 7b61c8a
change all the error
agarwalhimanshugaya b5e991a
rectify all the error
agarwalhimanshugaya c568b6b
Update 3000-Maximum area of diagonal rectangle.md
ajay-dhangar 7f456b6
Update 3001-Minimum moves to capture queen
ajay-dhangar 74b1f47
Update 3002-Maximum size of a set after removals.md
ajay-dhangar c178475
rectify all the error
agarwalhimanshugaya 08cbc6f
rectify all the error
agarwalhimanshugaya 8024634
Merge remote-tracking branch 'origin/dsa-ques' into dsa-ques
agarwalhimanshugaya 174e8b7
rectify all the error
agarwalhimanshugaya 996fd78
Rename 3000-Maximum area of diagonal rectangle.md to 3000-maximum-are…
ajay-dhangar 457c31b
Update and rename 3001-Minimum moves to capture queen.md to minimum-m…
ajay-dhangar c906f6a
Update and rename 3002-Maximum size of a set after removals.md to 300…
ajay-dhangar 5464741
Update and rename 3005-count elements with maximum frequency.md to 30…
ajay-dhangar 28553a8
Update and rename 3006-find beautiful indices in the given array.md t…
ajay-dhangar bcd17f2
Update and rename 3010-divide an array into subarrays with minimum co…
ajay-dhangar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
dsa-solutions/lc-solutions/3000-3099/3000-Maximum area of diagonal rectangle.md
This file contains hidden or 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,118 @@ | ||
--- | ||
id: 3000 | ||
title: Maximum area of longest diagonal rectangle (Leetcode) | ||
sidebar_label: 3000-Maximum area of longest diagonal rectangle | ||
description: Solution of Maximum area of longest diagonal rectangle | ||
--- | ||
|
||
## Problem Description | ||
|
||
| Problem Statement | Solution Link | LeetCode Profile | | ||
| :---------------------------------------------------------------------------------------------------------------------------------- | :------------ | :--------------- | | ||
| [Maximum area of longest diagonal rectangle](https://leetcode.com/problems/maximum-area-of-longest-diagonal-rectangle/description/) | | ||
|
||
## Problem Description | ||
|
||
You are given a 2D 0-indexed integer array dimensions. | ||
|
||
For all indices i, 0 <= i < dimensions.length, dimensions[i][0] represents the length and dimensions[i][1] represents the width of the rectangle i. | ||
|
||
Return the area of the rectangle having the longest diagonal. If there are multiple rectangles with the longest diagonal, return the area of the rectangle having the maximum area. | ||
|
||
### Examples | ||
|
||
#### Example 1 | ||
|
||
- **Input:** $dimensions = [[9,3],[8,6]]$ | ||
- **Output:** $48$ | ||
- **Explanation:** For index = $0$, $\text{length} = 9$ and width = $3$. $\text{Diagonal length} = sqrt(9 \times 9 + 3 \times 3) = sqrt(90) ≈ 9.487$. | ||
For $\text{index} = 1$, $length = 8$ and $\text{width} = 6$. $\text{Diagonal length} = sqrt(8 \times 8 + 6 \times 6) = sqrt(100) = 10$. | ||
So, the rectangle at index 1 has a greater diagonal length therefore we return $area = 8 \times 6 = 48..$ | ||
|
||
#### Example 2 | ||
|
||
- **Input:** $dimensions = [[3,4],[4,3]]$ | ||
- **Output:** $12$ | ||
- **Explanation:**$ Length of diagonal is the same for both which is 5, so maximum area = 12$. | ||
|
||
### Constraints | ||
|
||
- $1 \leq dimensions.length \leq 100$ | ||
- $dimensions[i].length == 2$ | ||
- $1 <= dimensions[i][0], dimensions[i][1] <= 100$ | ||
|
||
### Intuition | ||
|
||
The problem requires finding the rectangle with the maximum diagonal length and, in case of a tie, returning the one with the maximum area. | ||
To achieve this, we need to calculate the diagonal length for each rectangle and compare it with the current maximum diagonal length. | ||
If the diagonal length is greater or equal (in case of a tie), we update the maximum diagonal length and check if the area of the current rectangle is greater than the maximum area. If so, we update the maximum area as well. | ||
|
||
### Approach | ||
|
||
The code uses a loop to iterate through each rectangle in the given dimensions vector. | ||
It calculates the square of the diagonal length for each rectangle and compares it with the previous maximum diagonal length (diag). | ||
If the current rectangle has a longer or equal diagonal length, it updates the maximum area (area) accordingly. | ||
The area variable keeps track of the maximum area among rectangles with the longest diagonal. | ||
Finally, the function returns the maximum area among rectangles with the longest diagonal. | ||
|
||
### Solution Code | ||
|
||
#### Python | ||
|
||
```py | ||
class Solution: | ||
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int: | ||
a,b = max(dimensions, key = lambda x: (x[0]*x[0]+x[1]*x[1], (x[0]*x[1]))) | ||
return a*b | ||
``` | ||
|
||
#### Java | ||
|
||
```java | ||
|
||
public int areaOfMaxDiagonal(int[][] dimensions) { | ||
int diagSq = 0, area = 0; | ||
for (int[] d : dimensions) { | ||
int ds = d[0] * d[0] + d[1] * d[1]; | ||
int ar = d[0] * d[1]; | ||
if (ds > diagSq) { | ||
area = ar; | ||
diagSq = ds; | ||
} | ||
else if (ds == diagSq && ar > area) area = ar; | ||
} | ||
return area; | ||
} | ||
``` | ||
|
||
#### C++ | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int areaOfMaxDiagonal(vector<vector<int>>& dimensions) { | ||
|
||
double max_diagonal = 0; | ||
int max_area = 0; | ||
|
||
for (const auto& rectangle : dimensions) { | ||
int length = rectangle[0]; | ||
int width = rectangle[1]; | ||
|
||
double diagonal_length = sqrt(pow(length, 2) + pow(width, 2)); | ||
|
||
if (diagonal_length > max_diagonal || (diagonal_length == max_diagonal && length * width > max_area)) { | ||
max_diagonal = diagonal_length; | ||
max_area = length * width; | ||
} | ||
} | ||
|
||
return max_area; | ||
|
||
} | ||
}; | ||
``` | ||
|
||
### Conclusion | ||
|
||
At last we can say that we can calculate maximum area of diagonal rectangle using timecomplexity of $o(n)$ and spacecomplexity of $o(1)$ using a simple for loop and some variable |
249 changes: 249 additions & 0 deletions
249
dsa-solutions/lc-solutions/3000-3099/3001-Minimum moves to capture queen
This file contains hidden or 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,249 @@ | ||
--- | ||
id: Minimum Moves to Capture The Queen | ||
title: Minimum Moves to Capture The Queen | ||
sidebar_label: 3001 - Minimum Moves to Capture The Queen | ||
tags: | ||
- Array | ||
description: "This is a solution to the Minimum Moves to Capture The Queen problem on LeetCode." | ||
--- | ||
|
||
## Problem Description | ||
|
||
There is a 1-indexed 8 x 8 chessboard containing 3 pieces. | ||
|
||
You are given 6 integers a, b, c, d, e, and f where: | ||
|
||
(a, b) denotes the position of the white rook. | ||
(c, d) denotes the position of the white bishop. | ||
(e, f) denotes the position of the black queen. | ||
Given that you can only move the white pieces, return the minimum number of moves required to capture the black queen. | ||
|
||
Note that: | ||
|
||
Rooks can move any number of squares either vertically or horizontally, but cannot jump over other pieces. | ||
Bishops can move any number of squares diagonally, but cannot jump over other pieces. | ||
A rook or a bishop can capture the queen if it is located in a square that they can move to. | ||
The queen does not move. | ||
|
||
### Examples | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: a = 1, b = 1, c = 8, d = 8, e = 2, f = 3 | ||
Output: 2 | ||
Explanation: We can capture the black queen in two moves by moving the white rook to (1, 3) then to (2, 3). | ||
It is impossible to capture the black queen in less than two moves since it is not being attacked by any of the pieces at the beginning. | ||
|
||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: a = 5, b = 3, c = 3, d = 4, e = 5, f = 2 | ||
Output: 1 | ||
Explanation: We can capture the black queen in a single move by doing one of the following: | ||
- Move the white rook to (5, 2). | ||
- Move the white bishop to (5, 2). | ||
|
||
``` | ||
|
||
### Constraints | ||
|
||
- `1 <= a, b, c, d, e, f <= 8` | ||
- `No two pieces are on the same square.` | ||
|
||
## Solution for Minimum Moves to Capture The Queen Problem | ||
|
||
### Intuition | ||
|
||
Same Row or Column: | ||
If the queen is in the same row (a == e) or the same column (b == f), it means it can be captured in one move. | ||
|
||
Same Diagonal: | ||
If the absolute difference between the columns (abs(c - e)) is equal to the absolute difference between the rows (abs(d - f)), it means the queen is on the same diagonal and can be captured in one move. | ||
|
||
Otherwise: | ||
If none of the above conditions are met, it implies that the queen cannot be captured in one move. | ||
|
||
Pieces in Between | ||
For suppose a rook is standing in the way of bishop and vice-versa the min no of moves is two , to check whether the piece in between i have used the manhattan distance. | ||
|
||
### Approach | ||
|
||
Check for Same Row, Column, or Diagonal: | ||
If the queen is in the same row, column, or diagonal as the starting point, calculate the Manhattan distance. | ||
|
||
Check for Obstacles: | ||
While calculating the Manhattan distance, inspect the squares between the starting point and the queen's position. | ||
If any square along the path contains another piece, consider it an obstacle. | ||
|
||
Minimum Moves Calculation: | ||
Assess the minimum number of moves based on the presence of obstacles. | ||
If no obstacles are present, it's likely a direct capture in one move. | ||
If obstacles are encountered, it might require at least two moves to maneuver around them. | ||
|
||
Handle Piece-Specific Scenarios: | ||
Adjust the approach based on the specific pieces involved. | ||
For a rook, obstacles in the same row or column are significant. | ||
For a bishop, obstacles along diagonals need consideration. | ||
|
||
Return Minimum Moves: | ||
Return the calculated minimum number of moves. | ||
|
||
### Solution Code | ||
|
||
#### Python | ||
|
||
```py | ||
class Solution: | ||
def minMovesToCaptureTheQueen(self, a: int, b: int, c: int, d: int, e: int, f: int) -> int: | ||
# rock and queen in the same row or col | ||
if a == e: # same row | ||
if a == c and (d - b) * (d - f) < 0: # bishop on the same row and between rock and queen | ||
return 2 | ||
else: | ||
return 1 | ||
elif b == f: # same col | ||
if b == d and (c - a) * (c - e) < 0: | ||
return 2 | ||
else: | ||
return 1 | ||
# bishop and queen in the same diagonal | ||
elif c - e == d - f: # \ diagonal | ||
if a - e == b - f and (a - c) * (a - e) < 0: | ||
return 2 | ||
else: | ||
return 1 | ||
elif c - e == f - d: # / diagonal | ||
if a - e == f - b and (a - c) * (a - e) < 0: | ||
return 2 | ||
else: | ||
return 1 | ||
return 2 | ||
|
||
|
||
|
||
``` | ||
|
||
#### Java | ||
|
||
```java | ||
=public int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { | ||
// ----- Rook ----- | ||
if(a==e){ | ||
if(a==c && ((b<d && d<f) || (f<d && d<b))) { | ||
return 2; | ||
}else return 1; | ||
|
||
}else if(b==f) { | ||
if(b==d && ((a<c && c<e) || (e<c && c<a))){ | ||
return 2; | ||
}else return 1; | ||
} | ||
|
||
|
||
// ----- Bishop ----- | ||
// flag -> true means rook is between bishop and queen - so we need to move rook | ||
boolean flag = false; | ||
for(int i=c, j=d; i>0&&j>0; i--, j--){ | ||
if(i==a && j==b) flag = true; | ||
else if(i==e && j==f) { | ||
if(flag) return 2; | ||
else return 1; | ||
} | ||
} | ||
|
||
flag =false; | ||
for(int i=c, j=d; i>0&&j<9; i--, j++){ | ||
if(i==a && j==b) flag = true; | ||
else if(i==e && j==f) { | ||
if(flag) return 2; | ||
else return 1; | ||
} | ||
} | ||
|
||
flag = false; | ||
for(int i=c, j=d; i<9&&j<9; i++, j++){ | ||
if(i==a && j==b) flag = true; | ||
else if(i==e && j==f) { | ||
if(flag) return 2; | ||
else return 1; | ||
} | ||
} | ||
|
||
flag = false; | ||
for(int i=c, j=d; i<9&&j>0; i++, j--){ | ||
if(i==a && j==b) flag = true; | ||
else if(i==e && j==f) { | ||
if(flag) return 2; | ||
else return 1; | ||
} | ||
} | ||
|
||
// for every condition Rook can capture the queen in 2 steps | ||
return 2; | ||
} | ||
|
||
``` | ||
|
||
#### C++ | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int minMovesToCaptureTheQueen(int a, int b, int c, int d, int e, int f) { | ||
if(a == e){ | ||
if(c == e){ | ||
int d0 = abs(b-f) ; | ||
int d1 = abs(b-d) ; | ||
int d2 = abs(d-f); | ||
if(d0 == d1+d2) | ||
return 2; | ||
else | ||
return 1; | ||
} | ||
else | ||
return 1; | ||
} | ||
else if(b == f){ | ||
if(d == f){ | ||
int d0 = abs(a-e) ; | ||
int d1 = abs(a-c); | ||
int d2 = abs(c-e) ; | ||
if(d0 == d1+d2) | ||
return 2; | ||
else | ||
return 1; | ||
} | ||
else | ||
return 1; | ||
|
||
} | ||
else if(abs(c-e) == abs(d - f)){ | ||
if(abs(a-c) == abs(b-d)) { | ||
int d0 = abs(c-e) + abs(d-f); | ||
int d1 = abs(a-c) + abs(b-d); | ||
int d2 = abs(e-a) + abs(f-b); | ||
if(d0 == d1+d2) | ||
return 2; | ||
else | ||
return 1; | ||
} | ||
else | ||
return 1; | ||
} | ||
else | ||
return 2; | ||
|
||
} | ||
}; | ||
``` | ||
|
||
### Conclusion | ||
|
||
THis code efficectively calculates the minimum number of moves using constant space and constant time | ||
|
||
- **LeetCode Problem**: [Minimum Moves to Capture The Queen](https://leetcode.com/problems/minimum-moves-to-capture-the-queen/description/) | ||
|
||
- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/minimum-moves-to-capture-the-queen/solutions/4577719/beats-100-time-complexity-o-1) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.