I am looking for data structure / algorithm in Java that does the following -
- If there is no exact match in existence, then select the nearest (one value for B)
- If the middle is fine, choose the higher - for example - my Pass numbers are >
I only know the value of A and by applying the above rules, make a straightforward look, Appropriate would / remove.
Example - 1
If I get 80 as value, then output is 0
Example - 2
If I If value is available as 75, then output 1 [according to rule 2]
<example - 3
If I get value as 70, then output 1 [rule] 3]
Any advice?
Updates based on comments - log (n) lookup is acceptable. I am open to implementing myself but need suggestions on how to achieve it. A range ranges from 0 to 1000 with accurate digits of 1 digit.
In fact there is already a data structure that is actually looking for you, which is .
There are ways in which you allow a 'key' to get 'floor' and 'roof'. After this, a little math will give you the value you really want to return:
Public Fixed TreeMap & lt; Integer, Integer & gt; TM = new tree mark & lt; & Gt; (); Public static zero principal (string [] args) Exception {throws tm.put (80, 0); Tm.put (76, 1); Tm.put (64, 3); Tm.put (56, 4); TM.Product (48, 10); Println (myGet (80)); // System.out.println (myGet (75)); // 1 System.out.println (myGet (70)); // 1} public static int myGet (int key) {integer value = tm.get (key); If (value == zero) {entry
Note: I did not bother with the appropriate null
when there is no floor / roof, but you get this idea.
No comments:
Post a Comment