Skip to content

Commit 7674841

Browse files
authored
Merge pull request #1018 from Anshika14528/628
Added leetcode problem:414 and 628 #999
2 parents cc01faf + 4ba84c3 commit 7674841

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
id: third-maximum-number
3+
title: Third Maximum Number
4+
sidebar_label: 0414 Third Maximum Number
5+
tags:
6+
- Math
7+
- Vector
8+
- Set
9+
- LeetCode
10+
- C++
11+
description: "This is a solution to the Third Maximum Number problem on LeetCode."
12+
---
13+
14+
## Problem Description
15+
16+
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.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
```
23+
24+
Input: nums = [1,2]
25+
Output: 2
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: nums = [2,2,3,1]
32+
Output: 1
33+
```
34+
35+
**Example 2:**
36+
37+
```
38+
Input: root = [0]
39+
Output: [0]
40+
```
41+
42+
### Constraints
43+
44+
- $1 \leq \text{nums.length} \leq 10^4$.
45+
- $-2^(31) \leq \text{Node.val} \leq 2^(31)-1$.
46+
47+
### Approach
48+
49+
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.
50+
51+
#### Code in C++
52+
53+
```cpp
54+
class Solution {
55+
public:
56+
int thirdMax(vector<int>& nums) {
57+
set<int>b;
58+
for(int i=0;i<nums.size();i++){
59+
b.insert(nums.at(i));
60+
}
61+
int n=b.size();
62+
if(n<3){
63+
return *max_element(nums.begin() ,nums.end());
64+
}
65+
vector<int>p;
66+
for(auto x :b){
67+
p.push_back(x);
68+
}
69+
70+
return p.at(n-3);
71+
}
72+
};
73+
```
74+
75+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
id: maximum-product-of-three-numbers
3+
title: Maximum Product of Three Numbers
4+
sidebar_label: 628 Maximum Product of Three Numbers
5+
tags:
6+
- Math
7+
- Vector
8+
- LeetCode
9+
- C++
10+
description: "This is a solution to the Maximum Product of Three Numbers problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
15+
Given an integer array nums, find three numbers whose product is maximum and return the maximum product.
16+
17+
### Examples
18+
19+
**Example 1:**
20+
21+
```
22+
23+
Input: nums = [1,2,3]
24+
Output: 6
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: nums = [1,2,3,4]
31+
Output: 24
32+
```
33+
34+
**Example 2:**
35+
36+
```
37+
Input: nums = [-1,-2,-3]
38+
Output: -6
39+
```
40+
41+
### Constraints
42+
43+
- $3 \leq \text{nums.length} \leq 10^4$.
44+
- $-1000 \leq \text{Node.val} \leq 1000$.
45+
46+
### Approach
47+
48+
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.
49+
50+
#### Code in C++
51+
52+
```cpp
53+
class Solution {
54+
public:
55+
int maximumProduct(vector<int>& nums) {
56+
sort(nums.begin(),nums.end());
57+
int n=nums.size();
58+
int l=nums[n-3]*nums[n-2]*nums[n-1]; // prouduct of maximum numbers
59+
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)
60+
if(p>l){
61+
return p;
62+
}
63+
return l;
64+
}
65+
};
66+
```
67+
68+
#### code in Python
69+
70+
```python
71+
class Solution(object):
72+
def maximumProduct(self, nums):
73+
"""
74+
:type nums: List[int]
75+
:rtype: int
76+
"""
77+
nums.sort()
78+
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.
79+
l=nums[-1]*nums[-2]*nums[-3] #Calculate the product of the three largest numbers in the sorted list.
80+
return max(l,p)
81+
82+
```
83+

0 commit comments

Comments
 (0)