Commit 0991c619 by 王力

Merge branch 'dev_rescheduling20230711' into 'develop'

算法优化

See merge request !200
2 parents 5bcdc36a 1f25fd63
package com.dituhui.pea.order.common;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
@Slf4j
public class SegmentInsertion {
public static int insertSegment(int segmentLength, int startRange, int endRange, List<Segment> segments) {
if (segments.isEmpty()) {
return 0;
}
int pos = startRange;
while (pos <= endRange) {
for (int index = 0; index < segments.size(); index++) {
Segment segment = segments.get(index);
if (index == segments.size() - 1) {
// 最后一个节点
if (pos > segment.getEnd()) {
return index + 1;
}
}
if (pos >= segment.getStart() && pos <= segment.getEnd()) {
// 起点在 segment 内,不符合要求
pos = segment.getEnd() + 1;
continue;
}
if (pos < segment.getStart() && pos + segmentLength > segment.getStart()) {
pos = segment.getEnd() + 1;
continue;
}
if (pos < segment.getStart() && pos + segmentLength < segment.getStart()) {
return index;
}
}
}
return -1;
}
@Data
public static class Segment {
private String sid;
......@@ -20,30 +60,4 @@ public class SegmentInsertion {
}
}
public static int insertSegment(int segmentLength, int startRange, int endRange, List<Segment> occupiedSegments) {
if (startRange < 0 || endRange > 1440) {
return -1; // 新线段范围超出了有效范围,无法插入
}
for (Segment segment : occupiedSegments) {
if (startRange < segment.getEnd() && endRange > segment.getStart()) {
return -1; // 新线段与已占用的线段重叠,无法插入
}
}
for (int i = 0; i < occupiedSegments.size(); i++) {
Segment segment = occupiedSegments.get(i);
if (i == 0 && startRange < segment.getStart() && endRange <= segment.getStart()) {
// 新线段与第一个线段相交,无法插入
return i;
} else if (i == occupiedSegments.size() - 1 && endRange > segment.getEnd() && startRange >= segment.getEnd()) {
// 新线段与最后一个线段相交,无法插入
return i;
} else if (startRange > segment.getEnd() && endRange < occupiedSegments.get(i + 1).getStart()) {
return i;
}
}
return -1; // 无法插入,所有已占用线段都与新线段相交
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!