| Author |
Best Data Structure to solve this problem
|
danial leksevo
Greenhorn
Joined: Jun 25, 2011
Posts: 3
|
|
Hi All,
Could you please suggest me which data structure is best to use to solve this problem
I have set of int ranges in the form of two dimensional array e.g int[][] array= {{1- 4},{6-10}{8-20},{20- 30},{50-60}};
I want to combining ranges wherever possible to make a range which cover all of sub-set of the ranges e.g {{6-10}{8-20},{20- 30}}={6-30}
and my final set of ranges will be like array= {{1- 4},{6-30},{50-60}}
Thanks in advance!
|
 |
Marty Groban
Greenhorn
Joined: Jun 25, 2011
Posts: 4
|
|
|
I would try to use a doubly linked list.
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
You could, 1) add each individual value to a Set. This would
eliminate any duplicates. Then 2) sort the set, and 3) scan
the set to formulate the new ranges.
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
danial leksevo
Greenhorn
Joined: Jun 25, 2011
Posts: 3
|
|
Thanks Jim,
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Yes, you're on the right track. If the Set is sorted on Integer, you can
just look for a missing value to find the end of a range. Give it a try.
Jim ... ...
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
|
Maybe you should create a Range class. You can then write a utility method
|
 |
 |
|
|
subject: Best Data Structure to solve this problem
|
|
|