1. 程式人生 > >LeetCode Sliding Window Maximum 滑動視窗(雙向連結串列實現佇列效果)

LeetCode Sliding Window Maximum 滑動視窗(雙向連結串列實現佇列效果)

思路:

使用雙向連結串列(LinkedList,LinkedList類是雙向列表,列表中的每個節點都包含了對前一個和後一個元素的引用)。

雙向連結串列的大小就是視窗的個數,每次向視窗中增加一個元素時,如果比視窗中最後一個大,就刪除視窗中最後一個,以此類推,來保證視窗中的元素都是正序。如果超出視窗大小就刪除視窗中第一個元素。每次視窗的變化看更新ans陣列。

時間複雜度O(N),空間複雜度O(K),其中N為陣列大小,K為視窗大小。

java code:

public class Solution {
    public int[] maxSlidingWindow(int[] nums, int
k) { if(k == 0) return new int[0]; int len = nums.length; int[] ans = new int[len - k + 1]; LinkedList<Integer> windowList = new LinkedList<Integer>(); for(int i = 0; i < len; ++i) { while(!windowList.isEmpty() && nums[i] > nums[windowList.getLast()]) { windowList.removeLast(); } windowList.addLast(i); if
(i - windowList.getFirst() + 1 > k) { windowList.removeFirst(); } if(i + 1 >= k) { ans[i + 1 - k] = nums[windowList.getFirst()]; } } return ans; } }

相關推薦

LeetCode Sliding Window Maximum 滑動視窗雙向連結串列實現佇列效果

思路: 使用雙向連結串列(LinkedList,LinkedList類是雙向列表,列表中的每個節點都包含了對前一個和後一個元素的引用)。 雙向連結串列的大小就是視窗的個數,每次向視窗中增加一個元素時,如果比視窗中最後一個大,就刪除視窗中最後一個,以此類推,來

[LeetCode] Sliding Window Maximum 滑動視窗最大值

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k number

[LeetCode] Sliding Window Median 滑動視窗中位數

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two mi

連結串列倒序問題雙向連結串列的基礎運用

#include <stdio.h> #include <stdlib.h> struct node { int key; struct node *next,*before;//結構中包含前向指標before,後向指標next };

bzoj 4548: 小奇的糖果 && bzoj 3658: Jabberwocky雙向連結串列+樹狀陣列

Time Limit: 20 Sec  Memory Limit: 1024 MBSubmit: 263  Solved: 107 [Submit][Status][Discuss] Description 平面上有n個點,每個點有k種顏色中的一個。 你可以選擇一條

[leetcode]239. Sliding Window Maximum滑動窗口最大值

code 數據結構 spa and TE 分享 div pos array Given an array nums, there is a sliding window of size k which is moving from the very left of the

leetcode 76. Minimum Window Substring 滑動視窗

I will first give the solution then show you the magic template.   The code of solving this problem is below. It might be the shortest among all sol

Leetcode 239 Silding Window Maximum(滑動視窗的最大值

一,問題描述 1,給定一個整數陣列nums 和一個正整數k的滑動視窗,滑動視窗每次從左到右移動一個數字,返回每次滑動視窗中的最大值。 2,例如: 輸入: nums=[1,3,-1,-3,5,3,6,7] k=3 輸出: 3, 3 ,5,

Leetcode|Sliding Window Maximum(multiset,優先佇列,雙端佇列和區間樹的應用)

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can

【POJ 2823】Sliding Window滑動視窗/單調佇列入門

題目大意 輸入一個長度為n(n≤≤106106)的數列,給定一個長度為k的視窗,讓這個視窗在數列上移動,求移動到每個位置視窗中包含數的最大值和最小值。即設序列為A1,A2,…,AnA1,A2,…,AnA1,A2,…,An,設f(i)=minAi−k+1Ai−k+

LeetCode:152. Maximum Product Subarray最大的乘積下標

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Examp

Leetcode 146 LRU Cache雙向連結串列+STL

解題思路:用一個雙向連結串列,維護一個最近訪問次序,用map記錄對應key的結點指標。對於get請求,需要將當前結點移動到連結串列的頭位置;對於put操作,如果是更新,則同樣將當前結點移動到頭位置,如果不是更新,則在頭位置插入一個新結點。如果連結串列長度超過快取上限,則刪除末

Leetcode之合併有序單鏈表簡單 連結串列 遞迴

合併兩個已排序的連結列表並將其作為新列表返回。新列表應該通過拼接前兩個列表的節點來完成。 例: 輸入: 1-> 2-> 4,1-> 3-> 4 輸出: 1-> 1-> 2-> 3-> 4-> 4 直接遞迴實現,程式碼來了 /**

LeetCode自我總結連結串列進行插入排序

對連結串列進行插入排序。 插入排序的動畫演示如上。從第一個元素開始,該連結串列可以被認為已經部分排序(用黑色表示)。 每次迭代時,從輸入資料中移除一個元素(用紅色表示),並原地將其插入到已排好序的連結串列中。   插入排序演算法: 插入排序是迭代的,每次只移動一個元

手寫LinkedList雙向連結串列

手寫LinkedList(雙向連結串列) 系統jdk裡的LinkedList是由一個個節點連線起來的,節點就相當於一個物件,裡面有資料域和指標域,資料域是存放資料用的,指標域就是指向下一個節點 從而相連線的 這裡是一個節點 那麼連結串列裡是什麼樣子的呢

HDU 6215 Brute Force Sorting雙向連結串列+佇列

Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 2304    Acce

【 C# 資料結構】 -------------------------- 泛型帶頭節點的單鏈表,雙向連結串列實現

在程式設計領域,資料結構與演算法向來都是提升程式設計能力的重點。而一般常見的資料結構是連結串列,棧,佇列,樹等。事實上C#也已經封裝好了這些資料結構,在標頭檔案 System.Collections.Generic 中,直接建立並呼叫其成員方法就行。不過我們學習當然要知其然,亦知其所以然。 本文實現的是連結

Lava連結串列雙向連結串列---介面實現

在Java中連標配的結點需要用類來封裝,下面的簡單的雙向連結串列實現: class Node { private Object data; private Node next; public Node(Object data) {

C++ 類模板小結雙向連結串列的類模板實現

一、類模板定義 定義一個類模板:template<class 模板引數表> class 類名{ // 類定義...... };其中,template 是宣告類模板的關鍵字,表示宣告一個模板,模板引數可以是一個,也可以是多個,可以是型別引數,也可以是非型別引數。型

HDU 4286 Data Handler 雙向連結串列

                                                 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) P