Skip to content

Python implementation of Mo's Algorithm - a method for answering a series of range queries on the same array. The repository provides a general interface for Mos operations

License

Notifications You must be signed in to change notification settings

jyuv/Mos-Algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mos-Algorithm

Code style: black

Overview

Python implementation of Mo's Algorithm - a method for answering a series of range queries on the same array.

Running time and space complexity of this method are:

The repository provides a general interface for Mos operations to later be adjusted to specific tasks, and 2 examples of such tasks.

Use Case Example 1 - Sum In Range

Given an array this example answers efficiently a series of range queries where each query calculate the sum of its range.

from mos_algorithm import MosAlgorithm, SumInRange
input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
queries = [(0, 4), (1, 3), (2, 4)]
case1 = MosAlgorithm(input_array, queries)
case1.run_queries(SumInRange)

Use Case Example 2 - Unique Items In Range

Given an array this example answers efficiently a series of range queries where each query calculate the number of unique items in its range.

from mos_algorithm import MosAlgorithm, UniqueItemsInRange
input_array = [1, 1, 2, 1, 3, 4, 5, 2, 8]
queries = [(0, 4), (1, 3), (2, 4)]
case1 = MosAlgorithm(input_array, queries)
case1.run_queries(UniqueItemsInRange)

About

Python implementation of Mo's Algorithm - a method for answering a series of range queries on the same array. The repository provides a general interface for Mos operations

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages