mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +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
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @param {number} k
|
||||
* @return {number[]}
|
||||
*/
|
||||
var maxSlidingWindow = function (nums, k) {
|
||||
// 队列数组(存放的是元素下标,为了取值方便)
|
||||
const q = [];
|
||||
// 结果数组
|
||||
const ans = [];
|
||||
for (let i = 0; i < nums.length; i++) {
|
||||
// 若队列不为空,且当前元素大于等于队尾所存下标的元素,则弹出队尾
|
||||
while (q.length && nums[i] >= nums[q[q.length - 1]]) {
|
||||
q.pop();
|
||||
class MonoQueue {
|
||||
queue;
|
||||
constructor() {
|
||||
this.queue = [];
|
||||
}
|
||||
enqueue(value) {
|
||||
let back = 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);
|
||||
}
|
||||
dequeue(value) {
|
||||
let front = this.front();
|
||||
if (front === value) {
|
||||
this.queue.shift();
|
||||
}
|
||||
}
|
||||
front() {
|
||||
return this.queue[0];
|
||||
}
|
||||
}
|
||||
// 入队当前元素下标
|
||||
q.push(i);
|
||||
// 判断当前最大值(即队首元素)是否在窗口中,若不在便将其出队
|
||||
if (q[0] <= i - k) {
|
||||
q.shift();
|
||||
let helperQueue = new MonoQueue();
|
||||
let i = 0, j = 0;
|
||||
let resArr = [];
|
||||
while (j < k) {
|
||||
helperQueue.enqueue(nums[j++]);
|
||||
}
|
||||
// 当达到窗口大小时便开始向结果中添加数据
|
||||
if (i >= k - 1) ans.push(nums[q[0]]);
|
||||
}
|
||||
return ans;
|
||||
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
|
||||
/// 双向链表
|
||||
class DoublyListNode {
|
||||
|
@ -183,28 +183,22 @@ public:
|
||||
```java
|
||||
class Solution {
|
||||
public int eraseOverlapIntervals(int[][] intervals) {
|
||||
if (intervals.length < 2) return 0;
|
||||
|
||||
Arrays.sort(intervals, new Comparator<int[]>() {
|
||||
@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]);
|
||||
}
|
||||
}
|
||||
Arrays.sort(intervals, (a, b) -> {
|
||||
if (a[0] == a[0]) return a[1] - b[1];
|
||||
return a[0] - b[0];
|
||||
});
|
||||
|
||||
int count = 1;
|
||||
int edge = intervals[0][1];
|
||||
for (int i = 1; i < intervals.length; i++) {
|
||||
if (edge <= intervals[i][0]){
|
||||
count ++; //non overlap + 1
|
||||
int count = 0;
|
||||
int edge = Integer.MIN_VALUE;
|
||||
for (int i = 0; i < intervals.length; i++) {
|
||||
if (edge <= intervals[i][0]) {
|
||||
edge = intervals[i][1];
|
||||
} else {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return intervals.length - count;
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -183,11 +183,9 @@ private static void testCompletePack(){
|
||||
int[] value = {15, 20, 30};
|
||||
int bagWeight = 4;
|
||||
int[] dp = new int[bagWeight + 1];
|
||||
for (int i = 0; i < weight.length; i++){
|
||||
for (int j = 1; j <= bagWeight; j++){
|
||||
if (j - weight[i] >= 0){
|
||||
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
|
||||
}
|
||||
for (int i = 0; i < weight.length; i++){ // 遍历物品
|
||||
for (int j = weight[i]; j <= bagWeight; j++){ // 遍历背包容量
|
||||
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
|
||||
}
|
||||
}
|
||||
for (int maxValue : dp){
|
||||
@ -201,8 +199,8 @@ private static void testCompletePackAnotherWay(){
|
||||
int[] value = {15, 20, 30};
|
||||
int bagWeight = 4;
|
||||
int[] dp = new int[bagWeight + 1];
|
||||
for (int i = 1; i <= bagWeight; i++){
|
||||
for (int j = 0; j < weight.length; j++){
|
||||
for (int i = 1; i <= bagWeight; i++){ // 遍历背包容量
|
||||
for (int j = 0; j < weight.length; j++){ // 遍历物品
|
||||
if (i - weight[j] >= 0){
|
||||
dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user