Skip to content

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
merged 17 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
id: 3000-maximum-area-of-diagonal-rectangle
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

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
id: maximum-size-of-a-set-after-removals
title: Maximum Size of a Set After Removals
sidebar_label: 3002 -Maximum Size of a Set After Removals
tags:
- Array
- Hash Table
- Greedy
description: "This is a solution to the Maximum Size of a Set After Removals problem on LeetCode."
---

## Problem Description

You are given two 0-indexed integer arrays nums1 and nums2 of even length n.

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.

Return the maximum possible size of the set s.

### Examples

**Example 1:**

```
Input: nums1 = [1,2,1,2], nums2 = [1,1,1,1]
Output: 2
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}.
It can be shown that 2 is the maximum possible size of the set s after the removals.

```

**Example 2:**

```
Input: nums1 = [1,2,3,4,5,6], nums2 = [2,3,2,3,2,3]
Output: 5
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}.
It can be shown that 5 is the maximum possible size of the set s after the removals.

```

**Example 3:**

```
Input: nums1 = [1,1,2,2,3,3], nums2 = [4,4,5,5,6,6]
Output: 6
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}.
It can be shown that 6 is the maximum possible size of the set s after the removals.
```

### Constraints

- `n == nums1.length == nums2.length`
- `1 <= n <= 2 * 104`
- `n is even`
- `1 <= nums1[i], nums2[i] <= 109`

## Solution for Minimum Moves to Capture The Queen Problem

### Intuition

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.

### Approach

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.

Calculate the number of elements common to both n1 and n2 and store it in inter_num.

Calculate the difference between the lengths of n1 and n2 and length, representing the excess elements in each array.

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.

Return the sum of the lengths of n1 and n2 minus max_diff, which represents the maximum possible size of the set s.

### Solution Code

#### Python

```py
class Solution:
def maximumSetSize(self, nums1: list[int], nums2: list[int]) -> int:
length, n1, n2 = len(nums1) // 2, set(nums1), set(nums2)
inter_num = len(n1.intersection(n2))
diff = 0

diff += len(n1) - length if len(n1) >= length else 0
diff += len(n2) - length if len(n2) >= length else 0

max_diff=max(diff,inter_num)

return (len(n1)+len(n2))-max_diff

```

#### Java

```java
class Solution {
public int maximumSetSize(int[] nums1, int[] nums2) {
int i,j,n=nums1.length;
Set<Integer> set1=new HashSet<>();
Set<Integer> set2=new HashSet<>();
Set<Integer> set3=new HashSet<>();
for(int x:nums1)
{
set1.add(x);
set3.add(x);
}
for(int x:nums2)
{
set2.add(x);
set3.add(x);
}
int common=set1.size()+set2.size()-set3.size();
int n1=set1.size(),n2=set2.size();
int ans=Math.min(n/2,n1-common);
ans+=Math.min(n/2,n2-common);
ans+=common;
ans=Math.min(n,ans);
return ans;
}
}

```

#### C++

```cpp
class Solution {
public:
int maximumSetSize(vector<int>& nums1, vector<int>& nums2) {
set<int> s1, s2, s;
int n = nums1.size();
for (int x : nums1) {
s1.insert(x);
s.insert(x);
}
for (int x : nums2) {
s2.insert(x);
s.insert(x);
}
int ans = min(min(n/2, (int)s1.size()) + min(n/2, (int)s2.size()),(int) s.size());

return ans;
}
};
```

#### Complexity Analysis

- 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.
- 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.
Loading
Loading