Redundant code in 13.8

#1

Hello,
I found that in problem 13.8 (Find smallest subarray covering all values), the following code may be unnecessary. This is because the result will finally be set later when advancing the left index.

// Found all keywords, update the smallest subarray containing all keywords.
if (keywords_to_count.size() == keywords.size() &&
    ((result.first == -1 && result.second == -1) ||
     right - 1 - left < result.second - result.first)) {
  result = {left, right - 1};
}

Moreover, I think the condition “left < right” may be also redundant when advancing the left because the keywords_to_count map will finally reduce to less than the keywords.size():

while (**left < right &&** keywords_to_count.size() == keywords.size())

I removed the above codes and tested with the EPI testing engine and have found no assertion error.

0 Likes

#2

Hey Duc,

Impressive observation! I have double-checked this code, and think you are correct that this part of code could be removed which definitely make this implement more succinct. Greatly appreciated your efforts on this!

0 Likes