Skip to content

Commit 401b153

Browse files
committed
🚀 12-Sep-2020
1 parent 7d529c8 commit 401b153

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
A. Subset Mex
3+
time limit per test1 second
4+
memory limit per test512 megabytes
5+
inputstandard input
6+
outputstandard output
7+
Given a set of integers (it can contain equal elements).
8+
9+
You have to split it into two subsets A and B (both of them can contain equal elements or be empty). You have to maximize the value of mex(A)+mex(B).
10+
11+
Here mex of a set denotes the smallest non-negative integer that doesn't exist in the set. For example:
12+
13+
mex({1,4,0,2,2,1})=3
14+
mex({3,3,2,1,3,0,0})=4
15+
mex(∅)=0 (mex for empty set)
16+
The set is splitted into two subsets A and B if for any integer number x the number of occurrences of x into this set is equal to the sum of the number of occurrences of x into A and the number of occurrences of x into B.
17+
18+
Input
19+
The input consists of multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.
20+
21+
The first line of each test case contains an integer n (1≤n≤100) — the size of the set.
22+
23+
The second line of each testcase contains n integers a1,a2,…an (0≤ai≤100) — the numbers in the set.
24+
25+
Output
26+
For each test case, print the maximum value of mex(A)+mex(B).
27+
28+
Example
29+
inputCopy
30+
4
31+
6
32+
0 2 1 5 0 1
33+
3
34+
0 1 2
35+
4
36+
0 2 0 1
37+
6
38+
1 2 3 4 5 6
39+
outputCopy
40+
5
41+
3
42+
4
43+
0
44+
Note
45+
In the first test case, A={0,1,2},B={0,1,5} is a possible choice.
46+
47+
In the second test case, A={0,1,2},B=∅ is a possible choice.
48+
49+
In the third test case, A={0,1,2},B={0} is a possible choice.
50+
51+
In the fourth test case, A={1,3,5},B={2,4,6} is a possible choice.
52+
53+
54+
*/
55+
56+
57+
58+
59+
60+
61+
62+
#include<bits/stdc++.h>
63+
using namespace std;
64+
65+
int main(){
66+
ios_base::sync_with_stdio(false);
67+
cin.tie(NULL);
68+
69+
int t;
70+
cin>>t;
71+
72+
while(t--){
73+
int n;
74+
cin>>n;
75+
76+
vector<int> v;
77+
for(int i=0;i<n;i++){
78+
int ch;
79+
cin>>ch;
80+
v.push_back(ch);
81+
}
82+
83+
set<int> a, b;
84+
85+
for(auto &x: v){
86+
if(a.find(x)==a.end()){
87+
a.insert(x);
88+
} else b.insert(x);
89+
}
90+
91+
92+
int i, res=0;
93+
for(i=0;i<101;i++){
94+
if(a.find(i)==a.end())
95+
break;
96+
}
97+
98+
res+=i;
99+
100+
for(i=0;i<101;i++){
101+
if(b.find(i)==b.end())
102+
break;
103+
}
104+
105+
res+=i;
106+
107+
cout<<res<<endl;
108+
}
109+
110+
return 0;
111+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.
2+
3+
Example 1:
4+
5+
Input: [2,3,-2,4]
6+
Output: 6
7+
Explanation: [2,3] has the largest product 6.
8+
Example 2:
9+
10+
Input: [-2,0,-1]
11+
Output: 0
12+
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
13+
14+
15+
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
int maxProduct(vector<int>& nums) {
24+
int n=nums.size();
25+
if(n==0) return 0;
26+
int ans=INT_MIN, maxVal=1, minVal=1, prevMax;
27+
for(int i=0;i<n;i++){
28+
if(nums[i]>0){
29+
maxVal=maxVal*nums[i];
30+
minVal=min(1, minVal*nums[i]);
31+
} else if(nums[i]==0){
32+
maxVal=0;
33+
minVal=1;
34+
} else if(nums[i]<0){
35+
prevMax=maxVal;
36+
maxVal=minVal*nums[i];
37+
minVal=prevMax*nums[i];
38+
}
39+
40+
ans=max(ans, maxVal);
41+
42+
if(maxVal<=0)
43+
maxVal=1;
44+
}
45+
return ans;
46+
}
47+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Find all possible combinations of k numbers that add up to a number n,
2+
given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
3+
4+
Note:
5+
6+
All numbers will be positive integers.
7+
The solution set must not contain duplicate combinations.
8+
Example 1:
9+
10+
Input: k = 3, n = 7
11+
Output: [[1,2,4]]
12+
Example 2:
13+
14+
Input: k = 3, n = 9
15+
Output: [[1,2,6], [1,3,5], [2,3,4]]
16+
17+
18+
19+
20+
21+
22+
class Solution {
23+
public:
24+
25+
void combSum3(int k, int start, int target, vector<int> &comb, vector<vector<int> > &res){
26+
if(comb.size()==k && target==0){
27+
res.push_back(comb);
28+
return;
29+
}
30+
31+
if(comb.size()>=k || target<=0)
32+
return;
33+
34+
for(int i=start; i<10;i++){
35+
comb.push_back(i);
36+
combSum3(k, i+1, target-i, comb, res);
37+
comb.erase(comb.end()-1);
38+
}
39+
}
40+
41+
vector<vector<int>> combinationSum3(int k, int n) {
42+
vector<vector<int> > res;
43+
vector<int> comb;
44+
combSum3(k, 1, n, comb, res);
45+
return res;
46+
}
47+
};

0 commit comments

Comments
 (0)