-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ac1741
commit 8b73a29
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/github/songjiang951130/leetcode/heap/ArrayMax.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.github.songjiang951130.leetcode.heap; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
public class ArrayMax { | ||
|
||
/** | ||
* 思路记录:滑动窗口 打算用linked list。做队列,然后用生成一个队列最大值 max; | ||
* 出队的时候元素a 比较max 1、如果 a==max,重新生成队列最大值max;2、a < max 最大值存入res[] 3、a >max 不可能存在</> | ||
* | ||
* @param nums | ||
* @param k | ||
* @return | ||
*/ | ||
public int[] maxSlidingWindow(int[] nums, int k) { | ||
int[] sw = new int[k]; | ||
int[] res = new int[nums.length - k + 1]; | ||
int max = Integer.MIN_VALUE; | ||
for (int i = 0; i < k; i++) { | ||
sw[i] = nums[i]; | ||
max = Math.max(max, sw[i]); | ||
} | ||
int index = 0; | ||
res[index++] = max; | ||
|
||
for (int i = k; i < nums.length; i++) { | ||
int num = nums[i]; | ||
int offer = offer(sw); | ||
add(sw, num); | ||
|
||
if (offer == max) { | ||
max = getMax(sw); | ||
} else if (num > max) { | ||
max = num; | ||
} | ||
res[index++] = max; | ||
} | ||
return res; | ||
} | ||
|
||
private int getMax(int[] array) { | ||
int max = Integer.MIN_VALUE; | ||
for (int i = 0; i < array.length; i++) { | ||
max = Math.max(max, array[i]); | ||
} | ||
return max; | ||
} | ||
|
||
private int offer(int[] array) { | ||
int res = array[0]; | ||
for (int i = 0; i < array.length - 1; i++) { | ||
array[i] = array[i + 1]; | ||
} | ||
return res; | ||
} | ||
|
||
private void add(int[] array, int val) { | ||
array[array.length - 1] = val; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/test/java/com/github/songjiang951130/leetcode/heap/ArrayMaxTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.songjiang951130.leetcode.heap; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
|
||
public class ArrayMaxTest extends TestCase { | ||
|
||
public void testMaxSlidingWindow() { | ||
//warning 超时 | ||
ArrayMax solution = new ArrayMax(); | ||
assertArrayEquals(new int[]{3, 3, 5, 5, 6, 7}, solution.maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3)); | ||
assertArrayEquals(new int[]{1}, solution.maxSlidingWindow(new int[]{1}, 1)); | ||
} | ||
} |