[Python] How to find the closest smaller value in a large sorted array efficiently

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    For a sorted list, how can I find the smallest number which close to the a given number?

    For example,

    mysortedList = [37, 72, 235, 645, 715, 767, 847, 905, 908, 960]


    How can I find the largest element which is less or equal to 700 quickly? (If I have 10 million elements, then it will be slow to search linearly). In this example, the answer is 645.

    Continue reading...

Compartilhe esta Página