代码随想录算法训练营第13天 | 239. 滑动窗口最大值 | 347. 前 K 个高频元素

本文主要是介绍代码随想录算法训练营第13天 | 239. 滑动窗口最大值 | 347. 前 K 个高频元素,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

239. 滑动窗口最大值

题目链接

题意

给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。返回 滑动窗口中的最大值 。示例 1:输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置                最大值
---------------               -----
[1  3  -1] -3  5  3  6  7       31 [3  -1  -3] 5  3  6  7       31  3 [-1  -3  5] 3  6  7       51  3  -1 [-3  5  3] 6  7       51  3  -1  -3 [5  3  6] 7       61  3  -1  -3  5 [3  6  7]      7
示例 2:输入:nums = [1], k = 1
输出:[1]提示:1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length

解1

用栈实现单调栈, 由于需要拷贝, 超时了

/*** Note: The returned array must be malloced, assume caller calls free().*/struct stack{int top;int array[100005];
};int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {struct stack *st = (struct stack *)malloc(sizeof(struct stack));int *ans = (int *)malloc(sizeof(int) * 100005);int idx = 0;memset(ans, 0, sizeof(int) * 100005);memset(st, 0, sizeof(*st));st->top = -1;for (int i = 0; i < k; i++) {while (st->top != -1 && st->array[st->top] < nums[i]) {st->top--;} st->array[++st->top] = nums[i];}ans[idx++] = st->array[0];for (int i = k; i < numsSize; i++) {while (st->top != -1 && st->array[st->top] < nums[i])  st->top--;st->array[++st->top] = nums[i];if (st->array[0] == nums[i-k]) {int j = 0;while (j < st->top) {st->array[j] = st->array[j+1];j++;}st->top--;}ans[idx++] = st->array[0];}*returnSize = idx;return ans;
}

解2: 队列实现单调栈

/*** Note: The returned array must be malloced, assume caller calls free().*/const int max = 1e5+10;
struct queue{int front, back;int array[100005];
};int empty(struct queue *queue) {if (queue->front == queue->back) {return 1;}return 0;
}void push(struct queue *que, int n) {while (!empty(que) && que->array[que->back] < n) {que->back--;}que->array[++que->back] = n;
}void pop(struct queue *que, int n) {if (que->array[que->front+1] == n) {que->front++;}
}int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {struct queue *que = (struct queue *)malloc(sizeof(struct queue));int *ans = (int *)malloc(sizeof(int) * max);int idx = 0;memset(que, 0, sizeof(*que));que->front = que->back = -1;for (int i = 0; i < k; i++) {push(que, nums[i]);}ans[idx++] = que->array[que->front+1];for (int i = k; i < numsSize; i++) {pop(que, nums[i-k]);push(que, nums[i]);ans[idx++] = que->array[que->front+1];}*returnSize = idx;return ans;
}

347. 前 K 个高频元素

题目链接

题意

给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。示例 1:输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
示例 2:输入: nums = [1], k = 1
输出: [1]提示:1 <= nums.length <= 105
k 的取值范围是 [1, 数组中不相同的元素的个数]
题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的进阶:你所设计算法的时间复杂度 必须 优于 O(n log n) ,其中 n 是数组大小。

小顶堆

class Solution {
public:struct cmp_pair {bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) {return lhs.second > rhs.second;}};vector<int> topKFrequent(vector<int>& nums, int k) {vector<int> ans(k);unordered_map<int, int> map;for (int i = 0; i < nums.size(); i++) {map[nums[i]]++;}priority_queue<pair<int, int>, vector<pair<int, int>>, cmp_pair> pri_que;for (unordered_map<int, int>::iterator it = map.begin(); it != map.end(); it++) {pri_que.push(*it);if (pri_que.size() > k) {pri_que.pop();}}for (int i = k-1; i >= 0; i--) {ans[i] = pri_que.top().first;pri_que.pop();}return ans;}
};

这篇关于代码随想录算法训练营第13天 | 239. 滑动窗口最大值 | 347. 前 K 个高频元素的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/947428

相关文章

随想录 Day 69 并查集 107. 寻找存在的路径

随想录 Day 69 并查集 107. 寻找存在的路径 理论基础 int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好vector<int> father = vector<int> (n, 0); // C++里的一种数组结构// 并查集初始化void init() {for (int i = 0; i < n; ++i) {father[i] = i;}

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

公共筛选组件(二次封装antd)支持代码提示

如果项目是基于antd组件库为基础搭建,可使用此公共筛选组件 使用到的库 npm i antdnpm i lodash-esnpm i @types/lodash-es -D /components/CommonSearch index.tsx import React from 'react';import { Button, Card, Form } from 'antd'

17.用300行代码手写初体验Spring V1.0版本

1.1.课程目标 1、了解看源码最有效的方式,先猜测后验证,不要一开始就去调试代码。 2、浓缩就是精华,用 300行最简洁的代码 提炼Spring的基本设计思想。 3、掌握Spring框架的基本脉络。 1.2.内容定位 1、 具有1年以上的SpringMVC使用经验。 2、 希望深入了解Spring源码的人群,对 Spring有一个整体的宏观感受。 3、 全程手写实现SpringM

代码随想录算法训练营:12/60

非科班学习算法day12 | LeetCode150:逆波兰表达式 ,Leetcode239: 滑动窗口最大值  目录 介绍 一、基础概念补充: 1.c++字符串转为数字 1. std::stoi, std::stol, std::stoll, std::stoul, std::stoull(最常用) 2. std::stringstream 3. std::atoi, std

记录AS混淆代码模板

开启混淆得先在build.gradle文件中把 minifyEnabled false改成true,以及shrinkResources true//去除无用的resource文件 这些是写在proguard-rules.pro文件内的 指定代码的压缩级别 -optimizationpasses 5 包明不混合大小写 -dontusemixedcaseclassnames 不去忽略非公共

人工智能机器学习算法总结神经网络算法(前向及反向传播)

1.定义,意义和优缺点 定义: 神经网络算法是一种模仿人类大脑神经元之间连接方式的机器学习算法。通过多层神经元的组合和激活函数的非线性转换,神经网络能够学习数据的特征和模式,实现对复杂数据的建模和预测。(我们可以借助人类的神经元模型来更好的帮助我们理解该算法的本质,不过这里需要说明的是,虽然名字是神经网络,并且结构等等也是借鉴了神经网络,但其原型以及算法本质上还和生物层面的神经网络运行原理存在

麻了!一觉醒来,代码全挂了。。

作为⼀名程序员,相信大家平时都有代码托管的需求。 相信有不少同学或者团队都习惯把自己的代码托管到GitHub平台上。 但是GitHub大家知道,经常在访问速度这方面并不是很快,有时候因为网络问题甚至根本连网站都打不开了,所以导致使用体验并不友好。 经常一觉醒来,居然发现我竟然看不到我自己上传的代码了。。 那在国内,除了GitHub,另外还有一个比较常用的Gitee平台也可以用于

大林 PID 算法

Dahlin PID算法是一种用于控制和调节系统的比例积分延迟算法。以下是一个简单的C语言实现示例: #include <stdio.h>// DALIN PID 结构体定义typedef struct {float SetPoint; // 设定点float Proportion; // 比例float Integral; // 积分float Derivative; // 微分flo

LeetCode--220 存在重复元素 III

题目 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。 示例 示例 1:输入: nums = [1,2,3,1], k = 3, t = 0输出: true示例 2:输入: nums = [1,0,1,1], k = 1, t = 2输出: true示例