删除一个ArrayList相交值ArrayList

2023-09-11 05:01:30 作者:酷到被通缉

我有一类这样的

 公共类Func键{
    字符串ID;
    长的startTime;
    长endTime的;
}
 

和在我的这些类对象的列表的方法之一

 名单,其中,Func键> funList =新的ArrayList< Func键>();
 

最新最好的办法从这个funList的startTime和endTime的相交在同一列表中的其他对象中删除的对象。

我能想到的一种方法是采取每个对象列表中的,并与其它的目的进行比较,并把该ID在其它数据结构中,如果它相交。之后的每一个对象进行比较等,经过数据结构,其中ID是保存和从列表中删除它们。

解决方案 在排序的startTime下降,endTime的acsending。 扫描阵列由左到右夹持元件与目前最大右端/ 如果新项目的左端小于当前的最大权则既标记为删除。 右键如果需要更新的最大值。

复杂度:为O(n LN N)

常用API String ArrayList

修改例如

(1,3)(2,4)(5,6) curmax = -inf curmax = 3 2'; 3 - 标记的第一和第二的坏。 curmax = 4 5> 4 - 什么都不做。 curmax = 6。 (5,6) - 是唯一的好段

I have a class like this

    public class Func {
    String id;
    long startTime;
    long endTime;
}

and in one of the methods I have list of these class objects

List<Func> funList = new ArrayList<Func>();

Whats the best way to remove the objects from this funList whose startTime and endTime intersect with another object in the same list.

One way I can think of is to take each object in the list and compare with other objects and put the id in an other data structure if it intersects. After every object is compared against other, go through the datastructure where ID's are save and remove them from the List.

解决方案

Sort by startTime descending, endTime acsending. Scan array from left to right holding element with currently max right end/

If new item's left end is less than current max right then mark both for deletion. Update maximum right if needed.

Complexity: O(n ln n)

EDIT example:

(1, 3) (2, 4) (5, 6) curmax = -inf curmax = 3 2 < 3 - mark first and second as "bad". curmax = 4 5 > 4 - do nothing. curmax = 6. (5,6) - is the only good segment.