-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreaking The Records.cpp
More file actions
150 lines (96 loc) · 3.62 KB
/
Breaking The Records.cpp
File metadata and controls
150 lines (96 loc) · 3.62 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
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
Task: Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.
For example, assume her scores for the season are represented in the array scores = [12,24,10,24]. Scores are in the same order as the games played. She would tabulate her results as follows:
Count
Game Score Minimum Maximum Min Max
0 12 12 12 0 0
1 24 12 24 0 1
2 10 10 24 1 1
3 24 10 24 1 1
Given the scores for a season, find and print the number of times Maria breaks her records for most and least points scored during the season.
Function Description
Complete the breakingRecords function in the editor below. It must return an integer array containing the numbers of times she broke her records. Index 0 is for breaking most points records, and index 1 is for breaking least points records.
breakingRecords has the following parameter(s):
scores: an array of integers
Input Format
The first line contains an integer n, the number of games.
The second line contains n space-separated integers describing the respective values of score0,score1,score2,......scoren-1.
Constraints
1<=n<=1000
0<=score[i]<=10^8
Output Format
Print two space-seperated integers describing the respective numbers of times the best (highest) score increased and the worst (lowest) score decreased.
Sample Input 0
9
10 5 20 20 4 5 2 25 1
Sample Output 0
2 4
Solution:
#include <bits/stdc++.h>
using namespace std;
vector<string> split_string(string);
// Complete the breakingRecords function below.
vector<int> breakingRecords(vector<int> scores) {
vector<int> Minmax(2); //VERY CRUCIAL FOR AVOIDING SEGMENTATION FAULT
int i=0;
long long int Min=0, Max=0;
Max = scores.at(i);
Min = scores.at(i);i++;
while (i< scores.size())
{
if(scores.at(i) > Max)
{
Max = scores.at(i);Minmax.at(0)++;
}
else if(scores.at(i) < Min)
{
Min = scores.at(i);Minmax.at(1)++;
}
i++;
}
return(Minmax);
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string scores_temp_temp;
getline(cin, scores_temp_temp);
vector<string> scores_temp = split_string(scores_temp_temp);
vector<int> scores(n);
for (int i = 0; i < n; i++) {
int scores_item = stoi(scores_temp[i]);
scores[i] = scores_item;
}
vector<int> result = breakingRecords(scores);
for (int i = 0; i < result.size(); i++) {
fout << result[i];
if (i != result.size() - 1) {
fout << " ";
}
}
fout << "\n";
fout.close();
return 0;
}
vector<string> split_string(string input_string) {
string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
return x == y and x == ' ';
});
input_string.erase(new_end, input_string.end());
while (input_string[input_string.length() - 1] == ' ') {
input_string.pop_back();
}
vector<string> splits;
char delimiter = ' ';
size_t i = 0;
size_t pos = input_string.find(delimiter);
while (pos != string::npos) {
splits.push_back(input_string.substr(i, pos - i));
i = pos + 1;
pos = input_string.find(delimiter, i);
}
splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
return splits;
}