左神带你刷题之生成窗口最大值数值

题目描述:

  有一个整型数组arr和一个大小为w的窗口从数组的最左边滑到最右边,窗口每次向右滑动一个位置。

比如 : 给定数组【4 3 5 4 3 3 6 7】

【4 3 5 】4 3 3 6 7       ----------- 窗口中最大值为5

4【 3 5 4】 3 3 6 7     ----------- 窗口中最大值为5

4 3 【5 4 3】 3 6 7     ----------- 窗口中最大值为5

4 3 5 【4 3  3】  6 7   ----------- 窗口中最大值为4

4 3 5 4 【3 3 6】 7      ----------- 窗口中最大值为6

4 3 5 4 3 【3 6 7】        ---------- 窗口中最大值为7

如果窗口长度为n,窗口大小为w,则一共产生n-w+1个窗口的最大值;

package april;

import java.util.Scanner;

public class Class_7 {
    public static void main(String[] args) {
        Scanner in  = new Scanner(System.in);
        System.out.println("输入数组元素的大小n:");
        int n = in.nextInt() ;
        System.out.println("滑窗的窗口大小w:") ;
        int w = in.nextInt();
        int [] arr = new int[n] ;
        for (int index=0 ; index<n;index++)
        {
            arr[index] = in.nextInt();
        }

        int [] result = new int[n-w+1] ;
        Class_7 class7 = new Class_7() ;
        result = class7.huachuang(arr,w,result);
        in.close();
        for(int ele :result)
        {
            System.out.print(ele+" ");
        }
    }

    public int [] huachuang(int [] arr , int w,int [] result)
    {
        int [] arrw = new int[w] ;
        for (int i=0; i<=arr.length-w; i++)
        {
            for (int j=i ;j<w+i ;j++ )
            {
                arrw[j-i] = arr[j] ;
            }
            result[i] = getMax(arrw) ;
        }

        return result ;
    }

    public int getMax(int [] arrw)
    {
        int max = Integer.MIN_VALUE ;
        for(int index = 0 ; index<arrw.length ; index++)
        {
            if (arrw[index]>max)
            {
                max = arrw[index];
            }
        }

        return max ;
    }

}

时间复杂度分析:O(n)

原文地址:https://www.cnblogs.com/rrttp/p/8719620.html

时间: 2024-08-29 07:28:30

左神带你刷题之生成窗口最大值数值的相关文章

生成窗口最大值数组

1.生成窗口最大值数组 有一个整型数组arr和一个大小为w的窗口从数组的最左边滑到最右边,窗口每次向右边滑一个位置. 例如,数组为[4,3,5,4,3,3,6,7],窗口大小为3时: [4 3 5] 4 3 3 6 7 窗口中最大值为5 4 [3 5 4] 3 3 6 7 窗口中最大值为5 4 3 [5 4 3] 3 6 7 窗口中最大值为5 4 3 5 [4 3 3] 6 7 窗口中最大值为4 4 3 5 4 [3 3 6] 7 窗口中最大值为6 4 3 5 4 3 [3 6 7] 窗口中最大

5.生成窗口最大值数组

题设:给一个整型数组arr和一个大小为w的窗口,窗口从最左边滑到最右边,窗口每次向右边滑一个位置,例如arr={4,3,5,4,3,6,7}时,w=3,此时生成的窗口最大值数组为{5,5,5,6,7}. 思路:给定一个长度为L的数组arr ,当滑动窗口大小 为w时,则生成的窗口最大值数组maxWindow长度为L-w+1,;用一个双端队列queue(可以用LinkedList来模拟)来存储滑动窗口的最大值,假设当窗口移动到位置i时,当queue为空时直接将i添加到queue的队尾,当queue不

编程7:生成窗口最大值数组

<?php header("content-type:text/html;charset=utf-8"); /* *生成窗口的最大值数组 P19 * 注意SplDoublyLinkedList的使用!!! * top指的是生成链表的尾部!!! * bottom指的是生成链表的头部!!! */ function getMaxwindow($arr,$w){ if(count($arr)<$w){ return 0; } $qMax = new SplDoublyLinkedL

[程序员代码面试指南]栈和队列-生成窗口最大值数组(双端队列)

问题描述 输入数组arr={4,3,5,4,3,3,6,7},窗口大小w=3,窗口由左向右移动,输出每个窗口最大值组成的数组. 解题思路 数据结构:使用ArrayList模拟双端队列. 遍历一遍arr,时间复杂度O(n).具体地, 队列队尾的位置对应的元素若不比当前位置对应元素大,则弹出,否则,将当前元素入队. -每次检查队首元素下标是否已超出以当前位置为结尾的窗口,超出则出队. 代码 import java.util.LinkedList; public class Main { public

8月leetcode刷题总结

刷题链接:https://leetcode-cn.com/explore/ 根据leetcode的探索栏目,八月份一直在上面进行刷题.发现算法题真的好难,真-计算机思维. 核心是将现实问题转化为计算机能够处理的计算问题.而这些计算问题都是通过逻辑分析来解决的. 因此逻辑-分析能力就是计算机思维的核心能力.比如将一个数组的增删改查,是通过逻辑中的 if   -else   while   这些基本语句构成的. 引用左神的算法刷题方法:" 第一阶段:对于某一个具体的算法,首先要搞清楚这个算法解决的问

跟着chengyulala刷题之[kuangbin带你飞]之&#39;并查集&#39;专题/斜眼笑

[kuangbin带你飞] 专题1-23 https://vjudge.net/article/187 专题五 并查集 POJ 2236 Wireless Network  http://poj.org/problem?id=2236POJ 1611 The Suspects  http://poj.org/problem?id=1611HDU 1213 How Many Tables  http://acm.hdu.edu.cn/showproblem.php?pid=1213HDU 3038

今日刷题集合

月考没考,最皮的是刷题效率低的可怕,搜索中的那些回溯用的还是很水,不如总结一下. codevs 题号:1501 1506 1842 1983 2549 2806 3143 3145 1008 1294 1295 1501 二叉树的最大宽度和高度(没加using namespace std ;会过不去编译,max.min函数封装在#include<iostream>里,所以没有using namespace std ;不行.) 1 #include<bits/stdc++.h> 2

编程日志&amp;&amp;刷题日志&amp;&amp;开发日志迁移之碎碎念

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15]

刷题记录:[De1CTF 2019]Giftbox &amp;&amp; Comment

目录 刷题记录:[De1CTF 2019]Giftbox && Comment 一.知识点 1.sql注入 && totp 2.RCE 3.源码泄露 4.敏感文件读取 刷题记录:[De1CTF 2019]Giftbox && Comment 题目复现链接:https://buuoj.cn/challenges 参考链接:De1CTF Web WriteUp BUUCTF平台 web writeup 第三弹 一.知识点 1.sql注入 && t