Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ainevsia committed Jul 24, 2023
1 parent 790b4ce commit 8ed29c4
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 1 deletion.
6 changes: 5 additions & 1 deletion notes/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@
- [Day 11](./day11.md)
- [20. 有效的括号](./day11/lc20.md)
- [1047. 删除字符串中的所有相邻重复项](./day11/lc1047.md)
- [150. 逆波兰表达式求值](./day11/lc150.md)
- [150. 逆波兰表达式求值](./day11/lc150.md)
- [Day 12](./day12.md)
- [Day 13](./day13.md)
- [239. 滑动窗口最大值](./day13/lc239.md)
- [347. 前 K 个高频元素](./day13/lc347.md)
3 changes: 3 additions & 0 deletions notes/src/day12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 休息日

小红书笔试薄砂我
33 changes: 33 additions & 0 deletions notes/src/day13.md
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
40 changes: 40 additions & 0 deletions notes/src/day13/lc239.md
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
}
}
```

## 学习感想

一开始确实以为大顶堆就行了,其实要用单调栈,之前也有做过单调栈的题目
42 changes: 42 additions & 0 deletions notes/src/day13/lc347.md
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+小顶堆

0 comments on commit 8ed29c4

Please sign in to comment.