-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 休息日 | ||
|
||
小红书笔试薄砂我 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 第五章 栈与队列part03 | ||
今日内容: | ||
|
||
● 239. 滑动窗口最大值 | ||
● 347.前 K 个高频元素 | ||
● 总结 | ||
|
||
详细布置 | ||
|
||
|
||
## 239. 滑动窗口最大值 (一刷至少需要理解思路) | ||
|
||
之前讲的都是栈的应用,这次该是队列的应用了。 | ||
|
||
本题算比较有难度的,需要自己去构造单调队列,建议先看视频来理解。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0239.%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E6%9C%80%E5%A4%A7%E5%80%BC.html | ||
|
||
|
||
## 347.前 K 个高频元素 (一刷至少需要理解思路) | ||
|
||
大/小顶堆的应用, 在C++中就是优先级队列 | ||
|
||
本题是 大数据中取前k值 的经典思路,了解想法之后,不算难。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0347.%E5%89%8DK%E4%B8%AA%E9%AB%98%E9%A2%91%E5%85%83%E7%B4%A0.html | ||
|
||
|
||
## 总结 | ||
|
||
栈与队列做一个总结吧,加油 | ||
|
||
https://programmercarl.com/%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97%E6%80%BB%E7%BB%93.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 239. 滑动窗口最大值 | ||
|
||
## 题目描述 | ||
|
||
给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 | ||
|
||
返回 滑动窗口中的最大值 。 | ||
|
||
|
||
## 解题思路 | ||
|
||
```rust | ||
impl Solution { | ||
pub fn max_sliding_window(v: Vec<i32>, k: i32) -> Vec<i32> { | ||
let k = k as usize; | ||
let n = v.len(); | ||
let mut res = vec![]; | ||
let mut q = std::collections::VecDeque::new(); | ||
for i in 0..n { | ||
if i >= k && v[i - k] == q[0] { | ||
q.pop_front(); | ||
} | ||
// add last | ||
while let Some(&x) = q.back() { | ||
if x < v[i] { q.pop_back(); } | ||
else { break } | ||
} | ||
q.push_back(v[i]); | ||
if i >= k - 1 { | ||
res.push(q[0]); | ||
} | ||
} | ||
res | ||
} | ||
} | ||
``` | ||
|
||
## 学习感想 | ||
|
||
一开始确实以为大顶堆就行了,其实要用单调栈,之前也有做过单调栈的题目 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 347. 前 K 个高频元素 | ||
|
||
## 题目描述 | ||
|
||
给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。 | ||
|
||
## 解题思路 | ||
|
||
|
||
```rust | ||
struct Solution {} | ||
#[derive(PartialEq, Eq)] | ||
struct Y { a: i32, b: usize, } | ||
type Z = Y; | ||
use std::cmp::Ordering; | ||
impl PartialOrd<Z>for Z{fn partial_cmp(&self,o:&Z)->Option<Ordering>{Some(self.cmp(o))}} | ||
impl Ord for Z{fn cmp(&self,o:&Z)->Ordering{o.b.cmp(&self.b)}} | ||
impl Solution { | ||
pub fn top_k_frequent(v: Vec<i32>, k: i32) -> Vec<i32> { | ||
use std::collections::HashMap; | ||
use std::collections::BinaryHeap; | ||
let mut m: HashMap<i32, usize> = HashMap::new(); | ||
let mut q: BinaryHeap<Z> = BinaryHeap::new(); | ||
for i in v { | ||
*m.entry(i).or_default() += 1; | ||
} | ||
for (a, b) in m { | ||
q.push(Z {a:a, b:b}); | ||
if q.len() > k as usize { q.pop(); } | ||
} | ||
let mut res = vec![]; | ||
for i in q { | ||
res.push(i.a); | ||
} | ||
res | ||
} | ||
} | ||
``` | ||
|
||
## 学习感想 | ||
|
||
map+小顶堆 |