Finding two non-subsequent elements in array which sum is minimal

前端 未结 13 1021
小蘑菇
小蘑菇 2021-02-05 03:40

Intro: As far as I could search, this question wasn\'t asked in SO yet.
This is an interview question.
I am not even specifically looking for a code sol

13条回答
  •  梦如初夏
    2021-02-05 04:01

    I think this should work:

    Find the minimum 3 element and their indices. Since all of them can't be adjacent choose 2 among them.

    If all of them are adjacent and the minimum number is in the middle of them, iterate through all elements, find the forth minimum element, choose minimum of min1+min4, min2+min3, whichever is smaller.

    You can do this in one iteration too.

提交回复
热议问题