Skip to content

Commit a2793e8

Browse files
author
Carlos Leonard
committed
Group Total
1 parent 5cdb422 commit a2793e8

19 files changed

+184
-115
lines changed

50_FoodDistribution.cpp

Lines changed: 50 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,31 @@ Another example : if arr is[4, 5, 2, 3, 1, 0] then you can distribute the sandwi
99
#include <iostream>
1010
#include <string>
1111
#include <vector>
12+
#include <algorithm>
1213
using namespace std;
1314

14-
void bubbleSort(vector <int>& list, vector <int>& index)
15-
{
16-
int temp, temp2;
17-
bool swap;
18-
19-
do
20-
{
21-
swap = false;
2215

23-
for (int x = 1; x < list.size()-1; x++)
24-
{
25-
// Will move the elements around in the new list
26-
// Also keeps track of the index in relation to the original array
27-
if (list[x] < list[x + 1])
28-
{
29-
temp = list[x];
30-
temp2 = index[x];
31-
index[x] = index[x + 1];
32-
index[x + 1] = temp2;
33-
list[x] = list[x + 1];
34-
list[x + 1] = temp;
35-
swap = true;
36-
}
37-
}
38-
} while (swap);
39-
}
16+
/*
17+
NEEDS FIX
18+
19+
Will apply a greedy approach
20+
first we will sort the hunger levels in descending order
21+
with the new order, we will traverse each value and compare it with its neighbor
22+
the goal is to always match the level together to minimize hunger levels
23+
so for example if current is 4 and neighbor is 2
24+
we do our best to bring the current hunger level to 2
4025
26+
for each change we update our total sandwiches
27+
we also restart our iteration each time an change in hunger levels occurs
28+
*/
4129
int FoodDistribution(int arr[], int size)
4230
{
43-
int total = arr[0]; // Number of Sandwiches
31+
int numSandwiches = arr[0]; // Number of Sandwiches
4432
int difference = 0; // Use to store the difference
4533
bool give; // Signal each time a distribution takes place
4634

35+
vector <int> newList; // list used to sort our values
36+
4737
// Checking for the difference of the original
4838
// In some cases we might not need to do food distribution if the difference across is 0
4939
for (int x = 1; x < size-1; x++)
@@ -54,51 +44,58 @@ int FoodDistribution(int arr[], int size)
5444
temp *= -1;
5545
}
5646
difference += temp;
47+
48+
newList.push_back(arr[x]);
5749
}
5850
if (difference == 0)
5951
{
6052
return 0;
6153
}
6254

63-
vector <int> newList, indexConect;
64-
newList.push_back(-1); // Garbage value just to keep the size parallel to original array
65-
indexConect.push_back(-1);
66-
67-
for (int x = 1; x < size; x++)
68-
{
69-
newList.push_back(arr[x]);
70-
indexConect.push_back(x);
71-
}
55+
// adding last element
56+
newList.push_back(arr[size - 1]);
7257

73-
bubbleSort(newList, indexConect);
58+
// sorting in descending order
59+
sort(newList.rbegin(), newList.rend());
7460

75-
// Loop to distribute the food
61+
// loop to balance hunger levels of each
7662
do
7763
{
7864
give = false;
79-
for (int x = 1; x < newList.size() - 1 && total > 0; x++)
65+
for (int x = 0; x < newList.size() - 1; x++)
8066
{
81-
if (newList[x] > newList[x + 1])
67+
// check the difference between the 2 neighbors
68+
int temp = newList[x] - newList[x + 1];
69+
70+
// condition to check if hunger level for current person can be updated
71+
if (temp != 0 && numSandwiches > 0)
8272
{
83-
give = true;
84-
newList[x]--;
85-
total--;
73+
if (temp < 0)
74+
{
75+
// give this person the remaining sandwiches we had
76+
newList[x] -= numSandwiches;
77+
give = true;
78+
}
79+
else
80+
{
81+
// give this person the total difference between the their neighbor
82+
// also update our sandwich count
83+
newList[x] -= temp;
84+
numSandwiches -= temp;
85+
give = true;
86+
}
87+
8688
break;
8789
}
8890
}
8991
} while (give);
9092

91-
// Loop to match the new list to the original order
92-
for (int x = 1; x < newList.size(); x++)
93-
{
94-
arr[x] = newList[indexConect[x]];
95-
}
9693

97-
// Checking for the difference after the distribution took place
94+
// final pass to get difference after our greedy approach
9895
difference = 0;
99-
for (int x = 1; x < size - 1; x++)
96+
for (int x = 0; x < newList.size()-1; x++)
10097
{
101-
int temp = arr[x] - arr[x + 1];
98+
int temp = newList[x] - newList[x + 1];
10299
if (temp < 0)
103100
{
104101
temp *= -1;
@@ -118,42 +115,16 @@ int main()
118115
int E[] = { 3, 2, 1, 0, 4, 1, 0 };
119116
int F[] = { 4, 5, 4, 5, 2, 3, 1, 2 };
120117
int G[] = { 7, 5, 4, 3, 4, 5, 2, 3, 1, 4, 5 };
118+
int H[] = { 7, 1,1,1,1,1 };
119+
121120
cout << FoodDistribution(A, sizeof(A)/sizeof(A[0])) << endl; // 0
122121
cout << FoodDistribution(B, sizeof(B) / sizeof(B[0])) << endl; // 2
123122
cout << FoodDistribution(C, sizeof(C) / sizeof(C[0])) << endl; // 0
124123
cout << FoodDistribution(D, sizeof(D) / sizeof(D[0])) << endl; // 1
125124
cout << FoodDistribution(E, sizeof(E) / sizeof(E[0])) << endl; // 4
126125
cout << FoodDistribution(F, sizeof(F) / sizeof(F[0])) << endl; // 3
127126
cout << FoodDistribution(G, sizeof(G) / sizeof(G[0])) << endl; // 6
128-
129-
/*
130-
5 4 5 2 3 1 2
131-
132-
4 4 4 2 3 1 2
133-
4 4 4 2 2 1 2
134-
4 4 4 2 2 1 1
135-
0 0 2 0 1 0
136-
137-
138-
5 5 4 3 2 2 1 4
139-
5 4 4 3 2 2 1 3
140-
4 4 4 3 2 2 1 2
141-
4 4 3 3 2 2 1 1
142-
4 3 3 3 2 2 1 0
143-
144-
4 3 3 2 3 1 2
145-
1 0 1 1 2 1
146-
147-
148-
149-
5 4 3 4 5 2 3 1 4 5
150-
151-
152-
153-
154-
*/
155-
156-
127+
cout << FoodDistribution(H, sizeof(H) / sizeof(H[0])) << endl; // 0
157128

158129
return 0;
159130
}

81_MovingMedian.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ int MovingMedian(int arr[], int size)
1919
int main()
2020
{
2121
int A[] = { 3, 1, 3, 5, 10, 6, 4, 3, 1 };
22-
int A[] = { 5, 2, 4, 6 };
23-
int A[] = { 3, 0, 0, -2, 0, 2, 0, -2 };
22+
int B[] = { 5, 2, 4, 6 };
23+
int C[] = { 3, 0, 0, -2, 0, 2, 0, -2 };
2424

2525
cout << MovingMedian(A, sizeof(A) / sizeof(A[0])) << endl; // 1,2,3,5,6,6,4,3
2626
cout << MovingMedian(B, sizeof(B) / sizeof(B[0])) << endl; // 2,3,4

83_GroupTotals.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// For this challenge you will be adding values from key, value pairs.
2+
/*
3+
have the function GroupTotals(strArr) read in the strArr parameter containing key:value pairs where the key is a string and the value is an integer. Your program should return a string with new key:value pairs separated by a comma such that each key appears only once with the total values summed up.
4+
For example: if strArr is ["B:-1", "A:1", "B:3", "A:5"] then your program should return the string A:6,B:2.
5+
6+
Your final output string should return the keys in alphabetical order. Exclude keys that have a value of 0 after being summed up.
7+
*/
8+
9+
#include <iostream>
10+
#include <string>
11+
#include <map>
12+
#include <sstream>
13+
using namespace std;
14+
15+
/*
16+
we traverse the string array
17+
store key value pairs into a hash table
18+
if key is already in our table we sum up the values
19+
else create a new key value pair in the table
20+
lastly we output the result, ignore any keys with a value of 0
21+
*/
22+
string GroupTotals(string strArr[], int size)
23+
{
24+
// our hast table
25+
map <string, int> table;
26+
27+
// traversing the string array to extract the key value pairs
28+
for (int x = 0; x < size; x++)
29+
{
30+
string key;
31+
int value;
32+
33+
// extracting the key
34+
for (int y = 0; y < strArr[x].length(); y++)
35+
{
36+
// breakpoint
37+
if (strArr[x][y] == ':')
38+
{
39+
break;
40+
}
41+
else
42+
{
43+
key += strArr[x][y];
44+
}
45+
}
46+
47+
// extracting the value
48+
string num;
49+
for (int y = strArr[x].find(':') + 1; y < strArr[x].length(); y++)
50+
{
51+
num += strArr[x][y];
52+
}
53+
54+
istringstream convert(num);
55+
convert >> value;
56+
57+
// conditions to check the new data gathered
58+
// if the pair is new we just add it to our table
59+
// else we perform simple addition to the already existing pair
60+
if (table.count(key) == 0)
61+
{
62+
table[key] = value;
63+
}
64+
else
65+
{
66+
table[key] += value;
67+
}
68+
}
69+
70+
// gathering our result
71+
string result;
72+
73+
for (auto current = table.begin(); current != table.end(); current++)
74+
{
75+
// we ignore any pairs that have a value of 0
76+
if (current->second != 0)
77+
{
78+
ostringstream convert;
79+
convert << current->second;
80+
string currentResult = current->first + ":" + convert.str();
81+
result += currentResult + ",";
82+
}
83+
}
84+
85+
result.pop_back(); // remove last comma
86+
return result;
87+
}
88+
89+
int main()
90+
{
91+
string A[] = { "B:-1", "A:1", "B:3", "A:5" };
92+
string B[] = { "X:-1", "Y:1", "X:-4", "B:3", "X:5" };
93+
string C[] = { "Z:0", "A:-1" };
94+
95+
cout << GroupTotals(A, sizeof(A) / sizeof(A[0])) << endl; // A:6,B:2
96+
cout << GroupTotals(B, sizeof(B) / sizeof(B[0])) << endl; // B:3,Y:1
97+
cout << GroupTotals(C, sizeof(C) / sizeof(C[0])) << endl; // A:-1
98+
return 0;
99+
}

Debug/80_StarRating.obj

-186 KB
Binary file not shown.
Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\vc120.pdb
22
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\vc120.idb
3-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code37.obj
4-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code45.obj
5-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\test.obj
6-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code55.obj
7-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code50.obj
8-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code56.obj
9-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code58.obj
10-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code57.obj
11-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code59.obj
12-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\6_longestword.obj
3+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\80_starrating.obj
4+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\50_fooddistribution.obj
135
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\60_closestenemy.obj
146
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\61_largestfour.obj
157
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\62_questionmarks.obj
@@ -20,25 +12,34 @@ c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practic
2012
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\67_onedecremented.obj
2113
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\68_distinctcharacters.obj
2214
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\69_snakecase.obj
15+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\6_longestword.obj
2316
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\70_elementmerger.obj
2417
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\71_asciiconversion.obj
2518
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\72_gcf.obj
19+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\73_closestenemy2.obj
2620
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\74_serialnumber.obj
2721
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\75_stringperiods.obj
28-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\73_closestenemy2.obj
2922
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\76_numberstream.obj
3023
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\77_palindromeswapper.obj
3124
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\78_removebrackets.obj
3225
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\79_commandline.obj
33-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\80_starrating.obj
34-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\debug\fun practice.ilk
35-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\debug\fun practice.exe
26+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code37.obj
27+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code45.obj
28+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code50.obj
29+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code55.obj
30+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code56.obj
31+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code57.obj
32+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code58.obj
33+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\code59.obj
34+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\test.obj
35+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.ilk
36+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\vc140.idb
37+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.pdb
38+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\vc140.pdb
3639
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\debug\fun practice.pdb
37-
c:\users\carlo\documents\github\fun-practice\debug\vc120.idb
38-
c:\users\carlo\documents\github\fun-practice\debug\fun practice.exe
39-
c:\users\carlo\documents\github\fun-practice\debug\fun practice.tlog\cl.command.1.tlog
40-
c:\users\carlo\documents\github\fun-practice\debug\fun practice.tlog\cl.read.1.tlog
41-
c:\users\carlo\documents\github\fun-practice\debug\fun practice.tlog\cl.write.1.tlog
42-
c:\users\carlo\documents\github\fun-practice\debug\fun practice.tlog\link.command.1.tlog
43-
c:\users\carlo\documents\github\fun-practice\debug\fun practice.tlog\link.read.1.tlog
44-
c:\users\carlo\documents\github\fun-practice\debug\fun practice.tlog\link.write.1.tlog
40+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.tlog\cl.command.1.tlog
41+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.tlog\cl.read.1.tlog
42+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.tlog\cl.write.1.tlog
43+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.tlog\link.command.1.tlog
44+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.tlog\link.read.1.tlog
45+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\debug\fun practice.tlog\link.write.1.tlog

Debug/Fun Practice.ilk

-510 KB
Binary file not shown.

Debug/Fun Practice.log

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
Build started 1/2/2018 11:49:38 AM.
1+
Build started 4/30/2018 11:50:08 AM.
22
1>Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" on node 2 (Build target(s)).
33
1>ClCompile:
4-
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 80_StarRating.cpp
5-
80_StarRating.cpp
4+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 83_GroupTotals.cpp
5+
83_GroupTotals.cpp
6+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\83_grouptotals.cpp(34): warning C4018: '<' : signed/unsigned mismatch
7+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice\fun practice\83_grouptotals.cpp(49): warning C4018: '<' : signed/unsigned mismatch
68
Link:
7-
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\80_StarRating.obj
9+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.lib" /MACHINE:X86 Debug\83_GroupTotals.obj
810
Fun Practice.vcxproj -> C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Debug\Fun Practice.exe
911
1>Done Building Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice\Fun Practice\Fun Practice.vcxproj" (Build target(s)).
1012

1113
Build succeeded.
1214

13-
Time Elapsed 00:00:03.96
15+
Time Elapsed 00:00:01.47

Debug/Fun Practice.pdb

-948 KB
Binary file not shown.
-2.45 KB
Binary file not shown.
324 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)