-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximalSubsetSum.cpp
More file actions
88 lines (84 loc) · 1.32 KB
/
MaximalSubsetSum.cpp
File metadata and controls
88 lines (84 loc) · 1.32 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
BASIC INFO:
stu ID:40843245
name:Huang Jay
Due date:2022/11/25
HW: Algorithm HW3-1
*/
#include <iostream>
using namespace std;
//This code solves Maximal Subsequent Subset Sum problem
class MaximalSubsetSum
{
private:
int size;
int *arr;
int *f;
int ans;
public:
MaximalSubsetSum(int size)
{
this->size=size;
this->arr=new int[this->size];
this->f=new int[this->size];
this->ans=0;
this->FetchInput();
this->Init();
this->FindMaximalSubsetSum();
this->DisplayResult();
}
public:
void FetchInput()
{
int val=0;
for(int i=0;i<this->size;i++)
{
cin>>val;
this->arr[i]=val;
}
}
void Init()
{
for(int i=0;i<this->size;i++)
{
this->f[i]=0;
}
}
void FindMaximalSubsetSum()
{
for(int i=0;i<this->size;i++)
{
if(i==0 || this->f[i-1]<=0)
{
this->f[i]=this->arr[i];
}else
{
this->f[i]=this->f[i-1]+this->arr[i];
}
}
this->ans=this->f[0];
for(int i=0;i<this->size;i++)
{
if(this->f[i]>this->ans)
{
this->ans=this->f[i];
}
}
}
void DisplayResult()
{
cout<<this->ans<<endl;
}
};
int main()
{
int numOfData=0;
int size=0;
cin>>numOfData;
while(numOfData--)
{
cin>>size;
MaximalSubsetSum *maximalSubsetSum=new MaximalSubsetSum(size);
}
return 0;
}