mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -395,30 +395,102 @@ func maxSlidingWindow(nums []int, k int) []int {
|
|||||||
|
|
||||||
Javascript:
|
Javascript:
|
||||||
```javascript
|
```javascript
|
||||||
|
/**
|
||||||
|
* @param {number[]} nums
|
||||||
|
* @param {number} k
|
||||||
|
* @return {number[]}
|
||||||
|
*/
|
||||||
var maxSlidingWindow = function (nums, k) {
|
var maxSlidingWindow = function (nums, k) {
|
||||||
// 队列数组(存放的是元素下标,为了取值方便)
|
class MonoQueue {
|
||||||
const q = [];
|
queue;
|
||||||
// 结果数组
|
constructor() {
|
||||||
const ans = [];
|
this.queue = [];
|
||||||
for (let i = 0; i < nums.length; i++) {
|
|
||||||
// 若队列不为空,且当前元素大于等于队尾所存下标的元素,则弹出队尾
|
|
||||||
while (q.length && nums[i] >= nums[q[q.length - 1]]) {
|
|
||||||
q.pop();
|
|
||||||
}
|
}
|
||||||
// 入队当前元素下标
|
enqueue(value) {
|
||||||
q.push(i);
|
let back = this.queue[this.queue.length - 1];
|
||||||
// 判断当前最大值(即队首元素)是否在窗口中,若不在便将其出队
|
while (back !== undefined && back < value) {
|
||||||
if (q[0] <= i - k) {
|
this.queue.pop();
|
||||||
q.shift();
|
back = this.queue[this.queue.length - 1];
|
||||||
}
|
}
|
||||||
// 当达到窗口大小时便开始向结果中添加数据
|
this.queue.push(value);
|
||||||
if (i >= k - 1) ans.push(nums[q[0]]);
|
|
||||||
}
|
}
|
||||||
return ans;
|
dequeue(value) {
|
||||||
|
let front = this.front();
|
||||||
|
if (front === value) {
|
||||||
|
this.queue.shift();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
front() {
|
||||||
|
return this.queue[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let helperQueue = new MonoQueue();
|
||||||
|
let i = 0, j = 0;
|
||||||
|
let resArr = [];
|
||||||
|
while (j < k) {
|
||||||
|
helperQueue.enqueue(nums[j++]);
|
||||||
|
}
|
||||||
|
resArr.push(helperQueue.front());
|
||||||
|
while (j < nums.length) {
|
||||||
|
helperQueue.enqueue(nums[j]);
|
||||||
|
helperQueue.dequeue(nums[i]);
|
||||||
|
resArr.push(helperQueue.front());
|
||||||
|
i++, j++;
|
||||||
|
}
|
||||||
|
return resArr;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
TypeScript:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function maxSlidingWindow(nums: number[], k: number): number[] {
|
||||||
|
/** 单调递减队列 */
|
||||||
|
class MonoQueue {
|
||||||
|
private queue: number[];
|
||||||
|
constructor() {
|
||||||
|
this.queue = [];
|
||||||
|
};
|
||||||
|
/** 入队:value如果大于队尾元素,则将队尾元素删除,直至队尾元素大于value,或者队列为空 */
|
||||||
|
public enqueue(value: number): void {
|
||||||
|
let back: number | undefined = this.queue[this.queue.length - 1];
|
||||||
|
while (back !== undefined && back < value) {
|
||||||
|
this.queue.pop();
|
||||||
|
back = this.queue[this.queue.length - 1];
|
||||||
|
}
|
||||||
|
this.queue.push(value);
|
||||||
|
};
|
||||||
|
/** 出队:只有当队头元素等于value,才出队 */
|
||||||
|
public dequeue(value: number): void {
|
||||||
|
let top: number | undefined = this.top();
|
||||||
|
if (top !== undefined && top === value) {
|
||||||
|
this.queue.shift();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public top(): number | undefined {
|
||||||
|
return this.queue[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const helperQueue: MonoQueue = new MonoQueue();
|
||||||
|
let i: number = 0,
|
||||||
|
j: number = 0;
|
||||||
|
let resArr: number[] = [];
|
||||||
|
while (j < k) {
|
||||||
|
helperQueue.enqueue(nums[j++]);
|
||||||
|
}
|
||||||
|
resArr.push(helperQueue.top()!);
|
||||||
|
while (j < nums.length) {
|
||||||
|
helperQueue.enqueue(nums[j]);
|
||||||
|
helperQueue.dequeue(nums[i]);
|
||||||
|
resArr.push(helperQueue.top()!);
|
||||||
|
j++, i++;
|
||||||
|
}
|
||||||
|
return resArr;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift:
|
Swift:
|
||||||
|
|
||||||
```Swift
|
```Swift
|
||||||
/// 双向链表
|
/// 双向链表
|
||||||
class DoublyListNode {
|
class DoublyListNode {
|
||||||
|
@ -183,28 +183,22 @@ public:
|
|||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public int eraseOverlapIntervals(int[][] intervals) {
|
public int eraseOverlapIntervals(int[][] intervals) {
|
||||||
if (intervals.length < 2) return 0;
|
Arrays.sort(intervals, (a, b) -> {
|
||||||
|
if (a[0] == a[0]) return a[1] - b[1];
|
||||||
Arrays.sort(intervals, new Comparator<int[]>() {
|
return a[0] - b[0];
|
||||||
@Override
|
|
||||||
public int compare(int[] o1, int[] o2) {
|
|
||||||
if (o1[1] != o2[1]) {
|
|
||||||
return Integer.compare(o1[1],o2[1]);
|
|
||||||
} else {
|
|
||||||
return Integer.compare(o1[0],o2[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
int count = 1;
|
int count = 0;
|
||||||
int edge = intervals[0][1];
|
int edge = Integer.MIN_VALUE;
|
||||||
for (int i = 1; i < intervals.length; i++) {
|
for (int i = 0; i < intervals.length; i++) {
|
||||||
if (edge <= intervals[i][0]){
|
if (edge <= intervals[i][0]) {
|
||||||
count ++; //non overlap + 1
|
|
||||||
edge = intervals[i][1];
|
edge = intervals[i][1];
|
||||||
|
} else {
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return intervals.length - count;
|
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -183,13 +183,11 @@ private static void testCompletePack(){
|
|||||||
int[] value = {15, 20, 30};
|
int[] value = {15, 20, 30};
|
||||||
int bagWeight = 4;
|
int bagWeight = 4;
|
||||||
int[] dp = new int[bagWeight + 1];
|
int[] dp = new int[bagWeight + 1];
|
||||||
for (int i = 0; i < weight.length; i++){
|
for (int i = 0; i < weight.length; i++){ // 遍历物品
|
||||||
for (int j = 1; j <= bagWeight; j++){
|
for (int j = weight[i]; j <= bagWeight; j++){ // 遍历背包容量
|
||||||
if (j - weight[i] >= 0){
|
|
||||||
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
|
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
for (int maxValue : dp){
|
for (int maxValue : dp){
|
||||||
System.out.println(maxValue + " ");
|
System.out.println(maxValue + " ");
|
||||||
}
|
}
|
||||||
@ -201,8 +199,8 @@ private static void testCompletePackAnotherWay(){
|
|||||||
int[] value = {15, 20, 30};
|
int[] value = {15, 20, 30};
|
||||||
int bagWeight = 4;
|
int bagWeight = 4;
|
||||||
int[] dp = new int[bagWeight + 1];
|
int[] dp = new int[bagWeight + 1];
|
||||||
for (int i = 1; i <= bagWeight; i++){
|
for (int i = 1; i <= bagWeight; i++){ // 遍历背包容量
|
||||||
for (int j = 0; j < weight.length; j++){
|
for (int j = 0; j < weight.length; j++){ // 遍历物品
|
||||||
if (i - weight[j] >= 0){
|
if (i - weight[j] >= 0){
|
||||||
dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]);
|
dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user