Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Java in General
Best Data Structure to solve this problem
danial leksevo
Greenhorn
Posts: 3
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Posts: 4
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I would try to use a doubly linked list.
Jim Hoglund
Ranch Hand
Posts: 525
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Posts: 3
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks Jim,
Jim Hoglund
Ranch Hand
Posts: 525
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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 ...
...
BEE MBA PMP SCJP-6
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Maybe
you should
create a Range class. You can then write a utility method
public static Set<Range> normalize(Set<Range> ranges) { // assuming the set is sorted by lower range // for each range R(i) in the set, see if it intersects with the range R(i-1) // if it doesn't, start a new range Q with the same bounds as R(i) and add it to the result set // if it does, set the upper bound of Q to the upper bound of R(i), if it's greater. }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Best Data Structure to solve this problem
Java recursion method find largest
College Assignment Help
A Hard Puzzle
Displaying a histogram using input from an array
More...