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 be3b0d0b
authored
Jul 09, 2023
by
wangli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
实现线段插入算法
1 parent
417c9db4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
0 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
0 → 100644
View file @
be3b0d0
package
com
.
dituhui
.
pea
.
order
.
common
;
import
lombok.Data
;
import
java.util.List
;
public
class
SegmentInsertion
{
@Data
public
static
class
Segment
{
private
int
start
;
private
int
end
;
public
Segment
(
int
start
,
int
end
)
{
this
.
start
=
start
;
this
.
end
=
end
;
}
}
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