From 3f4f359aa2988d180fe4d1e4fb47da83fde7ec25 Mon Sep 17 00:00:00 2001 From: pranayh24 Date: Tue, 15 Oct 2024 15:36:39 +0530 Subject: [PATCH 1/7] Add Maximum Sliding Window algorithm using deque for efficient O(n) solution --- .../others/MaximumSlidingWindow.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java diff --git a/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java new file mode 100644 index 000000000000..e0dec6462a45 --- /dev/null +++ b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java @@ -0,0 +1,68 @@ +package com.thealgorithms.others; + +import java.util.ArrayDeque; +import java.util.Deque; + +/** + * Maximum Sliding Window Algorithm + * + * This algorithm finds the maximum element in each sliding window of size k + * in a given array of integers. It uses a deque (double-ended queue) to + * efficiently keep track of potential maximum values in the current window. + * + * Time Complexity: O(n), where n is the number of elements in the input array + * Space Complexity: O(k), where k is the size of the sliding window + */ + +public class MaximumSlidingWindow { + + /** + * Finds the maximum values in each sliding window of size k. + * + * @param nums The input array of integers + * @param windowSize The size of the sliding window + * @return An array of integers representing the maximums in each sliding window + */ + public int[] maxSlidingWindow(int[] nums, int windowSize){ + if(nums==null || nums.length==0 || windowSize<=0 || windowSize>nums.length) { + return new int[0]; // Handle edge cases + } + + int[] result = new int[nums.length - windowSize + 1]; + Deque deque = new ArrayDeque<>(); + + for(int currentIndex=0;currentIndex=windowSize-1) { + result[currentIndex - windowSize + 1] = nums[deque.peekFirst()]; + } + } + return result; + } + public static void main(String[] args) { + MaximumSlidingWindow msw = new MaximumSlidingWindow(); + int[] nums = {1,3,-1,-3,5,3,6,7}; + int k = 3; + + // Calculate the maximum sliding window + int[] result = msw.maxSlidingWindow(nums, k); + + // Print the result + for(int num:result) + System.out.print(num+" "); + } +} From d62fe559241922b33279109592a900d16d346d72 Mon Sep 17 00:00:00 2001 From: pranayh24 Date: Tue, 15 Oct 2024 16:29:27 +0530 Subject: [PATCH 2/7] Formatted MaximumSlidingWindow.java using clang-format --- .../others/MaximumSlidingWindow.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java index e0dec6462a45..0b12f6fc0b93 100644 --- a/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java +++ b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java @@ -21,25 +21,25 @@ public class MaximumSlidingWindow { * * @param nums The input array of integers * @param windowSize The size of the sliding window - * @return An array of integers representing the maximums in each sliding window + * @return An array of integers representing the maximums in each window */ - public int[] maxSlidingWindow(int[] nums, int windowSize){ - if(nums==null || nums.length==0 || windowSize<=0 || windowSize>nums.length) { + public int[] maxSlidingWindow(int[] nums, int windowSize) { + if (nums == null || nums.length == 0 || windowSize <= 0 || windowSize > nums.length) { return new int[0]; // Handle edge cases } int[] result = new int[nums.length - windowSize + 1]; Deque deque = new ArrayDeque<>(); - for(int currentIndex=0;currentIndex=windowSize-1) { + if (currentIndex >= windowSize - 1) { result[currentIndex - windowSize + 1] = nums[deque.peekFirst()]; } } @@ -55,14 +55,13 @@ public int[] maxSlidingWindow(int[] nums, int windowSize){ } public static void main(String[] args) { MaximumSlidingWindow msw = new MaximumSlidingWindow(); - int[] nums = {1,3,-1,-3,5,3,6,7}; + int[] nums = {1, 3, -1, -3, 5, 3, 6, 7}; int k = 3; // Calculate the maximum sliding window int[] result = msw.maxSlidingWindow(nums, k); // Print the result - for(int num:result) - System.out.print(num+" "); + for (int num : result) System.out.print(num + " "); } } From bf74f4f3284fc3704c7bab8cc47ab117492abafd Mon Sep 17 00:00:00 2001 From: pranayh24 Date: Tue, 15 Oct 2024 16:33:47 +0530 Subject: [PATCH 3/7] Fixed Checkstyle issue by adding braces to for loop --- .../java/com/thealgorithms/others/MaximumSlidingWindow.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java index 0b12f6fc0b93..1d17f2970b34 100644 --- a/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java +++ b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java @@ -62,6 +62,8 @@ public static void main(String[] args) { int[] result = msw.maxSlidingWindow(nums, k); // Print the result - for (int num : result) System.out.print(num + " "); + for (int num : result) { + System.out.print(num + " "); + } } } From 1f13d8402c01a5e8d025805eaeec9bff07173bd2 Mon Sep 17 00:00:00 2001 From: pranayh24 Date: Wed, 16 Oct 2024 17:34:44 +0530 Subject: [PATCH 4/7] Add tests for MaximumSlidingWindow --- .../others/MaximumSlidingWindowTest.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java diff --git a/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java b/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java new file mode 100644 index 000000000000..e33f4f57d533 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java @@ -0,0 +1,63 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class MaximumSlidingWindowTest { + + MaximumSlidingWindow msw; + int[] nums; + int k; + + @BeforeEach + void setUp() { + msw = new MaximumSlidingWindow(); // Initialize the MaximumSlidingWindow object + } + + // Test for a simple sliding window case + @Test + void testMaxSlidingWindow_SimpleCase() { + nums = new int[] {1, 3, -1, -3, 5, 3, 6, 7}; + k = 3; + int[] expected = {3, 3, 5, 5, 6, 7}; + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); + } + + // Test when window size is 1 (output should be the array itself) + @Test + void testMaxSlidingWindow_WindowSizeOne() { + nums = new int[] {4, 2, 12, 11, -5}; + k = 1; + int[] expected = {4, 2, 12, 11, -5}; + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); + } + + // Test when the window size is equal to the array length (output should be a single max element) + @Test + void testMaxSlidingWindow_WindowSizeEqualsArrayLength() { + nums = new int[] {4, 2, 12, 11, -5}; + k = nums.length; + int[] expected = {12}; // Maximum of the entire array + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); + } + + // Test when the input array is empty + @Test + void testMaxSlidingWindow_EmptyArray() { + nums = new int[] {}; + k = 3; + int[] expected = {}; + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); + } + + // Test when the window size is larger than the array (should return empty) + @Test + void testMaxSlidingWindow_WindowSizeLargerThanArray() { + nums = new int[] {1, 2, 3}; + k = 5; + int[] expected = {}; // Window size is too large, so no result + assertArrayEquals(expected, msw.maxSlidingWindow(nums, k)); + } +} From 5e3ddc3ec1ffec3132819316e2cd2805d6c908c9 Mon Sep 17 00:00:00 2001 From: pranayh24 Date: Wed, 16 Oct 2024 17:40:19 +0530 Subject: [PATCH 5/7] Added test function for MaximumSlidingWindow --- .../thealgorithms/others/MaximumSlidingWindow.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java index 1d17f2970b34..425146d5d042 100644 --- a/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java +++ b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java @@ -53,17 +53,5 @@ public int[] maxSlidingWindow(int[] nums, int windowSize) { } return result; } - public static void main(String[] args) { - MaximumSlidingWindow msw = new MaximumSlidingWindow(); - int[] nums = {1, 3, -1, -3, 5, 3, 6, 7}; - int k = 3; - // Calculate the maximum sliding window - int[] result = msw.maxSlidingWindow(nums, k); - - // Print the result - for (int num : result) { - System.out.print(num + " "); - } - } } From b5305ffd81d42a484a6a26afa8edcdb0a4f0a14a Mon Sep 17 00:00:00 2001 From: pranayh24 Date: Wed, 16 Oct 2024 17:49:03 +0530 Subject: [PATCH 6/7] Fixed formatting issues --- .../java/com/thealgorithms/others/MaximumSlidingWindow.java | 1 - .../java/com/thealgorithms/others/MaximumSlidingWindowTest.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java index 425146d5d042..d0b2c2a0e56d 100644 --- a/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java +++ b/src/main/java/com/thealgorithms/others/MaximumSlidingWindow.java @@ -53,5 +53,4 @@ public int[] maxSlidingWindow(int[] nums, int windowSize) { } return result; } - } diff --git a/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java b/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java index e33f4f57d533..46d1e9570bc7 100644 --- a/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java +++ b/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java @@ -1,6 +1,6 @@ package com.thealgorithms.others; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; From f3f705a8ba2a5c27c19c841d661703a43dd0720d Mon Sep 17 00:00:00 2001 From: pranayh24 Date: Wed, 16 Oct 2024 17:54:38 +0530 Subject: [PATCH 7/7] Fix method names to conform to Checkstyle standards --- .../thealgorithms/others/MaximumSlidingWindowTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java b/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java index 46d1e9570bc7..9209136a5af3 100644 --- a/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java +++ b/src/test/java/com/thealgorithms/others/MaximumSlidingWindowTest.java @@ -18,7 +18,7 @@ void setUp() { // Test for a simple sliding window case @Test - void testMaxSlidingWindow_SimpleCase() { + void testMaxSlidingWindowSimpleCase() { nums = new int[] {1, 3, -1, -3, 5, 3, 6, 7}; k = 3; int[] expected = {3, 3, 5, 5, 6, 7}; @@ -27,7 +27,7 @@ void testMaxSlidingWindow_SimpleCase() { // Test when window size is 1 (output should be the array itself) @Test - void testMaxSlidingWindow_WindowSizeOne() { + void testMaxSlidingWindowWindowSizeOne() { nums = new int[] {4, 2, 12, 11, -5}; k = 1; int[] expected = {4, 2, 12, 11, -5}; @@ -36,7 +36,7 @@ void testMaxSlidingWindow_WindowSizeOne() { // Test when the window size is equal to the array length (output should be a single max element) @Test - void testMaxSlidingWindow_WindowSizeEqualsArrayLength() { + void testMaxSlidingWindowWindowSizeEqualsArrayLength() { nums = new int[] {4, 2, 12, 11, -5}; k = nums.length; int[] expected = {12}; // Maximum of the entire array @@ -45,7 +45,7 @@ void testMaxSlidingWindow_WindowSizeEqualsArrayLength() { // Test when the input array is empty @Test - void testMaxSlidingWindow_EmptyArray() { + void testMaxSlidingWindowEmptyArray() { nums = new int[] {}; k = 3; int[] expected = {}; @@ -54,7 +54,7 @@ void testMaxSlidingWindow_EmptyArray() { // Test when the window size is larger than the array (should return empty) @Test - void testMaxSlidingWindow_WindowSizeLargerThanArray() { + void testMaxSlidingWindowWindowSizeLargerThanArray() { nums = new int[] {1, 2, 3}; k = 5; int[] expected = {}; // Window size is too large, so no result pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy