-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAY 15 week 3 Contain Duplicate.cpp
55 lines (50 loc) · 1.12 KB
/
DAY 15 week 3 Contain Duplicate.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Contain Duplicate.cpp leetcode
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
/* for(int i=0; i<nums.size(); i++)
for(int j=i+1; j<nums.size() && j<=i+1; j++)
{
if(nums[j]==nums[i])
return true;
}
return false;
}
int main()
{
vector<int> nums={1,2,3,1};
int k=3;
if(containsDuplicate(nums,k))
cout<<"true"<<endl;
else
cout<<"false"<<endl;
return 0;*/
unordered_map<int,int>count;
for(auto j:nums)
if(++count[j]>1)return true;
return false;
}
};
// Maximum Subarray.cpp leet code
class Solution {
public:
int maxSubArray(vector<int>& nums) {
if(nums.size()<0)
return 0;
int run_sum=nums[0];
int max_sum=nums[0];
for(int i=1; i<nums.size(); i++)
{
if(run_sum<0)
{
run_sum =0;
}
run_sum=run_sum+nums[i];
if(run_sum>max_sum)
{
max_sum=run_sum;
}
}
return max_sum;
}
};