How to optimize a method written in Java using any of the Algorithm or Data Structure technique? [duplicate]
up vote
-1
down vote
favorite
This question already has an answer here:
Given a set of rectangles, do any overlap?
3 answers
find overlapping rectangles algorithm
12 answers
I have a list of rectangles sorted by priority.
While we iterate this List, I will insert a rectangle element into a Set. This set includes discrete rectangles. It means that all rectangles in Set should not overlap each other.
The below code has O(n^2) time complexity. But there seems to be a better way.
public Collection<Box> removeOverlapping(Collection<Box> boxList) {
Collection<Box> boxListRemovedOverlapping = new LinkedList<>();
for (Box box : boxList) {
boolean overlapping = false;
for (Box compBox : boxListRemovedOverlapping) {
if (isBoxOverlapped(box, compBox)) {
overlapping = true;
break;
}
}
if (!overlapping) {
boxListRemovedOverlapping.add(box);
}
}
return boxListRemovedOverlapping;
}
java algorithm data-structures collections
marked as duplicate by Andreas
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 5:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
up vote
-1
down vote
favorite
This question already has an answer here:
Given a set of rectangles, do any overlap?
3 answers
find overlapping rectangles algorithm
12 answers
I have a list of rectangles sorted by priority.
While we iterate this List, I will insert a rectangle element into a Set. This set includes discrete rectangles. It means that all rectangles in Set should not overlap each other.
The below code has O(n^2) time complexity. But there seems to be a better way.
public Collection<Box> removeOverlapping(Collection<Box> boxList) {
Collection<Box> boxListRemovedOverlapping = new LinkedList<>();
for (Box box : boxList) {
boolean overlapping = false;
for (Box compBox : boxListRemovedOverlapping) {
if (isBoxOverlapped(box, compBox)) {
overlapping = true;
break;
}
}
if (!overlapping) {
boxListRemovedOverlapping.add(box);
}
}
return boxListRemovedOverlapping;
}
java algorithm data-structures collections
marked as duplicate by Andreas
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 5:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
"But there seems to be a better way" What way would that be, and if so, why didn't you use it?
– Andreas
Nov 13 at 5:26
What is condition of overlapped box?
– htpvl
Nov 13 at 5:31
@Andreas I am looking for it.
– Bongousse
Nov 13 at 5:32
@htpvl The overlapped means one box includes the other or intersects with it.
– Bongousse
Nov 13 at 5:33
Consider some BSP data structure like R-tree
– MBo
Nov 13 at 5:33
|
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This question already has an answer here:
Given a set of rectangles, do any overlap?
3 answers
find overlapping rectangles algorithm
12 answers
I have a list of rectangles sorted by priority.
While we iterate this List, I will insert a rectangle element into a Set. This set includes discrete rectangles. It means that all rectangles in Set should not overlap each other.
The below code has O(n^2) time complexity. But there seems to be a better way.
public Collection<Box> removeOverlapping(Collection<Box> boxList) {
Collection<Box> boxListRemovedOverlapping = new LinkedList<>();
for (Box box : boxList) {
boolean overlapping = false;
for (Box compBox : boxListRemovedOverlapping) {
if (isBoxOverlapped(box, compBox)) {
overlapping = true;
break;
}
}
if (!overlapping) {
boxListRemovedOverlapping.add(box);
}
}
return boxListRemovedOverlapping;
}
java algorithm data-structures collections
This question already has an answer here:
Given a set of rectangles, do any overlap?
3 answers
find overlapping rectangles algorithm
12 answers
I have a list of rectangles sorted by priority.
While we iterate this List, I will insert a rectangle element into a Set. This set includes discrete rectangles. It means that all rectangles in Set should not overlap each other.
The below code has O(n^2) time complexity. But there seems to be a better way.
public Collection<Box> removeOverlapping(Collection<Box> boxList) {
Collection<Box> boxListRemovedOverlapping = new LinkedList<>();
for (Box box : boxList) {
boolean overlapping = false;
for (Box compBox : boxListRemovedOverlapping) {
if (isBoxOverlapped(box, compBox)) {
overlapping = true;
break;
}
}
if (!overlapping) {
boxListRemovedOverlapping.add(box);
}
}
return boxListRemovedOverlapping;
}
This question already has an answer here:
Given a set of rectangles, do any overlap?
3 answers
find overlapping rectangles algorithm
12 answers
java algorithm data-structures collections
java algorithm data-structures collections
edited Nov 13 at 6:46
Abhinav
296111
296111
asked Nov 13 at 5:24
Bongousse
214
214
marked as duplicate by Andreas
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 5:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Andreas
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 at 5:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
"But there seems to be a better way" What way would that be, and if so, why didn't you use it?
– Andreas
Nov 13 at 5:26
What is condition of overlapped box?
– htpvl
Nov 13 at 5:31
@Andreas I am looking for it.
– Bongousse
Nov 13 at 5:32
@htpvl The overlapped means one box includes the other or intersects with it.
– Bongousse
Nov 13 at 5:33
Consider some BSP data structure like R-tree
– MBo
Nov 13 at 5:33
|
show 1 more comment
"But there seems to be a better way" What way would that be, and if so, why didn't you use it?
– Andreas
Nov 13 at 5:26
What is condition of overlapped box?
– htpvl
Nov 13 at 5:31
@Andreas I am looking for it.
– Bongousse
Nov 13 at 5:32
@htpvl The overlapped means one box includes the other or intersects with it.
– Bongousse
Nov 13 at 5:33
Consider some BSP data structure like R-tree
– MBo
Nov 13 at 5:33
"But there seems to be a better way" What way would that be, and if so, why didn't you use it?
– Andreas
Nov 13 at 5:26
"But there seems to be a better way" What way would that be, and if so, why didn't you use it?
– Andreas
Nov 13 at 5:26
What is condition of overlapped box?
– htpvl
Nov 13 at 5:31
What is condition of overlapped box?
– htpvl
Nov 13 at 5:31
@Andreas I am looking for it.
– Bongousse
Nov 13 at 5:32
@Andreas I am looking for it.
– Bongousse
Nov 13 at 5:32
@htpvl The overlapped means one box includes the other or intersects with it.
– Bongousse
Nov 13 at 5:33
@htpvl The overlapped means one box includes the other or intersects with it.
– Bongousse
Nov 13 at 5:33
Consider some BSP data structure like R-tree
– MBo
Nov 13 at 5:33
Consider some BSP data structure like R-tree
– MBo
Nov 13 at 5:33
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
"But there seems to be a better way" What way would that be, and if so, why didn't you use it?
– Andreas
Nov 13 at 5:26
What is condition of overlapped box?
– htpvl
Nov 13 at 5:31
@Andreas I am looking for it.
– Bongousse
Nov 13 at 5:32
@htpvl The overlapped means one box includes the other or intersects with it.
– Bongousse
Nov 13 at 5:33
Consider some BSP data structure like R-tree
– MBo
Nov 13 at 5:33