Skip to content

Commit a831001

Browse files
committed
Integer Break
1 parent 0437df7 commit a831001

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

10-October/06-Integer Break.md

+26-25
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
11
## 06. Integer Break
22

33

4-
The problem can be found at the following link: [Question Link](https://leetcode.com/problems/majority-element-ii/description/)
4+
The problem can be found at the following link: [Question Link](https://leetcode.com/problems/integer-break/description)
55

66

77
### My Approach
88

99

10-
1. Initialize variables:
11-
- `n` to store the size of the input vector `nums`.
12-
- Create an unordered map `cnt` to store the count of occurrences of each element.
13-
- Initialize an empty vector `ans` to store the majority elements.
1410

15-
2. Iterate through the `nums` vector using a for loop:
16-
- Use the `cnt` map to count the occurrences of each element in the `nums` vector.
11+
1. Check if `n` is equal to 2:
12+
- If `n` is 2, return 1. This is because the maximum product of two positive integers that sum up to 2 is 1 * 1.
1713

18-
3. Iterate through the elements in the `cnt` map:
19-
- Check if the count (`j->second`) of an element is greater than one-third of the total elements (`n/3`).
20-
- If the count meets the condition, add the element (`j->first`) to the `ans` vector.
14+
2. Check if `n` is equal to 3:
15+
- If `n` is 3, return 2. This is because the maximum product of two positive integers that sum up to 3 is 1 * 2.
16+
17+
3. Calculate the integer division `x` of `n` by 3:
18+
- `x = n / 3`
19+
20+
4. Check if `n` is divisible by 3 (i.e., `n % 3 == 0`):
21+
- If `n` is divisible by 3, return the result of raising 3 to the power of `x` using the `pow` function. This is because you can split `n` into `x` equal parts, each of size 3, to maximize the product.
22+
23+
5. Check if `(n - 1)` is divisible by 3 (i.e., `(n - 1) % 3 == 0`):
24+
- If `(n - 1)` is divisible by 3, return the result of raising 3 to the power of `x - 1` and multiplying it by 4. This is because you can split `n` into `x - 1` equal parts of size 3 and one part of size 4 to maximize the product.
25+
26+
6. If none of the above conditions are met:
27+
- Return the result of raising 3 to the power of `x` and multiplying it by 2. This is because you can split `n` into `x` equal parts of size 3 and one part of size 2 to maximize the product.
2128

22-
4. After both loops have finished, the `ans` vector will contain the majority elements (elements that appear more than one-third of the time) in the input vector `nums`.
2329

24-
5. Return the `ans` vector as the result.
2530

2631

2732

2833
### Time and Auxiliary Space Complexity
2934

30-
- **Time Complexity**: `O(n)`
31-
- **Auxiliary Space Complexity**: `O(n)`
35+
- **Time Complexity**: `O(1)`
36+
- **Auxiliary Space Complexity**: `O(1)`
3237

3338

3439

@@ -38,17 +43,13 @@ The problem can be found at the following link: [Question Link](https://leetcode
3843

3944
class Solution {
4045
public:
41-
vector<int> majorityElement(vector<int>& nums) {
42-
int n=nums.size();
43-
unordered_map<int, int> cnt;
44-
vector<int> ans;
45-
for(int i=0;i<n;i++){
46-
cnt[nums[i]]++;
47-
}
48-
for(auto j=cnt.begin();j!=cnt.end();j++){
49-
if(j->second>n/3)ans.push_back(j->first);
50-
}
51-
return ans;
46+
int integerBreak(int n) {
47+
if(n==2) return 1;
48+
if(n==3) return 2;
49+
int x = n/3;
50+
if(n%3==0) return pow(3,x);
51+
else if((n-1)%3==0) return pow(3,x-1)*4;
52+
else return pow(3,x)*2;
5253
}
5354
};
5455

0 commit comments

Comments
 (0)