Skip to content

Added leetcode problem:414 and 628 #999 #1018

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 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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,75 @@
---
id: third-maximum-number
title: Third Maximum Number
sidebar_label: 0414 Third Maximum Number
tags:
- Math
- Vector
- Set
- LeetCode
- C++
description: "This is a solution to the Third Maximum Number problem on LeetCode."
---

## Problem Description

Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.

### Examples

**Example 1:**

```

Input: nums = [1,2]
Output: 2
```

**Example 2:**

```
Input: nums = [2,2,3,1]
Output: 1
```

**Example 2:**

```
Input: root = [0]
Output: [0]
```

### Constraints

- $1 \leq \text{nums.length} \leq 10^4$.
- $-2^(31) \leq \text{Node.val} \leq 2^(31)-1$.

### Approach

To solve this problem(third maximum element) first we will store the number from the array/vector in set to get all the unique number from the given array then if size of the set is less than 3 we will just return maximum element from the array otherwise we will return the third element from the set.

#### Code in C++

```cpp
class Solution {
public:
int thirdMax(vector<int>& nums) {
set<int>b;
for(int i=0;i<nums.size();i++){
b.insert(nums.at(i));
}
int n=b.size();
if(n<3){
return *max_element(nums.begin() ,nums.end());
}
vector<int>p;
for(auto x :b){
p.push_back(x);
}

return p.at(n-3);
}
};
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
id: maximum-product-of-three-numbers
title: Maximum Product of Three Numbers
sidebar_label: 628 Maximum Product of Three Numbers
tags:
- Math
- Vector
- LeetCode
- C++
description: "This is a solution to the Maximum Product of Three Numbers problem on LeetCode."
---

## Problem Description

Given an integer array nums, find three numbers whose product is maximum and return the maximum product.

### Examples

**Example 1:**

```

Input: nums = [1,2,3]
Output: 6
```

**Example 2:**

```
Input: nums = [1,2,3,4]
Output: 24
```

**Example 2:**

```
Input: nums = [-1,-2,-3]
Output: -6
```

### Constraints

- $3 \leq \text{nums.length} \leq 10^4$.
- $-1000 \leq \text{Node.val} \leq 1000$.

### Approach

To solve this problem(third maximum element) we will sort the given array and then return the product of last three numbers but there are chances that product of two smallest negative number(starting first two numbers generally) with largest positive number will be greater than last three numbers product so we will check that also.

#### Code in C++

```cpp
class Solution {
public:
int maximumProduct(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n=nums.size();
int l=nums[n-3]*nums[n-2]*nums[n-1]; // prouduct of maximum numbers
int p=nums[0]*nums[1]*nums[n-1];// here we are checking if (num[0] and num[1] is negative so may this product will be greater than the product(l) which is product of last three maximum numbers)
if(p>l){
return p;
}
return l;
}
};
```

#### code in Python

```python
class Solution(object):
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
p=nums[0]*nums[1]*nums[-1] #Calculate the product of the two smallest numbers (negative numbers) and the largest number in the sorted list.
l=nums[-1]*nums[-2]*nums[-3] #Calculate the product of the three largest numbers in the sorted list.
return max(l,p)

```

Loading