Skip to content

Commit 41c9a24

Browse files
authored
Merge pull request #928 from agarwalhimanshugaya/dsa-ques
added some solution between 3000-3010
2 parents 26c6d2f + bcd17f2 commit 41c9a24

6 files changed

+992
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
id: 3000-maximum-area-of-diagonal-rectangle
3+
title: Maximum area of longest diagonal rectangle (Leetcode)
4+
sidebar_label: 3000-Maximum area of longest diagonal rectangle
5+
description: Solution of Maximum area of longest diagonal rectangle
6+
---
7+
8+
9+
## Problem Description
10+
11+
You are given a 2D 0-indexed integer array dimensions.
12+
13+
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.
14+
15+
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.
16+
17+
### Examples
18+
19+
#### Example 1
20+
21+
- **Input:** $dimensions = [[9,3],[8,6]]$
22+
- **Output:** $48$
23+
- **Explanation:** For index = $0$, $\text{length} = 9$ and width = $3$. $\text{Diagonal length} = sqrt(9 \times 9 + 3 \times 3) = sqrt(90) ≈ 9.487$.
24+
For $\text{index} = 1$, $length = 8$ and $\text{width} = 6$. $\text{Diagonal length} = sqrt(8 \times 8 + 6 \times 6) = sqrt(100) = 10$.
25+
So, the rectangle at index 1 has a greater diagonal length therefore we return $area = 8 \times 6 = 48..$
26+
27+
#### Example 2
28+
29+
- **Input:** $dimensions = [[3,4],[4,3]]$
30+
- **Output:** $12$
31+
- **Explanation:**$ Length of diagonal is the same for both which is 5, so maximum area = 12$.
32+
33+
### Constraints
34+
35+
- $1 \leq dimensions.length \leq 100$
36+
- $dimensions[i].length == 2$
37+
- $1 <= dimensions[i][0], dimensions[i][1] <= 100$
38+
39+
### Intuition
40+
41+
The problem requires finding the rectangle with the maximum diagonal length and, in case of a tie, returning the one with the maximum area.
42+
To achieve this, we need to calculate the diagonal length for each rectangle and compare it with the current maximum diagonal length.
43+
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.
44+
45+
### Approach
46+
47+
The code uses a loop to iterate through each rectangle in the given dimensions vector.
48+
It calculates the square of the diagonal length for each rectangle and compares it with the previous maximum diagonal length (diag).
49+
If the current rectangle has a longer or equal diagonal length, it updates the maximum area (area) accordingly.
50+
The area variable keeps track of the maximum area among rectangles with the longest diagonal.
51+
Finally, the function returns the maximum area among rectangles with the longest diagonal.
52+
53+
### Solution Code
54+
55+
#### Python
56+
57+
```py
58+
class Solution:
59+
def areaOfMaxDiagonal(self, dimensions: List[List[int]]) -> int:
60+
a,b = max(dimensions, key = lambda x: (x[0]*x[0]+x[1]*x[1], (x[0]*x[1])))
61+
return a*b
62+
```
63+
64+
#### Java
65+
66+
```java
67+
68+
public int areaOfMaxDiagonal(int[][] dimensions) {
69+
int diagSq = 0, area = 0;
70+
for (int[] d : dimensions) {
71+
int ds = d[0] * d[0] + d[1] * d[1];
72+
int ar = d[0] * d[1];
73+
if (ds > diagSq) {
74+
area = ar;
75+
diagSq = ds;
76+
}
77+
else if (ds == diagSq && ar > area) area = ar;
78+
}
79+
return area;
80+
}
81+
```
82+
83+
#### C++
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int areaOfMaxDiagonal(vector<vector<int>>& dimensions) {
89+
90+
double max_diagonal = 0;
91+
int max_area = 0;
92+
93+
for (const auto& rectangle : dimensions) {
94+
int length = rectangle[0];
95+
int width = rectangle[1];
96+
97+
double diagonal_length = sqrt(pow(length, 2) + pow(width, 2));
98+
99+
if (diagonal_length > max_diagonal || (diagonal_length == max_diagonal && length * width > max_area)) {
100+
max_diagonal = diagonal_length;
101+
max_area = length * width;
102+
}
103+
}
104+
105+
return max_area;
106+
107+
}
108+
};
109+
```
110+
111+
### Conclusion
112+
113+
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
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
id: maximum-size-of-a-set-after-removals
3+
title: Maximum Size of a Set After Removals
4+
sidebar_label: 3002 -Maximum Size of a Set After Removals
5+
tags:
6+
- Array
7+
- Hash Table
8+
- Greedy
9+
description: "This is a solution to the Maximum Size of a Set After Removals problem on LeetCode."
10+
---
11+
12+
## Problem Description
13+
14+
You are given two 0-indexed integer arrays nums1 and nums2 of even length n.
15+
16+
You must remove n / 2 elements from nums1 and n / 2 elements from nums2. After the removals, you insert the remaining elements of nums1 and nums2 into a set s.
17+
18+
Return the maximum possible size of the set s.
19+
20+
### Examples
21+
22+
**Example 1:**
23+
24+
```
25+
Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
26+
Output: 2
27+
Explanation: We remove two occurences of 1 from nums1 and nums2. After the removals, the arrays become equal to nums1 = [2,2] and nums2 = [1,1]. Therefore, s = {1,2}.
28+
It can be shown that 2 is the maximum possible size of the set s after the removals.
29+
30+
```
31+
32+
**Example 2:**
33+
34+
```
35+
Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
36+
Output: 5
37+
Explanation: We remove 2, 3, and 6 from nums1, as well as 2 and two occurrences of 3 from nums2. After the removals, the arrays become equal to nums1 = [1,4,5] and nums2 = [2,3,2]. Therefore, s = {1,2,3,4,5}.
38+
It can be shown that 5 is the maximum possible size of the set s after the removals.
39+
40+
```
41+
42+
**Example 3:**
43+
44+
```
45+
Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
46+
Output: 6
47+
Explanation: We remove 1, 2, and 3 from nums1, as well as 4, 5, and 6 from nums2. After the removals, the arrays become equal to nums1 = [1,2,3] and nums2 = [4,5,6]. Therefore, s = {1,2,3,4,5,6}.
48+
It can be shown that 6 is the maximum possible size of the set s after the removals.
49+
```
50+
51+
### Constraints
52+
53+
- `n == nums1.length == nums2.length`
54+
- `1 <= n <= 2 * 104`
55+
- `n is even`
56+
- `1 <= nums1[i], nums2[i] <= 109`
57+
58+
## Solution for Minimum Moves to Capture The Queen Problem
59+
60+
### Intuition
61+
62+
To solve this problem, we can use a greedy approach. We aim to maximize the size of the set s after removing elements from nums1 and nums2. We can achieve this by removing elements that appear the most in both arrays. Additionally, we may need to remove elements from one array to balance the removals between the two arrays.
63+
64+
### Approach
65+
66+
Initialize variables length, n1, and n2. length represents half the length of the input arrays, while n1 and n2 are sets of unique elements from nums1 and nums2, respectively.
67+
68+
Calculate the number of elements common to both n1 and n2 and store it in inter_num.
69+
70+
Calculate the difference between the lengths of n1 and n2 and length, representing the excess elements in each array.
71+
72+
Determine the maximum possible difference between the lengths of n1 and n2 and length, or the number of elements common to both arrays. Store this maximum difference in max_diff.
73+
74+
Return the sum of the lengths of n1 and n2 minus max_diff, which represents the maximum possible size of the set s.
75+
76+
### Solution Code
77+
78+
#### Python
79+
80+
```py
81+
class Solution:
82+
def maximumSetSize(self, nums1: list[int], nums2: list[int]) -> int:
83+
length, n1, n2 = len(nums1) // 2, set(nums1), set(nums2)
84+
inter_num = len(n1.intersection(n2))
85+
diff = 0
86+
87+
diff += len(n1) - length if len(n1) >= length else 0
88+
diff += len(n2) - length if len(n2) >= length else 0
89+
90+
max_diff=max(diff,inter_num)
91+
92+
return (len(n1)+len(n2))-max_diff
93+
94+
```
95+
96+
#### Java
97+
98+
```java
99+
class Solution {
100+
public int maximumSetSize(int[] nums1, int[] nums2) {
101+
int i,j,n=nums1.length;
102+
Set<Integer> set1=new HashSet<>();
103+
Set<Integer> set2=new HashSet<>();
104+
Set<Integer> set3=new HashSet<>();
105+
for(int x:nums1)
106+
{
107+
set1.add(x);
108+
set3.add(x);
109+
}
110+
for(int x:nums2)
111+
{
112+
set2.add(x);
113+
set3.add(x);
114+
}
115+
int common=set1.size()+set2.size()-set3.size();
116+
int n1=set1.size(),n2=set2.size();
117+
int ans=Math.min(n/2,n1-common);
118+
ans+=Math.min(n/2,n2-common);
119+
ans+=common;
120+
ans=Math.min(n,ans);
121+
return ans;
122+
}
123+
}
124+
125+
```
126+
127+
#### C++
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
133+
set<int> s1, s2, s;
134+
int n = nums1.size();
135+
for (int x : nums1) {
136+
s1.insert(x);
137+
s.insert(x);
138+
}
139+
for (int x : nums2) {
140+
s2.insert(x);
141+
s.insert(x);
142+
}
143+
int ans = min(min(n/2, (int)s1.size()) + min(n/2, (int)s2.size()),(int) s.size());
144+
145+
return ans;
146+
}
147+
};
148+
```
149+
150+
#### Complexity Analysis
151+
152+
- Time complexity: $O(n)$, where n is the total number of elements in both nums1 and nums2. Calculating the set intersection and differences takes linear time.
153+
- Space complexity: $O(n)$ , where n is the total number of elements in both nums1 and nums2. The space complexity is dominated by the sets n1 and n2, which store unique elements from the input arrays.

0 commit comments

Comments
 (0)