We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

static int birthday(List<int> s, int d, int m) {
int sumCount = 0;
int sLength = s.Count;
if (m <= sLength) {
int curSum = 0;
for (int j=0; j<m; j++) {
curSum += s[j];
}
if (curSum == d) {
sumCount++;
}
for (int j=m; j<sLength; j++) {
curSum += s[j] - s[j-m];
if (curSum == d) {
sumCount++;
}
}
}
return sumCount;
}

Clever, took me a minute to understand what you did. More efficient than re-adding each series of ints in embedded loops.

Quick Summary of what was done:
1) The first loops adds the first m ints together to see if they equal d.
2) The second loop removes the first digit from the last calculation, and adds a later one to replace it.

## Birthday Chocolate

You are viewing a single comment's thread. Return to all comments →

My C# solution:

Clever, took me a minute to understand what you did. More efficient than re-adding each series of ints in embedded loops.

Quick Summary of what was done: 1) The first loops adds the first m ints together to see if they equal d. 2) The second loop removes the first digit from the last calculation, and adds a later one to replace it.