2.0.14 • Published 3 years ago

@algorithm.ts/lower-bound v2.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

A typescript implementation of the lower bound algorithm.

The lower bound algorithm is desired to find the index of first elements which greater or equals than the target element.

Install

  • npm

    npm install --save @algorithm.ts/lower-bound
  • yarn

    yarn add @algorithm.ts/lower-bound
  • deno

    import lowerBound from 'https://raw.githubusercontent.com/guanghechen/algorithm.ts/main/packages/lower-bound/src/index.ts'

Usage

  • Basic

    import lowerBound from '@algorithm.ts/lower-bound'
    
    // elements should be ordered.
    const elements: number[] = [2, 3, 7, 11, 19]
    
    // Find the index of elements which is the first element greater or equal than 8
    // elements[3] = 11 >= 8
    lowerBound(0, elements.length, x => elements[x] - 8) // => 3
    
    // Find the index of elements which is the first element greater or equal than 3
    // elements[1] = 3 >= 3
    lowerBound(0, elements.length, x => elements[x] - 3) // => 1
  • Complex

    import lowerBound from '@algorithm.ts/lower-bound'
    
    const fruits = [
      { type: 'orange', price: 3 },
      { type: 'apple', price: 10 },
      { type: 'banana', price: 10 },
      { type: 'watermelon', price: 12 },
      { type: 'lemon', price: 15 },
    ]
    
    // Find the index of fruits which price is greater or equal than 10
    lowerBound(0, fruits.length, x => fruits[x].price - 10) // => 1
    
    // Find the index of fruits which price is greater or equal than 11
    lowerBound(0, fruits.length, x => fruits[x].price - 11) // => 3
  • Bigint

    import { lowerBoundBigint } from '@algorithm.ts/lower-bound'
    
    lowerBoundBigint(-500000000000n, 5000000000n, x => x - 1n) // => 1n

Related

2.0.14

3 years ago

2.0.13

3 years ago

2.0.12

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.11

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.9

3 years ago

2.0.10

3 years ago

2.0.8

3 years ago

2.0.8-alpha.0

3 years ago

2.0.7-alpha.1

3 years ago

2.0.7-alpha.0

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.0-alpha.0

3 years ago

2.0.1

3 years ago

1.0.24

3 years ago

2.0.0

3 years ago

1.0.23

4 years ago

1.0.19

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago