Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 1f25fd63
authored
Jul 11, 2023
by
wangli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
算法优化
1 parent
9f74cf12
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
27 deletions
project-order/src/main/java/com/dituhui/pea/order/common/SegmentInsertion.java
project-order/src/main/java/com/dituhui/pea/order/common/SegmentInsertion.java
View file @
1f25fd6
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
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment