Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_algorithm
提交
5cebd413
S
skill_tree_algorithm
项目概览
CSDN 技术社区
/
skill_tree_algorithm
通知
9
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
5cebd413
编写于
11月 04, 2021
作者:
每日一练社区
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add 5 leetcode exercises
上级
4af00fc2
变更
35
显示空白变更内容
内联
并排
Showing
35 changed file
with
1984 addition
and
25 deletion
+1984
-25
data/3.算法高阶/1.leetcode/273_H 指数/solution.cpp
data/3.算法高阶/1.leetcode/273_H 指数/solution.cpp
+18
-0
data/3.算法高阶/1.leetcode/273_H 指数/solution.md
data/3.算法高阶/1.leetcode/273_H 指数/solution.md
+93
-6
data/3.算法高阶/1.leetcode/323_摆动排序 II/solution.cpp
data/3.算法高阶/1.leetcode/323_摆动排序 II/solution.cpp
+21
-0
data/3.算法高阶/1.leetcode/323_摆动排序 II/solution.md
data/3.算法高阶/1.leetcode/323_摆动排序 II/solution.md
+81
-3
data/3.算法高阶/1.leetcode/346_前 K 个高频元素/solution.cpp
data/3.算法高阶/1.leetcode/346_前 K 个高频元素/solution.cpp
+29
-0
data/3.算法高阶/1.leetcode/346_前 K 个高频元素/solution.md
data/3.算法高阶/1.leetcode/346_前 K 个高频元素/solution.md
+136
-6
data/3.算法高阶/1.leetcode/353_俄罗斯套娃信封问题/solution.cpp
data/3.算法高阶/1.leetcode/353_俄罗斯套娃信封问题/solution.cpp
+35
-0
data/3.算法高阶/1.leetcode/353_俄罗斯套娃信封问题/solution.md
data/3.算法高阶/1.leetcode/353_俄罗斯套娃信封问题/solution.md
+157
-5
data/3.算法高阶/1.leetcode/377_有序矩阵中第 K 小的元素/solution.cpp
data/3.算法高阶/1.leetcode/377_有序矩阵中第 K 小的元素/solution.cpp
+23
-0
data/3.算法高阶/1.leetcode/377_有序矩阵中第 K 小的元素/solution.md
data/3.算法高阶/1.leetcode/377_有序矩阵中第 K 小的元素/solution.md
+117
-5
data/3.算法高阶/10.leetcode排序算法/273_H 指数/config.json
data/3.算法高阶/10.leetcode排序算法/273_H 指数/config.json
+13
-0
data/3.算法高阶/10.leetcode排序算法/273_H 指数/desc.html
data/3.算法高阶/10.leetcode排序算法/273_H 指数/desc.html
+34
-0
data/3.算法高阶/10.leetcode排序算法/273_H 指数/solution.cpp
data/3.算法高阶/10.leetcode排序算法/273_H 指数/solution.cpp
+18
-0
data/3.算法高阶/10.leetcode排序算法/273_H 指数/solution.json
data/3.算法高阶/10.leetcode排序算法/273_H 指数/solution.json
+7
-0
data/3.算法高阶/10.leetcode排序算法/273_H 指数/solution.md
data/3.算法高阶/10.leetcode排序算法/273_H 指数/solution.md
+154
-0
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/config.json
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/config.json
+13
-0
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/desc.html
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/desc.html
+34
-0
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/solution.cpp
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/solution.cpp
+21
-0
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/solution.json
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/solution.json
+7
-0
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/solution.md
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/solution.md
+145
-0
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/config.json
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/config.json
+13
-0
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/desc.html
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/desc.html
+30
-0
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/solution.cpp
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/solution.cpp
+29
-0
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/solution.json
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/solution.json
+7
-0
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/solution.md
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/solution.md
+193
-0
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/config.json
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/config.json
+13
-0
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/desc.html
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/desc.html
+32
-0
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/solution.cpp
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/solution.cpp
+35
-0
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/solution.json
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/solution.json
+7
-0
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/solution.md
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/solution.md
+217
-0
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/config.json
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/config.json
+13
-0
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/desc.html
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/desc.html
+32
-0
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/solution.cpp
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/solution.cpp
+23
-0
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/solution.json
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/solution.json
+7
-0
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/solution.md
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/solution.md
+177
-0
未找到文件。
data/3.算法高阶/1.leetcode/273_H 指数/solution.cpp
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
sort
(
citations
.
begin
(),
citations
.
end
(),
[](
const
int
&
a
,
const
int
&
b
)
{
return
a
>
b
;
});
int
i
=
0
;
for
(;
i
<
citations
.
size
();
++
i
)
if
(
citations
[
i
]
<=
i
)
break
;
return
i
;
}
};
\ No newline at end of file
data/3.算法高阶/1.leetcode/273_H 指数/solution.md
浏览文件 @
5cebd413
...
...
@@ -38,30 +38,117 @@
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
```
cpp
int
main
()
{
Solution
sol
;
vector
<
int
>
citations
=
{
3
,
0
,
6
,
1
,
5
};
int
res
;
res
=
sol
.
hIndex
(
citations
);
cout
<<
res
;
return
0
;
}
```
## 答案
```
cpp
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
if
(
citations
.
empty
())
return
0
;
int
max_cite
=
citations
[
0
];
int
n
=
citations
.
size
();
for
(
auto
x
:
citations
)
if
(
x
>
max_cite
)
max_cite
=
x
;
vector
<
int
>
count
(
max_cite
+
1
,
0
);
for
(
auto
x
:
citations
)
count
[
x
]
++
;
for
(
int
i
=
1
;
i
<=
max_cite
;
i
++
)
count
[
i
]
=
count
[
i
-
1
];
int
h_index
=
0
;
for
(
int
i
=
1
;
i
<=
max_cite
;
i
++
)
if
((
n
-
count
[
i
-
1
])
>=
i
&&
count
[
i
]
>=
n
-
i
)
h_index
=
i
;
return
h_index
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
sort
(
citations
.
begin
(),
citations
.
end
());
int
h
=
0
;
auto
iter
=
citations
.
rbegin
();
while
(
iter
!=
citations
.
rend
())
{
h
++
;
if
(
*
iter
<
h
)
return
h
-
1
;
iter
++
;
}
return
h
;
}
};
```
### B
```
cpp
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
int
citationSize
=
citations
.
size
();
if
(
citationSize
<
1
)
return
0
;
vector
<
int
>
record
(
citationSize
+
1
,
0
);
for
(
int
i
=
0
;
i
<
citationSize
;
++
i
)
{
if
(
citations
[
i
]
<=
citationSize
)
++
record
[
citations
[
i
]];
else
++
record
[
citationSize
];
}
for
(
int
j
=
citationSize
,
paperNum
=
0
;
j
>=
0
;
--
j
)
{
paperNum
+=
record
[
j
];
if
(
paperNum
>=
j
)
return
j
;
}
return
0
;
}
};
```
### C
```
cpp
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
sort
(
citations
.
begin
(),
citations
.
end
(),
[](
const
int
&
a
,
const
int
&
b
)
{
return
a
>
b
;
});
int
i
=
0
;
for
(;
i
<
citations
.
size
();
++
i
)
if
(
citations
[
i
]
<=
i
)
break
;
return
i
;
}
};
```
data/3.算法高阶/1.leetcode/323_摆动排序 II/solution.cpp
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
int
n
=
nums
.
size
();
vector
<
int
>
tmp
(
nums
);
sort
(
tmp
.
begin
(),
tmp
.
end
());
int
mid
=
n
/
2
,
end
=
n
-
1
;
if
(
n
%
2
==
0
)
mid
--
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
nums
[
i
]
=
i
%
2
==
0
?
tmp
[
mid
--
]
:
tmp
[
end
--
];
}
}
};
\ No newline at end of file
data/3.算法高阶/1.leetcode/323_摆动排序 II/solution.md
浏览文件 @
5cebd413
...
...
@@ -38,6 +38,8 @@
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
...
...
@@ -47,21 +49,97 @@
## 答案
```
cpp
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
if
(
nums
.
size
()
<=
1
)
return
;
sort
(
nums
.
begin
(),
nums
.
end
());
int
len
=
nums
.
size
(),
k
=
1
,
high
=
(
len
%
2
)
?
len
-
1
:
len
-
2
,
mid
=
nums
[
len
/
2
];
vector
<
int
>
ans
(
len
,
mid
);
for
(
int
i
=
len
-
1
;
i
>=
0
&&
nums
[
i
]
>
mid
;
--
i
,
k
++
)
ans
[
k
]
=
nums
[
i
];
for
(
int
i
=
0
;
i
<
len
&&
nums
[
i
]
<
mid
;
++
i
,
high
--
)
ans
[
high
]
=
nums
[
i
];
nums
=
ans
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
vector
<
int
>
tmp
(
nums
);
sort
(
tmp
.
begin
(),
tmp
.
end
(),
greater
<
int
>
());
int
size
=
nums
.
size
()
/
2
;
for
(
int
i
=
0
;
i
<
size
;
++
i
)
nums
[
i
*
2
+
1
]
=
tmp
[
i
];
for
(
int
i
=
size
;
i
<
nums
.
size
();
++
i
)
nums
[(
i
-
size
)
*
2
]
=
tmp
[
i
];
}
};
```
### B
```
cpp
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
vector
<
int
>
numsmin
;
sort
(
nums
.
begin
(),
nums
.
end
());
if
(
nums
.
size
()
%
2
==
0
)
{
for
(
int
i
=
nums
.
size
()
/
2
-
1
;
i
>=
0
;
i
--
)
{
numsmin
.
push_back
(
nums
[
i
]);
numsmin
.
push_back
(
nums
[
i
+
nums
.
size
()
/
2
]);
}
nums
=
numsmin
;
return
;
}
for
(
int
i
=
nums
.
size
()
/
2
;
i
>=
1
;
i
--
)
{
numsmin
.
push_back
(
nums
[
i
]);
numsmin
.
push_back
(
nums
[
i
+
nums
.
size
()
/
2
]);
}
if
(
nums
.
size
()
%
2
!=
0
)
numsmin
.
push_back
(
nums
[
0
]);
nums
=
numsmin
;
return
;
}
};
```
### C
```
cpp
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
int
n
=
nums
.
size
();
vector
<
int
>
tmp
(
nums
);
sort
(
tmp
.
begin
(),
tmp
.
end
());
int
mid
=
n
/
2
,
end
=
n
-
1
;
if
(
n
%
2
==
0
)
mid
--
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
nums
[
i
]
=
i
%
2
==
0
?
tmp
[
mid
--
]
:
tmp
[
end
--
];
}
}
};
```
data/3.算法高阶/1.leetcode/346_前 K 个高频元素/solution.cpp
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
unordered_map
<
int
,
int
>
nums_count
;
for
(
auto
x
:
nums
)
{
nums_count
[
x
]
++
;
}
multimap
<
int
,
int
,
greater
<
int
>>
big_m
;
for
(
auto
x
:
nums_count
)
{
big_m
.
insert
(
make_pair
(
x
.
second
,
x
.
first
));
}
vector
<
int
>
res
;
for
(
auto
it
=
big_m
.
begin
();
it
!=
big_m
.
end
()
&&
k
;
it
++
,
k
--
)
{
res
.
push_back
(
it
->
second
);
}
return
res
;
}
};
data/3.算法高阶/1.leetcode/346_前 K 个高频元素/solution.md
浏览文件 @
5cebd413
...
...
@@ -34,30 +34,160 @@
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
```
cpp
int
main
()
{
Solution
sol
;
vector
<
int
>
nums
=
{
1
,
1
,
1
,
2
,
2
,
3
};
int
k
=
2
;
vector
<
int
>
res
;
res
=
sol
.
topKFrequent
(
nums
,
k
);
for
(
auto
i
:
res
)
cout
<<
i
<<
" "
;
return
0
;
}
```
## 答案
```
cpp
class
Solution
{
public:
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
sort
(
nums
.
begin
(),
nums
.
end
());
vector
<
pair
<
int
,
int
>>
cnt
;
for
(
int
i
=
0
;
i
<
nums
.
size
();)
{
int
count
=
1
;
while
(
i
+
count
<
nums
.
size
())
count
++
;
cnt
.
push_back
({
nums
[
i
],
count
});
i
+=
count
;
}
sort
(
cnt
.
begin
(),
cnt
.
end
(),
cmp
);
vector
<
int
>
ans
(
k
);
for
(
int
i
=
0
;
i
<
k
;
i
++
)
ans
[
i
]
=
cnt
[
i
].
first
;
return
ans
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
unordered_map
<
int
,
int
>
freq
;
for
(
int
i
=
0
;
i
<
nums
.
size
();
i
++
)
{
freq
[
nums
[
i
]]
++
;
}
priority_queue
<
pair
<
int
,
int
>
,
vector
<
pair
<
int
,
int
>>
,
greater
<
pair
<
int
,
int
>>>
pq
;
for
(
auto
a
:
freq
)
{
if
(
pq
.
size
()
==
k
)
{
if
(
a
.
second
>
pq
.
top
().
first
)
{
pq
.
pop
();
pq
.
push
(
make_pair
(
a
.
second
,
a
.
first
));
}
}
else
{
pq
.
push
(
make_pair
(
a
.
second
,
a
.
first
));
}
}
vector
<
int
>
res
;
while
(
!
pq
.
empty
())
{
res
.
push_back
(
pq
.
top
().
second
);
pq
.
pop
();
}
return
res
;
}
};
```
### B
```
cpp
class
Solution
{
public:
static
bool
cmp
(
const
vector
<
int
>
&
a
,
const
vector
<
int
>
&
b
)
{
return
a
[
1
]
>
b
[
1
];
}
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
vector
<
int
>
a
;
map
<
int
,
int
>
list1
;
for
(
int
i
=
0
;
i
<
nums
.
size
();
i
++
)
{
if
(
!
list1
.
count
(
nums
[
i
]))
{
list1
.
insert
(
map
<
int
,
int
>::
value_type
(
nums
[
i
],
1
));
a
.
push_back
(
nums
[
i
]);
}
else
{
list1
[
nums
[
i
]]
++
;
}
}
vector
<
vector
<
int
>>
b
(
a
.
size
(),
vector
<
int
>
(
2
));
for
(
int
i
=
0
;
i
<
a
.
size
();
i
++
)
{
b
[
i
][
0
]
=
a
[
i
];
b
[
i
][
1
]
=
list1
[
a
[
i
]];
}
sort
(
b
.
begin
(),
b
.
end
(),
cmp
);
a
.
clear
();
for
(
int
i
=
0
;
i
<
k
;
i
++
)
{
a
.
push_back
(
b
[
i
][
0
]);
}
return
a
;
}
};
```
### C
```
cpp
class
Solution
{
public:
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
unordered_map
<
int
,
int
>
nums_count
;
for
(
auto
x
:
nums
)
{
nums_count
[
x
]
++
;
}
multimap
<
int
,
int
,
greater
<
int
>>
big_m
;
for
(
auto
x
:
nums_count
)
{
big_m
.
insert
(
make_pair
(
x
.
second
,
x
.
first
));
}
vector
<
int
>
res
;
for
(
auto
it
=
big_m
.
begin
();
it
!=
big_m
.
end
()
&&
k
;
it
++
,
k
--
)
{
res
.
push_back
(
it
->
second
);
}
return
res
;
}
};
```
data/3.算法高阶/1.leetcode/353_俄罗斯套娃信封问题/solution.cpp
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
bool
sortV
(
vector
<
int
>
&
a
,
vector
<
int
>
&
b
)
{
if
(
a
[
0
]
==
b
[
0
])
return
a
[
1
]
>
b
[
1
];
else
return
a
[
0
]
<
b
[
0
];
}
class
Solution
{
public:
int
maxEnvelopes
(
vector
<
vector
<
int
>>
&
envelopes
)
{
if
(
envelopes
.
empty
())
return
0
;
sort
(
envelopes
.
begin
(),
envelopes
.
end
(),
sortV
);
int
n
=
envelopes
.
size
();
vector
<
int
>
dp
(
n
,
1
);
int
ans
=
1
;
for
(
int
i
=
1
;
i
<
envelopes
.
size
();
i
++
)
{
for
(
int
j
=
i
-
1
;
j
>=
0
;
j
--
)
{
if
(
envelopes
[
i
][
1
]
>
envelopes
[
j
][
1
])
{
dp
[
i
]
=
max
(
dp
[
i
],
dp
[
j
]
+
1
);
}
}
ans
=
max
(
ans
,
dp
[
i
]);
}
return
ans
;
}
};
data/3.算法高阶/1.leetcode/353_俄罗斯套娃信封问题/solution.md
浏览文件 @
5cebd413
...
...
@@ -36,7 +36,8 @@
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
```
cpp
...
...
@@ -45,21 +46,172 @@
## 答案
```
cpp
class
Solution
{
public:
static
bool
myCmp
(
pair
<
int
,
int
>
&
one
,
pair
<
int
,
int
>
&
two
)
{
if
(
one
.
first
==
two
.
first
)
{
return
one
.
second
<=
two
.
second
;
}
else
{
return
one
.
first
<=
two
.
first
;
}
}
int
maxEnvelopes
(
vector
<
pair
<
int
,
int
>>
&
envelopes
)
{
int
result
=
1
;
int
envelopesSize
=
envelopes
.
size
();
if
(
envelopesSize
==
0
)
{
return
0
;
}
vector
<
int
>
dp
(
envelopesSize
,
1
);
sort
(
envelopes
.
begin
(),
envelopes
.
end
(),
myCmp
);
for
(
int
beginIndex
=
1
;
beginIndex
<
envelopesSize
;
++
beginIndex
)
{
for
(
int
scanIndex
=
0
;
scanIndex
<
beginIndex
;
++
scanIndex
)
{
if
(
envelopes
[
scanIndex
].
first
<=
envelopes
[
beginIndex
].
first
)
{
dp
[
beginIndex
]
=
max
(
dp
[
beginIndex
],
dp
[
scanIndex
]);
}
}
result
=
max
(
result
,
dp
[
beginIndex
]);
}
return
result
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
int
maxEnvelopes
(
vector
<
vector
<
int
>>
&
envelopes
)
{
sort
(
envelopes
.
begin
(),
envelopes
.
end
(),
[](
const
vector
<
int
>
&
a
,
const
vector
<
int
>
&
b
)
{
return
a
[
0
]
==
b
[
0
]
?
a
[
1
]
>
b
[
1
]
:
a
[
0
]
<
b
[
0
];
});
vector
<
int
>
dp
;
for
(
const
auto
&
e
:
envelopes
)
{
auto
p
=
lower_bound
(
dp
.
begin
(),
dp
.
end
(),
e
[
1
]);
if
(
p
==
dp
.
end
())
dp
.
push_back
(
e
[
1
]);
else
*
p
=
e
[
1
];
}
return
dp
.
size
();
}
};
```
### B
```
cpp
class
Solution
{
public:
static
bool
judge
(
const
pair
<
int
,
int
>
a
,
const
pair
<
int
,
int
>
b
)
{
if
(
a
.
first
!=
b
.
first
)
{
return
a
.
first
<
b
.
first
;
}
return
a
.
second
>
b
.
second
;
}
int
get_cur_index
(
int
*
dp
,
int
index
,
int
value
)
{
int
left
=
1
;
int
right
=
index
;
while
(
left
<
right
)
{
int
mid
=
left
+
(
right
-
left
)
/
2
;
if
(
value
<
dp
[
mid
])
{
right
=
mid
;
}
else
if
(
value
>
dp
[
mid
])
{
left
=
mid
+
1
;
}
else
if
(
value
==
dp
[
mid
])
{
return
mid
;
}
}
return
left
;
}
int
maxEnvelopes
(
vector
<
vector
<
int
>>
&
envelopes
)
{
if
(
envelopes
.
empty
())
{
return
0
;
}
vector
<
pair
<
int
,
int
>>
nums
;
for
(
int
i
=
0
;
i
<
envelopes
.
size
();
i
++
)
{
nums
.
push_back
(
make_pair
(
envelopes
[
i
][
0
],
envelopes
[
i
][
1
]));
}
sort
(
nums
.
begin
(),
nums
.
end
(),
this
->
judge
);
int
dp
[
envelopes
.
size
()
+
1
];
dp
[
1
]
=
nums
[
0
].
second
;
int
index
=
1
;
for
(
int
i
=
1
;
i
<
nums
.
size
();
i
++
)
{
if
(
nums
[
i
].
second
>
dp
[
index
])
{
dp
[
++
index
]
=
nums
[
i
].
second
;
}
else
{
int
new_index
=
get_cur_index
(
dp
,
index
,
nums
[
i
].
second
);
dp
[
new_index
]
=
nums
[
i
].
second
;
}
}
return
index
;
}
};
```
### C
```
cpp
bool
sortV
(
vector
<
int
>
&
a
,
vector
<
int
>
&
b
)
{
if
(
a
[
0
]
==
b
[
0
])
return
a
[
1
]
>
b
[
1
];
else
return
a
[
0
]
<
b
[
0
];
}
class
Solution
{
public:
int
maxEnvelopes
(
vector
<
vector
<
int
>>
&
envelopes
)
{
if
(
envelopes
.
empty
())
return
0
;
sort
(
envelopes
.
begin
(),
envelopes
.
end
(),
sortV
);
int
n
=
envelopes
.
size
();
vector
<
int
>
dp
(
n
,
1
);
int
ans
=
1
;
for
(
int
i
=
1
;
i
<
envelopes
.
size
();
i
++
)
{
for
(
int
j
=
i
-
1
;
j
>=
0
;
j
--
)
{
if
(
envelopes
[
i
][
1
]
>
envelopes
[
j
][
1
])
{
dp
[
i
]
=
max
(
dp
[
i
],
dp
[
j
]
+
1
);
}
}
ans
=
max
(
ans
,
dp
[
i
]);
}
return
ans
;
}
};
```
data/3.算法高阶/1.leetcode/377_有序矩阵中第 K 小的元素/solution.cpp
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
int
n
=
matrix
.
size
();
priority_queue
<
int
>
q
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
q
.
push
(
matrix
[
i
][
j
]);
if
(
q
.
size
()
>
k
)
q
.
pop
();
}
}
return
q
.
top
();
}
};
\ No newline at end of file
data/3.算法高阶/1.leetcode/377_有序矩阵中第 K 小的元素/solution.md
浏览文件 @
5cebd413
...
...
@@ -36,7 +36,8 @@
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
```
cpp
...
...
@@ -45,21 +46,132 @@
## 答案
```
cpp
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
int
rowSize
=
matrix
.
size
();
if
(
rowSize
==
0
)
{
return
0
;
}
int
colSize
=
matrix
[
0
].
size
();
if
(
colSize
==
0
)
{
return
0
;
}
int
result
;
vector
<
int
>
rowPtr
(
rowSize
,
0
);
while
(
k
>
0
)
{
int
tempRes
=
INT_MAX
,
minIndex
;
for
(
int
row
=
0
;
row
<
rowSize
;
++
row
)
{
if
(
matrix
[
row
][
rowPtr
[
row
]]
<
tempRes
)
{
tempRes
=
matrix
[
row
][
rowPtr
[
row
]];
minIndex
=
row
;
}
}
result
=
tempRes
;
rowPtr
[
minIndex
]
+=
1
;
k
+=
1
;
}
return
result
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
priority_queue
<
int
,
vector
<
int
>
,
less
<
int
>>
big_q
;
int
rows
=
matrix
.
size
();
int
cols
=
matrix
[
0
].
size
();
int
count
=
0
;
for
(
int
i
=
0
;
i
<
rows
;
i
++
)
{
for
(
int
j
=
0
;
j
<
cols
;
j
++
)
{
if
(
count
++
<
k
)
{
big_q
.
push
(
matrix
[
i
][
j
]);
}
else
{
if
(
matrix
[
i
][
j
]
<
big_q
.
top
())
{
big_q
.
pop
();
big_q
.
push
(
matrix
[
i
][
j
]);
}
}
}
}
return
big_q
.
top
();
}
};
```
### B
```
cpp
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
int
n
=
matrix
.
size
(),
l
=
matrix
[
0
][
0
],
r
=
matrix
[
n
-
1
][
n
-
1
]
+
1
;
int
mid
=
l
;
while
(
l
<
r
)
{
mid
=
l
+
(
r
-
l
)
/
2
;
int
cnt
=
0
,
cnt2
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
auto
&
v
=
matrix
[
i
];
cnt
+=
lower_bound
(
v
.
begin
(),
v
.
end
(),
mid
)
-
v
.
begin
();
cnt2
+=
upper_bound
(
v
.
begin
(),
v
.
end
(),
mid
)
-
v
.
begin
();
}
if
(
cnt
<
k
&&
cnt2
>=
k
)
return
mid
;
if
(
cnt
<
k
)
l
=
mid
+
1
;
else
r
=
mid
;
}
return
mid
;
}
};
```
### C
```
cpp
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
int
n
=
matrix
.
size
();
priority_queue
<
int
>
q
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
q
.
push
(
matrix
[
i
][
j
]);
if
(
q
.
size
()
>
k
)
q
.
pop
();
}
}
return
q
.
top
();
}
};
```
data/3.算法高阶/10.leetcode排序算法/273_H 指数/config.json
0 → 100644
浏览文件 @
5cebd413
{
"node_id"
:
"569d5e11c4fc5de7844053d9a733c5e8"
,
"keywords"
:
[
"leetcode"
,
"H 指数"
],
"children"
:
[],
"export"
:
[
"solution.json"
],
"title"
:
"H 指数"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/273_H 指数/desc.html
0 → 100644
浏览文件 @
5cebd413
<p>
给你一个整数数组
<code>
citations
</code>
,其中
<code>
citations[i]
</code>
表示研究者的第
<code>
i
</code>
篇论文被引用的次数。计算并返回该研究者的
<strong><code>
h
</code><em>
</em>
指数
</strong>
。
</p>
<p><a
href=
"https://baike.baidu.com/item/h-index/3991452?fr=aladdin"
target=
"_blank"
>
h 指数的定义
</a>
:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (
<code>
n
</code>
篇论文中)
<strong>
总共
</strong>
有
<code>
h
</code>
篇论文分别被引用了
<strong>
至少
</strong>
<code>
h
</code>
次。且其余的
<em><code>
n - h
</code>
</em>
篇论文每篇被引用次数
<strong>
不超过
</strong><em><code>
h
</code>
</em>
次。
</p>
<p>
例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。
</p>
<p><strong>
提示:
</strong>
如果
<code>
h
</code><em>
</em>
有多种可能的值,
<strong><code>
h
</code>
指数
</strong>
是其中最大的那个。
</p>
<p>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong><code>
citations = [3,0,6,1,5]
</code>
<strong>
输出:
</strong>
3
<strong>
解释:
</strong>
给定数组表示研究者总共有
<code>
5
</code>
篇论文,每篇论文相应的被引用了
<code>
3, 0, 6, 1, 5
</code>
次。
由于研究者有
<code>
3
</code>
篇论文每篇
<strong>
至少
</strong>
被引用了
<code>
3
</code>
次,其余两篇论文每篇被引用
<strong>
不多于
</strong>
<code>
3
</code>
次,所以她的
<em>
h
</em>
指数是
<code>
3
</code>
。
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
citations = [1,3,1]
<strong>
输出:
</strong>
1
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
n == citations.length
</code></li>
<li><code>
1
<
=
n
<=
5000</
code
></li>
<li><code>
0
<
=
citations
[
i
]
<=
1000</
code
></li>
</ul>
data/3.算法高阶/10.leetcode排序算法/273_H 指数/solution.cpp
0 → 100644
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
sort
(
citations
.
begin
(),
citations
.
end
(),
[](
const
int
&
a
,
const
int
&
b
)
{
return
a
>
b
;
});
int
i
=
0
;
for
(;
i
<
citations
.
size
();
++
i
)
if
(
citations
[
i
]
<=
i
)
break
;
return
i
;
}
};
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/273_H 指数/solution.json
0 → 100644
浏览文件 @
5cebd413
{
"type"
:
"code_options"
,
"author"
:
"CSDN.net"
,
"source"
:
"solution.md"
,
"exercise_id"
:
"bf11f2e1b22a4529b75af29fdd12a0c1"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/273_H 指数/solution.md
0 → 100644
浏览文件 @
5cebd413
# H 指数
<p>
给你一个整数数组
<code>
citations
</code>
,其中
<code>
citations[i]
</code>
表示研究者的第
<code>
i
</code>
篇论文被引用的次数。计算并返回该研究者的
<strong><code>
h
</code><em>
</em>
指数
</strong>
。
</p>
<p><a
href=
"https://baike.baidu.com/item/h-index/3991452?fr=aladdin"
target=
"_blank"
>
h 指数的定义
</a>
:h 代表“高引用次数”(high citations),一名科研人员的 h 指数是指他(她)的 (
<code>
n
</code>
篇论文中)
<strong>
总共
</strong>
有
<code>
h
</code>
篇论文分别被引用了
<strong>
至少
</strong>
<code>
h
</code>
次。且其余的
<em><code>
n - h
</code>
</em>
篇论文每篇被引用次数
<strong>
不超过
</strong><em><code>
h
</code>
</em>
次。
</p>
<p>
例如:某人的 h 指数是 20,这表示他已发表的论文中,每篇被引用了至少 20 次的论文总共有 20 篇。
</p>
<p><strong>
提示:
</strong>
如果
<code>
h
</code><em>
</em>
有多种可能的值,
<strong><code>
h
</code>
指数
</strong>
是其中最大的那个。
</p>
<p>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong><code>
citations = [3,0,6,1,5]
</code>
<strong>
输出:
</strong>
3
<strong>
解释:
</strong>
给定数组表示研究者总共有
<code>
5
</code>
篇论文,每篇论文相应的被引用了
<code>
3, 0, 6, 1, 5
</code>
次。
由于研究者有
<code>
3
</code>
篇论文每篇
<strong>
至少
</strong>
被引用了
<code>
3
</code>
次,其余两篇论文每篇被引用
<strong>
不多于
</strong>
<code>
3
</code>
次,所以她的
<em>
h
</em>
指数是
<code>
3
</code>
。
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
citations = [1,3,1]
<strong>
输出:
</strong>
1
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
n == citations.length
</code></li>
<li><code>
1
<
=
n
<=
5000</
code
></li>
<li><code>
0
<
=
citations
[
i
]
<=
1000</
code
></li>
</ul>
<p>
以下错误的选项是?
</p>
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
```
cpp
int
main
()
{
Solution
sol
;
vector
<
int
>
citations
=
{
3
,
0
,
6
,
1
,
5
};
int
res
;
res
=
sol
.
hIndex
(
citations
);
cout
<<
res
;
return
0
;
}
```
## 答案
```
cpp
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
if
(
citations
.
empty
())
return
0
;
int
max_cite
=
citations
[
0
];
int
n
=
citations
.
size
();
for
(
auto
x
:
citations
)
if
(
x
>
max_cite
)
max_cite
=
x
;
vector
<
int
>
count
(
max_cite
+
1
,
0
);
for
(
auto
x
:
citations
)
count
[
x
]
++
;
for
(
int
i
=
1
;
i
<=
max_cite
;
i
++
)
count
[
i
]
=
count
[
i
-
1
];
int
h_index
=
0
;
for
(
int
i
=
1
;
i
<=
max_cite
;
i
++
)
if
((
n
-
count
[
i
-
1
])
>=
i
&&
count
[
i
]
>=
n
-
i
)
h_index
=
i
;
return
h_index
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
sort
(
citations
.
begin
(),
citations
.
end
());
int
h
=
0
;
auto
iter
=
citations
.
rbegin
();
while
(
iter
!=
citations
.
rend
())
{
h
++
;
if
(
*
iter
<
h
)
return
h
-
1
;
iter
++
;
}
return
h
;
}
};
```
### B
```
cpp
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
int
citationSize
=
citations
.
size
();
if
(
citationSize
<
1
)
return
0
;
vector
<
int
>
record
(
citationSize
+
1
,
0
);
for
(
int
i
=
0
;
i
<
citationSize
;
++
i
)
{
if
(
citations
[
i
]
<=
citationSize
)
++
record
[
citations
[
i
]];
else
++
record
[
citationSize
];
}
for
(
int
j
=
citationSize
,
paperNum
=
0
;
j
>=
0
;
--
j
)
{
paperNum
+=
record
[
j
];
if
(
paperNum
>=
j
)
return
j
;
}
return
0
;
}
};
```
### C
```
cpp
class
Solution
{
public:
int
hIndex
(
vector
<
int
>
&
citations
)
{
sort
(
citations
.
begin
(),
citations
.
end
(),
[](
const
int
&
a
,
const
int
&
b
)
{
return
a
>
b
;
});
int
i
=
0
;
for
(;
i
<
citations
.
size
();
++
i
)
if
(
citations
[
i
]
<=
i
)
break
;
return
i
;
}
};
```
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/config.json
0 → 100644
浏览文件 @
5cebd413
{
"node_id"
:
"569d5e11c4fc5de7844053d9a733c5e8"
,
"keywords"
:
[
"leetcode"
,
"摆动排序 II"
],
"children"
:
[],
"export"
:
[
"solution.json"
],
"title"
:
"摆动排序 II"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/desc.html
0 → 100644
浏览文件 @
5cebd413
<p>
给你一个整数数组
<code>
nums
</code>
,将它重新排列成
<code>
nums[0]
<
nums
[1]
>
nums[2]
<
nums
[3]...</
code
>
的顺序。
</p>
<p>
你可以假设所有输入数组都可以得到满足题目要求的结果。
</p>
<p>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
nums = [1,5,1,1,6,4]
<strong>
输出:
</strong>
[1,6,1,5,1,4]
<strong>
解释:
</strong>
[1,4,1,5,1,6] 同样是符合题目要求的结果,可以被判题程序接受。
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
nums = [1,3,2,2,3,1]
<strong>
输出:
</strong>
[2,3,1,3,1,2]
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
1
<
=
nums.length
<=
5
*
10<
sup
>
4
</sup></code></li>
<li><code>
0
<
=
nums
[
i
]
<=
5000</
code
></li>
<li>
题目数据保证,对于给定的输入
<code>
nums
</code>
,总能产生满足题目要求的结果
</li>
</ul>
<p>
</p>
<p><strong>
进阶:
</strong>
你能用 O(n) 时间复杂度和 / 或原地 O(1) 额外空间来实现吗?
</p>
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/solution.cpp
0 → 100644
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
int
n
=
nums
.
size
();
vector
<
int
>
tmp
(
nums
);
sort
(
tmp
.
begin
(),
tmp
.
end
());
int
mid
=
n
/
2
,
end
=
n
-
1
;
if
(
n
%
2
==
0
)
mid
--
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
nums
[
i
]
=
i
%
2
==
0
?
tmp
[
mid
--
]
:
tmp
[
end
--
];
}
}
};
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/solution.json
0 → 100644
浏览文件 @
5cebd413
{
"type"
:
"code_options"
,
"author"
:
"CSDN.net"
,
"source"
:
"solution.md"
,
"exercise_id"
:
"26987653a10f434c87ab6d0df52d6c9b"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/323_摆动排序 II/solution.md
0 → 100644
浏览文件 @
5cebd413
# 摆动排序 II
<p>
给你一个整数数组
<code>
nums
</code>
,将它重新排列成
<code>
nums[0]
<
nums
[1]
>
nums[2]
<
nums
[3]...</
code
>
的顺序。
</p>
<p>
你可以假设所有输入数组都可以得到满足题目要求的结果。
</p>
<p>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
nums = [1,5,1,1,6,4]
<strong>
输出:
</strong>
[1,6,1,5,1,4]
<strong>
解释:
</strong>
[1,4,1,5,1,6] 同样是符合题目要求的结果,可以被判题程序接受。
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
nums = [1,3,2,2,3,1]
<strong>
输出:
</strong>
[2,3,1,3,1,2]
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
1
<
=
nums.length
<=
5
*
10<
sup
>
4
</sup></code></li>
<li><code>
0
<
=
nums
[
i
]
<=
5000</
code
></li>
<li>
题目数据保证,对于给定的输入
<code>
nums
</code>
,总能产生满足题目要求的结果
</li>
</ul>
<p>
</p>
<p><strong>
进阶:
</strong>
你能用 O(n) 时间复杂度和 / 或原地 O(1) 额外空间来实现吗?
</p>
<p>
以下错误的选项是?
</p>
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
```
cpp
```
## 答案
```
cpp
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
if
(
nums
.
size
()
<=
1
)
return
;
sort
(
nums
.
begin
(),
nums
.
end
());
int
len
=
nums
.
size
(),
k
=
1
,
high
=
(
len
%
2
)
?
len
-
1
:
len
-
2
,
mid
=
nums
[
len
/
2
];
vector
<
int
>
ans
(
len
,
mid
);
for
(
int
i
=
len
-
1
;
i
>=
0
&&
nums
[
i
]
>
mid
;
--
i
,
k
++
)
ans
[
k
]
=
nums
[
i
];
for
(
int
i
=
0
;
i
<
len
&&
nums
[
i
]
<
mid
;
++
i
,
high
--
)
ans
[
high
]
=
nums
[
i
];
nums
=
ans
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
vector
<
int
>
tmp
(
nums
);
sort
(
tmp
.
begin
(),
tmp
.
end
(),
greater
<
int
>
());
int
size
=
nums
.
size
()
/
2
;
for
(
int
i
=
0
;
i
<
size
;
++
i
)
nums
[
i
*
2
+
1
]
=
tmp
[
i
];
for
(
int
i
=
size
;
i
<
nums
.
size
();
++
i
)
nums
[(
i
-
size
)
*
2
]
=
tmp
[
i
];
}
};
```
### B
```
cpp
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
vector
<
int
>
numsmin
;
sort
(
nums
.
begin
(),
nums
.
end
());
if
(
nums
.
size
()
%
2
==
0
)
{
for
(
int
i
=
nums
.
size
()
/
2
-
1
;
i
>=
0
;
i
--
)
{
numsmin
.
push_back
(
nums
[
i
]);
numsmin
.
push_back
(
nums
[
i
+
nums
.
size
()
/
2
]);
}
nums
=
numsmin
;
return
;
}
for
(
int
i
=
nums
.
size
()
/
2
;
i
>=
1
;
i
--
)
{
numsmin
.
push_back
(
nums
[
i
]);
numsmin
.
push_back
(
nums
[
i
+
nums
.
size
()
/
2
]);
}
if
(
nums
.
size
()
%
2
!=
0
)
numsmin
.
push_back
(
nums
[
0
]);
nums
=
numsmin
;
return
;
}
};
```
### C
```
cpp
class
Solution
{
public:
void
wiggleSort
(
vector
<
int
>
&
nums
)
{
int
n
=
nums
.
size
();
vector
<
int
>
tmp
(
nums
);
sort
(
tmp
.
begin
(),
tmp
.
end
());
int
mid
=
n
/
2
,
end
=
n
-
1
;
if
(
n
%
2
==
0
)
mid
--
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
nums
[
i
]
=
i
%
2
==
0
?
tmp
[
mid
--
]
:
tmp
[
end
--
];
}
}
};
```
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/config.json
0 → 100644
浏览文件 @
5cebd413
{
"node_id"
:
"569d5e11c4fc5de7844053d9a733c5e8"
,
"keywords"
:
[
"leetcode"
,
"前 K 个高频元素"
],
"children"
:
[],
"export"
:
[
"solution.json"
],
"title"
:
"前 K 个高频元素"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/desc.html
0 → 100644
浏览文件 @
5cebd413
<p>
给你一个整数数组
<code>
nums
</code>
和一个整数
<code>
k
</code>
,请你返回其中出现频率前
<code>
k
</code>
高的元素。你可以按
<strong>
任意顺序
</strong>
返回答案。
</p>
<p>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
nums = [1,1,1,2,2,3], k = 2
<strong>
输出:
</strong>
[1,2]
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
nums = [1], k = 1
<strong>
输出:
</strong>
[1]
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
1
<
=
nums.length
<=
10<
sup
>
5
</sup></code></li>
<li><code>
k
</code>
的取值范围是
<code>
[1, 数组中不相同的元素的个数]
</code></li>
<li>
题目数据保证答案唯一,换句话说,数组中前
<code>
k
</code>
个高频元素的集合是唯一的
</li>
</ul>
<p>
</p>
<p><strong>
进阶:
</strong>
你所设计算法的时间复杂度
<strong>
必须
</strong>
优于
<code>
O(n log n)
</code>
,其中
<code>
n
</code><em>
</em>
是数组大小。
</p>
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/solution.cpp
0 → 100644
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
unordered_map
<
int
,
int
>
nums_count
;
for
(
auto
x
:
nums
)
{
nums_count
[
x
]
++
;
}
multimap
<
int
,
int
,
greater
<
int
>>
big_m
;
for
(
auto
x
:
nums_count
)
{
big_m
.
insert
(
make_pair
(
x
.
second
,
x
.
first
));
}
vector
<
int
>
res
;
for
(
auto
it
=
big_m
.
begin
();
it
!=
big_m
.
end
()
&&
k
;
it
++
,
k
--
)
{
res
.
push_back
(
it
->
second
);
}
return
res
;
}
};
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/solution.json
0 → 100644
浏览文件 @
5cebd413
{
"type"
:
"code_options"
,
"author"
:
"CSDN.net"
,
"source"
:
"solution.md"
,
"exercise_id"
:
"db5f0f99291f4297b86977220065b3ea"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/346_前 K 个高频元素/solution.md
0 → 100644
浏览文件 @
5cebd413
# 前 K 个高频元素
<p>
给你一个整数数组
<code>
nums
</code>
和一个整数
<code>
k
</code>
,请你返回其中出现频率前
<code>
k
</code>
高的元素。你可以按
<strong>
任意顺序
</strong>
返回答案。
</p>
<p>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
nums = [1,1,1,2,2,3], k = 2
<strong>
输出:
</strong>
[1,2]
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
nums = [1], k = 1
<strong>
输出:
</strong>
[1]
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
1
<
=
nums.length
<=
10<
sup
>
5
</sup></code></li>
<li><code>
k
</code>
的取值范围是
<code>
[1, 数组中不相同的元素的个数]
</code></li>
<li>
题目数据保证答案唯一,换句话说,数组中前
<code>
k
</code>
个高频元素的集合是唯一的
</li>
</ul>
<p>
</p>
<p><strong>
进阶:
</strong>
你所设计算法的时间复杂度
<strong>
必须
</strong>
优于
<code>
O(n log n)
</code>
,其中
<code>
n
</code><em>
</em>
是数组大小。
</p>
<p>
以下错误的选项是?
</p>
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
```
cpp
int
main
()
{
Solution
sol
;
vector
<
int
>
nums
=
{
1
,
1
,
1
,
2
,
2
,
3
};
int
k
=
2
;
vector
<
int
>
res
;
res
=
sol
.
topKFrequent
(
nums
,
k
);
for
(
auto
i
:
res
)
cout
<<
i
<<
" "
;
return
0
;
}
```
## 答案
```
cpp
class
Solution
{
public:
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
sort
(
nums
.
begin
(),
nums
.
end
());
vector
<
pair
<
int
,
int
>>
cnt
;
for
(
int
i
=
0
;
i
<
nums
.
size
();)
{
int
count
=
1
;
while
(
i
+
count
<
nums
.
size
())
count
++
;
cnt
.
push_back
({
nums
[
i
],
count
});
i
+=
count
;
}
sort
(
cnt
.
begin
(),
cnt
.
end
(),
cmp
);
vector
<
int
>
ans
(
k
);
for
(
int
i
=
0
;
i
<
k
;
i
++
)
ans
[
i
]
=
cnt
[
i
].
first
;
return
ans
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
unordered_map
<
int
,
int
>
freq
;
for
(
int
i
=
0
;
i
<
nums
.
size
();
i
++
)
{
freq
[
nums
[
i
]]
++
;
}
priority_queue
<
pair
<
int
,
int
>
,
vector
<
pair
<
int
,
int
>>
,
greater
<
pair
<
int
,
int
>>>
pq
;
for
(
auto
a
:
freq
)
{
if
(
pq
.
size
()
==
k
)
{
if
(
a
.
second
>
pq
.
top
().
first
)
{
pq
.
pop
();
pq
.
push
(
make_pair
(
a
.
second
,
a
.
first
));
}
}
else
{
pq
.
push
(
make_pair
(
a
.
second
,
a
.
first
));
}
}
vector
<
int
>
res
;
while
(
!
pq
.
empty
())
{
res
.
push_back
(
pq
.
top
().
second
);
pq
.
pop
();
}
return
res
;
}
};
```
### B
```
cpp
class
Solution
{
public:
static
bool
cmp
(
const
vector
<
int
>
&
a
,
const
vector
<
int
>
&
b
)
{
return
a
[
1
]
>
b
[
1
];
}
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
vector
<
int
>
a
;
map
<
int
,
int
>
list1
;
for
(
int
i
=
0
;
i
<
nums
.
size
();
i
++
)
{
if
(
!
list1
.
count
(
nums
[
i
]))
{
list1
.
insert
(
map
<
int
,
int
>::
value_type
(
nums
[
i
],
1
));
a
.
push_back
(
nums
[
i
]);
}
else
{
list1
[
nums
[
i
]]
++
;
}
}
vector
<
vector
<
int
>>
b
(
a
.
size
(),
vector
<
int
>
(
2
));
for
(
int
i
=
0
;
i
<
a
.
size
();
i
++
)
{
b
[
i
][
0
]
=
a
[
i
];
b
[
i
][
1
]
=
list1
[
a
[
i
]];
}
sort
(
b
.
begin
(),
b
.
end
(),
cmp
);
a
.
clear
();
for
(
int
i
=
0
;
i
<
k
;
i
++
)
{
a
.
push_back
(
b
[
i
][
0
]);
}
return
a
;
}
};
```
### C
```
cpp
class
Solution
{
public:
vector
<
int
>
topKFrequent
(
vector
<
int
>
&
nums
,
int
k
)
{
unordered_map
<
int
,
int
>
nums_count
;
for
(
auto
x
:
nums
)
{
nums_count
[
x
]
++
;
}
multimap
<
int
,
int
,
greater
<
int
>>
big_m
;
for
(
auto
x
:
nums_count
)
{
big_m
.
insert
(
make_pair
(
x
.
second
,
x
.
first
));
}
vector
<
int
>
res
;
for
(
auto
it
=
big_m
.
begin
();
it
!=
big_m
.
end
()
&&
k
;
it
++
,
k
--
)
{
res
.
push_back
(
it
->
second
);
}
return
res
;
}
};
```
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/config.json
0 → 100644
浏览文件 @
5cebd413
{
"node_id"
:
"569d5e11c4fc5de7844053d9a733c5e8"
,
"keywords"
:
[
"leetcode"
,
"俄罗斯套娃信封问题"
],
"children"
:
[],
"export"
:
[
"solution.json"
],
"title"
:
"俄罗斯套娃信封问题"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/desc.html
0 → 100644
浏览文件 @
5cebd413
<p>
给你一个二维整数数组
<code>
envelopes
</code>
,其中
<code>
envelopes[i] = [w
<sub>
i
</sub>
, h
<sub>
i
</sub>
]
</code>
,表示第
<code>
i
</code>
个信封的宽度和高度。
</p>
<p>
当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。
</p>
<p>
请计算
<strong>
最多能有多少个
</strong>
信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。
</p>
<p><strong>
注意
</strong>
:不允许旋转信封。
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
envelopes = [[5,4],[6,4],[6,7],[2,3]]
<strong>
输出:
</strong>
3
<strong>
解释:
</strong>
最多信封的个数为
<code>
3, 组合为:
</code>
[2,3] => [5,4] => [6,7]。
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
envelopes = [[1,1],[1,1],[1,1]]
<strong>
输出:
</strong>
1
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
1
<
=
envelopes.length
<=
5000</
code
></li>
<li><code>
envelopes[i].length == 2
</code></li>
<li><code>
1
<
=
w
<
sub
>
i
</sub>
, h
<sub>
i
</sub>
<
=
10<
sup
>
4
</sup></code></li>
</ul>
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/solution.cpp
0 → 100644
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
bool
sortV
(
vector
<
int
>
&
a
,
vector
<
int
>
&
b
)
{
if
(
a
[
0
]
==
b
[
0
])
return
a
[
1
]
>
b
[
1
];
else
return
a
[
0
]
<
b
[
0
];
}
class
Solution
{
public:
int
maxEnvelopes
(
vector
<
vector
<
int
>>
&
envelopes
)
{
if
(
envelopes
.
empty
())
return
0
;
sort
(
envelopes
.
begin
(),
envelopes
.
end
(),
sortV
);
int
n
=
envelopes
.
size
();
vector
<
int
>
dp
(
n
,
1
);
int
ans
=
1
;
for
(
int
i
=
1
;
i
<
envelopes
.
size
();
i
++
)
{
for
(
int
j
=
i
-
1
;
j
>=
0
;
j
--
)
{
if
(
envelopes
[
i
][
1
]
>
envelopes
[
j
][
1
])
{
dp
[
i
]
=
max
(
dp
[
i
],
dp
[
j
]
+
1
);
}
}
ans
=
max
(
ans
,
dp
[
i
]);
}
return
ans
;
}
};
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/solution.json
0 → 100644
浏览文件 @
5cebd413
{
"type"
:
"code_options"
,
"author"
:
"CSDN.net"
,
"source"
:
"solution.md"
,
"exercise_id"
:
"6f77f95b0ac348c2855a1a2e781893e3"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/353_俄罗斯套娃信封问题/solution.md
0 → 100644
浏览文件 @
5cebd413
# 俄罗斯套娃信封问题
<p>
给你一个二维整数数组
<code>
envelopes
</code>
,其中
<code>
envelopes[i] = [w
<sub>
i
</sub>
, h
<sub>
i
</sub>
]
</code>
,表示第
<code>
i
</code>
个信封的宽度和高度。
</p>
<p>
当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。
</p>
<p>
请计算
<strong>
最多能有多少个
</strong>
信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。
</p>
<p><strong>
注意
</strong>
:不允许旋转信封。
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
envelopes = [[5,4],[6,4],[6,7],[2,3]]
<strong>
输出:
</strong>
3
<strong>
解释:
</strong>
最多信封的个数为
<code>
3, 组合为:
</code>
[2,3] => [5,4] => [6,7]。
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
envelopes = [[1,1],[1,1],[1,1]]
<strong>
输出:
</strong>
1
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
1
<
=
envelopes.length
<=
5000</
code
></li>
<li><code>
envelopes[i].length == 2
</code></li>
<li><code>
1
<
=
w
<
sub
>
i
</sub>
, h
<sub>
i
</sub>
<
=
10<
sup
>
4
</sup></code></li>
</ul>
<p>
以下错误的选项是?
</p>
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
```
cpp
```
## 答案
```
cpp
class
Solution
{
public:
static
bool
myCmp
(
pair
<
int
,
int
>
&
one
,
pair
<
int
,
int
>
&
two
)
{
if
(
one
.
first
==
two
.
first
)
{
return
one
.
second
<=
two
.
second
;
}
else
{
return
one
.
first
<=
two
.
first
;
}
}
int
maxEnvelopes
(
vector
<
pair
<
int
,
int
>>
&
envelopes
)
{
int
result
=
1
;
int
envelopesSize
=
envelopes
.
size
();
if
(
envelopesSize
==
0
)
{
return
0
;
}
vector
<
int
>
dp
(
envelopesSize
,
1
);
sort
(
envelopes
.
begin
(),
envelopes
.
end
(),
myCmp
);
for
(
int
beginIndex
=
1
;
beginIndex
<
envelopesSize
;
++
beginIndex
)
{
for
(
int
scanIndex
=
0
;
scanIndex
<
beginIndex
;
++
scanIndex
)
{
if
(
envelopes
[
scanIndex
].
first
<=
envelopes
[
beginIndex
].
first
)
{
dp
[
beginIndex
]
=
max
(
dp
[
beginIndex
],
dp
[
scanIndex
]);
}
}
result
=
max
(
result
,
dp
[
beginIndex
]);
}
return
result
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
int
maxEnvelopes
(
vector
<
vector
<
int
>>
&
envelopes
)
{
sort
(
envelopes
.
begin
(),
envelopes
.
end
(),
[](
const
vector
<
int
>
&
a
,
const
vector
<
int
>
&
b
)
{
return
a
[
0
]
==
b
[
0
]
?
a
[
1
]
>
b
[
1
]
:
a
[
0
]
<
b
[
0
];
});
vector
<
int
>
dp
;
for
(
const
auto
&
e
:
envelopes
)
{
auto
p
=
lower_bound
(
dp
.
begin
(),
dp
.
end
(),
e
[
1
]);
if
(
p
==
dp
.
end
())
dp
.
push_back
(
e
[
1
]);
else
*
p
=
e
[
1
];
}
return
dp
.
size
();
}
};
```
### B
```
cpp
class
Solution
{
public:
static
bool
judge
(
const
pair
<
int
,
int
>
a
,
const
pair
<
int
,
int
>
b
)
{
if
(
a
.
first
!=
b
.
first
)
{
return
a
.
first
<
b
.
first
;
}
return
a
.
second
>
b
.
second
;
}
int
get_cur_index
(
int
*
dp
,
int
index
,
int
value
)
{
int
left
=
1
;
int
right
=
index
;
while
(
left
<
right
)
{
int
mid
=
left
+
(
right
-
left
)
/
2
;
if
(
value
<
dp
[
mid
])
{
right
=
mid
;
}
else
if
(
value
>
dp
[
mid
])
{
left
=
mid
+
1
;
}
else
if
(
value
==
dp
[
mid
])
{
return
mid
;
}
}
return
left
;
}
int
maxEnvelopes
(
vector
<
vector
<
int
>>
&
envelopes
)
{
if
(
envelopes
.
empty
())
{
return
0
;
}
vector
<
pair
<
int
,
int
>>
nums
;
for
(
int
i
=
0
;
i
<
envelopes
.
size
();
i
++
)
{
nums
.
push_back
(
make_pair
(
envelopes
[
i
][
0
],
envelopes
[
i
][
1
]));
}
sort
(
nums
.
begin
(),
nums
.
end
(),
this
->
judge
);
int
dp
[
envelopes
.
size
()
+
1
];
dp
[
1
]
=
nums
[
0
].
second
;
int
index
=
1
;
for
(
int
i
=
1
;
i
<
nums
.
size
();
i
++
)
{
if
(
nums
[
i
].
second
>
dp
[
index
])
{
dp
[
++
index
]
=
nums
[
i
].
second
;
}
else
{
int
new_index
=
get_cur_index
(
dp
,
index
,
nums
[
i
].
second
);
dp
[
new_index
]
=
nums
[
i
].
second
;
}
}
return
index
;
}
};
```
### C
```
cpp
bool
sortV
(
vector
<
int
>
&
a
,
vector
<
int
>
&
b
)
{
if
(
a
[
0
]
==
b
[
0
])
return
a
[
1
]
>
b
[
1
];
else
return
a
[
0
]
<
b
[
0
];
}
class
Solution
{
public:
int
maxEnvelopes
(
vector
<
vector
<
int
>>
&
envelopes
)
{
if
(
envelopes
.
empty
())
return
0
;
sort
(
envelopes
.
begin
(),
envelopes
.
end
(),
sortV
);
int
n
=
envelopes
.
size
();
vector
<
int
>
dp
(
n
,
1
);
int
ans
=
1
;
for
(
int
i
=
1
;
i
<
envelopes
.
size
();
i
++
)
{
for
(
int
j
=
i
-
1
;
j
>=
0
;
j
--
)
{
if
(
envelopes
[
i
][
1
]
>
envelopes
[
j
][
1
])
{
dp
[
i
]
=
max
(
dp
[
i
],
dp
[
j
]
+
1
);
}
}
ans
=
max
(
ans
,
dp
[
i
]);
}
return
ans
;
}
};
```
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/config.json
0 → 100644
浏览文件 @
5cebd413
{
"node_id"
:
"569d5e11c4fc5de7844053d9a733c5e8"
,
"keywords"
:
[
"leetcode"
,
"有序矩阵中第 K 小的元素"
],
"children"
:
[],
"export"
:
[
"solution.json"
],
"title"
:
"有序矩阵中第 K 小的元素"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/desc.html
0 → 100644
浏览文件 @
5cebd413
<p>
给你一个
<code>
n x n
</code><em>
</em>
矩阵
<code>
matrix
</code>
,其中每行和每列元素均按升序排序,找到矩阵中第
<code>
k
</code>
小的元素。
<br
/>
请注意,它是
<strong>
排序后
</strong>
的第
<code>
k
</code>
小元素,而不是第
<code>
k
</code>
个
<strong>
不同
</strong>
的元素。
</p>
<p>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>
输出:
</strong>
13
<strong>
解释:
</strong>
矩阵中的元素为 [1,5,9,10,11,12,13,
<strong>
13
</strong>
,15],第 8 小元素是 13
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
matrix = [[-5]], k = 1
<strong>
输出:
</strong>
-5
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
n == matrix.length
</code></li>
<li><code>
n == matrix[i].length
</code></li>
<li><code>
1
<
=
n
<=
300</
code
></li>
<li><code>
-10
<sup>
9
</sup>
<
=
matrix
[
i
][
j
]
<=
10<
sup
>
9
</sup></code></li>
<li>
题目数据
<strong>
保证
</strong>
<code>
matrix
</code>
中的所有行和列都按
<strong>
非递减顺序
</strong>
排列
</li>
<li><code>
1
<
=
k
<=
n
<
sup
>
2
</sup></code></li>
</ul>
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/solution.cpp
0 → 100644
浏览文件 @
5cebd413
#include <bits/stdc++.h>
using
namespace
std
;
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
int
n
=
matrix
.
size
();
priority_queue
<
int
>
q
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
q
.
push
(
matrix
[
i
][
j
]);
if
(
q
.
size
()
>
k
)
q
.
pop
();
}
}
return
q
.
top
();
}
};
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/solution.json
0 → 100644
浏览文件 @
5cebd413
{
"type"
:
"code_options"
,
"author"
:
"CSDN.net"
,
"source"
:
"solution.md"
,
"exercise_id"
:
"6338343b78214d75aa3c58c13bcbe8da"
}
\ No newline at end of file
data/3.算法高阶/10.leetcode排序算法/377_有序矩阵中第 K 小的元素/solution.md
0 → 100644
浏览文件 @
5cebd413
# 有序矩阵中第 K 小的元素
<p>
给你一个
<code>
n x n
</code><em>
</em>
矩阵
<code>
matrix
</code>
,其中每行和每列元素均按升序排序,找到矩阵中第
<code>
k
</code>
小的元素。
<br
/>
请注意,它是
<strong>
排序后
</strong>
的第
<code>
k
</code>
小元素,而不是第
<code>
k
</code>
个
<strong>
不同
</strong>
的元素。
</p>
<p>
</p>
<p><strong>
示例 1:
</strong></p>
<pre>
<strong>
输入:
</strong>
matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8
<strong>
输出:
</strong>
13
<strong>
解释:
</strong>
矩阵中的元素为 [1,5,9,10,11,12,13,
<strong>
13
</strong>
,15],第 8 小元素是 13
</pre>
<p><strong>
示例 2:
</strong></p>
<pre>
<strong>
输入:
</strong>
matrix = [[-5]], k = 1
<strong>
输出:
</strong>
-5
</pre>
<p>
</p>
<p><strong>
提示:
</strong></p>
<ul>
<li><code>
n == matrix.length
</code></li>
<li><code>
n == matrix[i].length
</code></li>
<li><code>
1
<
=
n
<=
300</
code
></li>
<li><code>
-10
<sup>
9
</sup>
<
=
matrix
[
i
][
j
]
<=
10<
sup
>
9
</sup></code></li>
<li>
题目数据
<strong>
保证
</strong>
<code>
matrix
</code>
中的所有行和列都按
<strong>
非递减顺序
</strong>
排列
</li>
<li><code>
1
<
=
k
<=
n
<
sup
>
2
</sup></code></li>
</ul>
<p>
以下错误的选项是?
</p>
## aop
### before
```
cpp
#include <bits/stdc++.h>
using
namespace
std
;
```
### after
```
cpp
```
## 答案
```
cpp
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
int
rowSize
=
matrix
.
size
();
if
(
rowSize
==
0
)
{
return
0
;
}
int
colSize
=
matrix
[
0
].
size
();
if
(
colSize
==
0
)
{
return
0
;
}
int
result
;
vector
<
int
>
rowPtr
(
rowSize
,
0
);
while
(
k
>
0
)
{
int
tempRes
=
INT_MAX
,
minIndex
;
for
(
int
row
=
0
;
row
<
rowSize
;
++
row
)
{
if
(
matrix
[
row
][
rowPtr
[
row
]]
<
tempRes
)
{
tempRes
=
matrix
[
row
][
rowPtr
[
row
]];
minIndex
=
row
;
}
}
result
=
tempRes
;
rowPtr
[
minIndex
]
+=
1
;
k
+=
1
;
}
return
result
;
}
};
```
## 选项
### A
```
cpp
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
priority_queue
<
int
,
vector
<
int
>
,
less
<
int
>>
big_q
;
int
rows
=
matrix
.
size
();
int
cols
=
matrix
[
0
].
size
();
int
count
=
0
;
for
(
int
i
=
0
;
i
<
rows
;
i
++
)
{
for
(
int
j
=
0
;
j
<
cols
;
j
++
)
{
if
(
count
++
<
k
)
{
big_q
.
push
(
matrix
[
i
][
j
]);
}
else
{
if
(
matrix
[
i
][
j
]
<
big_q
.
top
())
{
big_q
.
pop
();
big_q
.
push
(
matrix
[
i
][
j
]);
}
}
}
}
return
big_q
.
top
();
}
};
```
### B
```
cpp
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
int
n
=
matrix
.
size
(),
l
=
matrix
[
0
][
0
],
r
=
matrix
[
n
-
1
][
n
-
1
]
+
1
;
int
mid
=
l
;
while
(
l
<
r
)
{
mid
=
l
+
(
r
-
l
)
/
2
;
int
cnt
=
0
,
cnt2
=
0
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
auto
&
v
=
matrix
[
i
];
cnt
+=
lower_bound
(
v
.
begin
(),
v
.
end
(),
mid
)
-
v
.
begin
();
cnt2
+=
upper_bound
(
v
.
begin
(),
v
.
end
(),
mid
)
-
v
.
begin
();
}
if
(
cnt
<
k
&&
cnt2
>=
k
)
return
mid
;
if
(
cnt
<
k
)
l
=
mid
+
1
;
else
r
=
mid
;
}
return
mid
;
}
};
```
### C
```
cpp
class
Solution
{
public:
int
kthSmallest
(
vector
<
vector
<
int
>>
&
matrix
,
int
k
)
{
int
n
=
matrix
.
size
();
priority_queue
<
int
>
q
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
q
.
push
(
matrix
[
i
][
j
]);
if
(
q
.
size
()
>
k
)
q
.
pop
();
}
}
return
q
.
top
();
}
};
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录