Is there any efficient algorithm to find the number of iterations of any two values when the values are changing in a certain range? [closed]
开发者_如何学运维
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 hours ago.
The community is reviewing whether to reopen this question as of 4 hours ago.
Improve this questionMain Codes
class NumIteration {
input(num) {
// do something
}
getIteration(num1, num2) {
// do something
}
}
Test Case:
const ni = new NumIteration()
ni.input(1)
ni.input(2) // 1 -> 2
ni.input(1) // 2 -> 1
ni.getIteration(1, 2) // return 1
ni.input(2) // 1 -> 2
ni.getIteration(1, 2) // return 2
ni.getIteration(2, 1) // return 1
ni.input(3) // 2 -> 3
ni.getIteration(2, 3) // return 1
ni.getIteration(1, 3) // return 1
ni.getIteration(1, 2) // return 2
ni.input(2) // 3 -> 2
ni.getIteration(3, 2) // return 1
ni.getIteration(2, 3) // return 1
ni.input(1) // 2 -> 1
ni.getIteration(2, 1) // return 2
ni.getIteration(3, 1) // return 1
ni.input(3) // 1 -> 3 Value changes may not be continuous
ni.getIteration(1, 3) // return 2
ni.getIteration(2, 3) // return 2
ni.getIteration(1, 2) // return 3
ni.getIteration(2, 1) // return 2
Requirements
Ensure memory usage as much as possible(the recording range shall not exceed the value change range)
As in the above example, you can store the following records in the input method:
class NumIteration {
records = [
{ value: 1, ... },
{ value: 2, ... },
{ value: 3, ... },
]
input(num) {
// this.records....
}
}
精彩评论