-
Notifications
You must be signed in to change notification settings - Fork 0
/
financeAnalyze.c
154 lines (141 loc) · 3.04 KB
/
financeAnalyze.c
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
// Linked list.
typedef struct linkNode
{
int value;
struct linkNode *next;
} node;
// FIFO queue.
typedef struct linkQueue
{
node *firstPtr;
node *lastPtr;
int sum;
} queue;
void forwardNodePtr(node **ptr)
{
*ptr = (*ptr)->next;
}
node *createLinkNode()
{
node *newNode = (node *)malloc(sizeof(node));
newNode->value = 0;
newNode->next = NULL;
return newNode;
}
queue *createQueue()
{
queue *newQueue = (queue *)malloc(sizeof(queue));
newQueue->firstPtr = NULL;
newQueue->lastPtr = NULL;
newQueue->sum = 0;
// newQueue->length = 0;
return newQueue;
}
void destroyLinkNode(node *tgtNode)
{
// free(tgtNode->value);
free(tgtNode);
}
void destroyQueue(queue *tgtQueue)
{
if (tgtQueue != NULL)
{
if (tgtQueue->firstPtr != NULL)
{
if (tgtQueue->firstPtr->next != NULL)
{
node *currentNode;
while (tgtQueue->firstPtr->next != NULL)
{
currentNode = tgtQueue->firstPtr;
forwardNodePtr(&(tgtQueue->firstPtr));
destroyLinkNode(currentNode);
}
}
destroyLinkNode(tgtQueue->firstPtr);
}
free(tgtQueue);
}
else
{
perror("Queue not initialized");
}
}
int queuePop(queue *tgtQueue) // Same as pop + front.
{
if (tgtQueue != NULL)
{
if (tgtQueue->firstPtr == NULL)
{
perror("Empty Queue");
}
else
{
int value = tgtQueue->firstPtr->value;
node *oldPtr = tgtQueue->firstPtr;
forwardNodePtr(&(tgtQueue->firstPtr));
if (tgtQueue->firstPtr == NULL)
{
tgtQueue->lastPtr = NULL;
}
destroyLinkNode(oldPtr);
// tgtQueue->length--;
tgtQueue->sum -= value;
return value;
}
}
else
{
perror("Queue not initialized");
return 0;
}
}
void queuePush(int input, queue *tgtQueue)
{
if (tgtQueue != NULL)
{
if (tgtQueue->firstPtr == NULL)
{
tgtQueue->firstPtr = createLinkNode();
tgtQueue->lastPtr = tgtQueue->firstPtr;
}
else
{
tgtQueue->lastPtr->next = createLinkNode();
tgtQueue->lastPtr = tgtQueue->lastPtr->next;
}
// tgtQueue->length++;
tgtQueue->lastPtr->value = input;
tgtQueue->sum += input;
}
else
{
perror("Queue not initialized");
}
}
int getMonth(int n, int t)
{
int count = 0;
int income;
queue *incomeQueue = createQueue();
for (int i = 0; i < n ; i++)
{
scanf("%d", &income);
queuePush(income, incomeQueue);
while (incomeQueue->sum >= t)
{
queuePop(incomeQueue);
}
++count;
}
return count;
}
int main(void)
{
int n, t;
scanf("%d%d", &n, &t);
printf("%d", getMonth(n, t));
return 0;
}