Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
GreyZeng
algorithm
提交
9b1ac5fd
A
algorithm
项目概览
GreyZeng
/
algorithm
通知
10
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9b1ac5fd
编写于
2月 08, 2024
作者:
GreyZeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update heap
上级
266965f0
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
61 addition
and
2 deletion
+61
-2
src/main/java/git/snippet/heap/DistanceLessK.java
src/main/java/git/snippet/heap/DistanceLessK.java
+4
-2
src/test/java/git/snippet/common/Generator.java
src/test/java/git/snippet/common/Generator.java
+28
-0
src/test/java/git/snippet/heap/DistanceLessKTest.java
src/test/java/git/snippet/heap/DistanceLessKTest.java
+29
-0
未找到文件。
src/main/java/git/snippet/heap/
Code_
DistanceLessK.java
→
src/main/java/git/snippet/heap/DistanceLessK.java
浏览文件 @
9b1ac5fd
...
...
@@ -7,7 +7,7 @@ import java.util.PriorityQueue;
// 几乎有序是指,如果把数组排好顺序的话,每个元素移动的距离一定不超过k,并且k相对于数组长度来说是比较小的。
// 请选择一个合适的排序策略,对这个数组进行排序。(从小到大)
// tips: 加k个数进堆,然后再加入一个,弹出一个,最后堆里面剩下的继续弹出即可 时间复杂度:O(N*logK)
public
class
Code_
DistanceLessK
{
public
class
DistanceLessK
{
public
static
void
sortedArrDistanceLessK
(
int
[]
arr
,
int
k
)
{
k
=
Math
.
min
(
arr
.
length
-
1
,
k
);
PriorityQueue
<
Integer
>
heap
=
new
PriorityQueue
<>();
...
...
@@ -20,8 +20,10 @@ public class Code_DistanceLessK {
for
(;
i
<
arr
.
length
;
i
++)
{
heap
.
offer
(
arr
[
i
]);
// 移动一定不会超过K次
if
(!
heap
.
isEmpty
())
{
arr
[
index
++]
=
heap
.
poll
();
}
}
while
(!
heap
.
isEmpty
())
{
arr
[
index
++]
=
heap
.
poll
();
}
...
...
src/test/java/git/snippet/common/Generator.java
浏览文件 @
9b1ac5fd
package
git.snippet.common
;
import
java.util.Arrays
;
public
class
Generator
{
// for test
// 几乎有序的数组排序测试
public
static
int
[]
randomArrayNoMoveMoreK
(
int
maxSize
,
int
maxValue
,
int
K
)
{
int
[]
arr
=
new
int
[(
int
)
((
maxSize
+
1
)
*
Math
.
random
())];
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++)
{
arr
[
i
]
=
(
int
)
((
maxValue
+
1
)
*
Math
.
random
())
-
(
int
)
(
maxValue
*
Math
.
random
());
}
// 先排个序
Arrays
.
sort
(
arr
);
// 然后开始随意交换,但是保证每个数距离不超过K
// swap[i] == true, 表示i位置已经参与过交换
// swap[i] == false, 表示i位置没有参与过交换
boolean
[]
isSwap
=
new
boolean
[
arr
.
length
];
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++)
{
int
j
=
Math
.
min
(
i
+
(
int
)
(
Math
.
random
()
*
(
K
+
1
)),
arr
.
length
-
1
);
if
(!
isSwap
[
i
]
&&
!
isSwap
[
j
])
{
isSwap
[
i
]
=
true
;
isSwap
[
j
]
=
true
;
int
tmp
=
arr
[
i
];
arr
[
i
]
=
arr
[
j
];
arr
[
j
]
=
tmp
;
}
}
return
arr
;
}
// for test
public
static
int
[]
generateRandomArray
(
int
maxSize
,
int
maxValue
)
{
// Math.random() -> [0,1)
...
...
src/test/java/git/snippet/heap/
Code_
DistanceLessKTest.java
→
src/test/java/git/snippet/heap/DistanceLessKTest.java
浏览文件 @
9b1ac5fd
...
...
@@ -7,10 +7,8 @@ import org.junit.jupiter.api.Test;
import
java.util.Arrays
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
@DisplayName
(
"几乎有序的数组排序测试"
)
public
class
Code_
DistanceLessKTest
{
public
class
DistanceLessKTest
{
@Test
@DisplayName
(
"几乎有序的数组排序测试"
)
...
...
@@ -20,39 +18,12 @@ public class Code_DistanceLessKTest {
int
maxValue
=
100
;
for
(
int
i
=
0
;
i
<
testTime
;
i
++)
{
int
k
=
(
int
)
(
Math
.
random
()
*
maxSize
)
+
1
;
int
[]
arr
=
randomArrayNoMoveMoreK
(
maxSize
,
maxValue
,
k
);
int
[]
arr
=
Generator
.
randomArrayNoMoveMoreK
(
maxSize
,
maxValue
,
k
);
int
[]
arr1
=
Generator
.
copyArray
(
arr
);
int
[]
arr2
=
Generator
.
copyArray
(
arr
);
Code_
DistanceLessK
.
sortedArrDistanceLessK
(
arr1
,
k
);
DistanceLessK
.
sortedArrDistanceLessK
(
arr1
,
k
);
Arrays
.
sort
(
arr2
);
Assertions
.
assertArrayEquals
(
arr1
,
arr2
);
}
}
// for test
public
static
int
[]
randomArrayNoMoveMoreK
(
int
maxSize
,
int
maxValue
,
int
K
)
{
int
[]
arr
=
new
int
[(
int
)
((
maxSize
+
1
)
*
Math
.
random
())];
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++)
{
arr
[
i
]
=
(
int
)
((
maxValue
+
1
)
*
Math
.
random
())
-
(
int
)
(
maxValue
*
Math
.
random
());
}
// 先排个序
Arrays
.
sort
(
arr
);
// 然后开始随意交换,但是保证每个数距离不超过K
// swap[i] == true, 表示i位置已经参与过交换
// swap[i] == false, 表示i位置没有参与过交换
boolean
[]
isSwap
=
new
boolean
[
arr
.
length
];
for
(
int
i
=
0
;
i
<
arr
.
length
;
i
++)
{
int
j
=
Math
.
min
(
i
+
(
int
)
(
Math
.
random
()
*
(
K
+
1
)),
arr
.
length
-
1
);
if
(!
isSwap
[
i
]
&&
!
isSwap
[
j
])
{
isSwap
[
i
]
=
true
;
isSwap
[
j
]
=
true
;
int
tmp
=
arr
[
i
];
arr
[
i
]
=
arr
[
j
];
arr
[
j
]
=
tmp
;
}
}
return
arr
;
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录