Skip to content

Conversation

@stillwearit
Copy link

No description provided.

@KFalcon2022 KFalcon2022 self-assigned this Feb 4, 2025
int stepsCount = 0;
int leftover = wateringCanVolume;
for (int i = 0; i < plants.length; i++) {
if (plants[i] < 1 || plants[i] > wateringCanVolume) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

уже писал - валидацию и логику стоит разделять

return -1;
}

if (leftover >= plants[i]) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

этот if-else можно упростить. Фактически, ты все описал верно. Но есть мнение, что увеличение шага на один и затраты на полив происходят всегда (на каждой итерации цикла). И лишь дополнительными действиями являются поход к реке и наполнение лейки. То есть тут фактически нет разветвления на два сценария, есть лишь небольшой крюк при определенном условии:)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вроде бы поняла, спасибо!

return stepsCount;
}

static boolean isValid (int plant, int wateringCanVolume) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лишний пробел после имени метода

int stepsCount = 0;
int leftover = wateringCanVolume;
for (int i = 0; i < plants.length; i++) {
if (!isValid(plants[i], wateringCanVolume)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

валидация все еще не выдела из логики. Где гарантия, что на plants[plants.length - 1] мы не обнаружим не валидное число? Получится, что все итерации до этого затратили лишние ресурсы процессора

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

думала, лучше всё одним циклом сделать, теперь понимаю, почему не лучше 😄

stepsCount += i * 2;
leftover = wateringCanVolume - plants[i];
}
stepsCount += 1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

кажется, табуляция поплыла

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants