亚欧色一区w666天堂,色情一区二区三区免费看,少妇特黄A片一区二区三区,亚洲人成网站999久久久综合,国产av熟女一区二区三区

  • 發布文章
  • 消息中心
點贊
收藏
評論
分享
原創

最小的K個數-算法學習

2023-07-11 10:13:25
4
0

題目詳情:
給定一個長度為 n 的可能有重復值的數組,找出其中不去重的最小的 k 個數。例如數組元素是4,5,1,6,2,7,3,8這8個數字,則最小的4個數字是1,2,3,4(任意順序皆可)。
例如
輸入:[4,5,1,6,2,7,3,8],4
返回值:[1,2,3,4]
說明:返(fan)回最小的4個數即(ji)可,返(fan)回[1,3,2,4]也(ye)可以


解題思路:

  1. 創建一個最大堆。
  2. 遍歷整個數組,將數組中的元素依次插入最大堆中。
  3. 先將前k個元素先入隊
  4. 在進行后面元素入隊時,要先比較根節點值是否大于當前元素,如果根節點值>當前元素,就將根節點出隊,當前元素入隊
  5. 完成遍歷后,堆中剩余的 k 個元素即為不去重的最小的 k 個數。

代碼實現:
class MaxHeap {
    constructor() {
        this.heap = [];
    }

    getParentIndex(index) {
        return Math.floor((index - 1) / 2);
    }

    getLeftChildIndex(index) {
        return index * 2 + 1;
    }

    getRightChildIndex(index) {
        return index * 2 + 2;
    }

    swap(arr, i, j) {
        [arr[i], arr[j]] = [arr[j], arr[i]];
    }

    shiftUp(index) {
        if (index === 0) {
            return;
        }
        const parentIndex = this.getParentIndex(index);
        if (this.heap[parentIndex] < this.heap[index]) {
            this.swap(this.heap, parentIndex, index);
            this.shiftUp(parentIndex);
        }
    }

    shiftDown(index) {
        const leftChildIndex = this.getLeftChildIndex(index);
        const rightChildIndex = this.getRightChildIndex(index);
        let maxIndex = index;
        if (
            leftChildIndex < this.heap.length &&
            this.heap[leftChildIndex] > this.heap[maxIndex]
        ) {
            maxIndex = leftChildIndex;
        }
        if (
            rightChildIndex < this.heap.length &&
            this.heap[rightChildIndex] > this.heap[maxIndex]
        ) {
            maxIndex = rightChildIndex;
        }
        if (maxIndex !== index) {
            this.swap(this.heap, maxIndex, index);
            this.shiftDown(maxIndex);
        }
    }

    insert(value) {
        this.heap.push(value);
        this.shiftUp(this.heap.length - 1);
    }

    pop() {
        if (this.heap.length === 0) {
            return -1;
        }
        if (this.heap.length === 1) {
            return this.heap.pop();
        }
        const top = this.heap[0];
        this.heap[0] = this.heap.pop();
        this.shiftDown(0);
        return top;
    }

    peek() {
        if (this.heap.length === 0) {
            return -1;
        }
        return this.heap[0];
    }

    size() {
        return this.heap.length;
    }
}

function getLeastNumbers(numbers, k) {
    const n = numbers.length;
    if (k <= 0 || k > n) {
        return [];
    }
    const heap = new MaxHeap();
    for (let i = 0; i < n; i++) {
        if (i < k) {
            heap.insert(numbers[i]);
        } else if (numbers[i] < heap.heap[0]) {
            heap.pop();
            heap.insert(numbers[i]);
        }
    }
    const result = [];
    for (let i = 0; i < k; i++) {
        result.push(heap.pop());
    }

    return result;
}

// 測試示例
console.log(getLeastNumbers([4, 5, 1, 6, 2, 7, 3, 8], 4)); // 輸出(chu): [ 4, 3, 2, 1 ]

0條評論
作者已關閉評論
t****m
98文章數
1粉(fen)絲數(shu)
t****m
98 文章 | 1 粉絲
t****m
98文(wen)章數
1粉絲數
t****m
98 文章 | 1 粉絲
原創

最小的K個數-算法學習

2023-07-11 10:13:25
4
0

題目詳情:
給定一個長度為 n 的可能有重復值的數組,找出其中不去重的最小的 k 個數。例如數組元素是4,5,1,6,2,7,3,8這8個數字,則最小的4個數字是1,2,3,4(任意順序皆可)。
例如
輸入:[4,5,1,6,2,7,3,8],4
返回值:[1,2,3,4]
說明:返回(hui)最小的(de)4個數即可,返回(hui)[1,3,2,4]也(ye)可以(yi)


解題思路:

  1. 創建一個最大堆。
  2. 遍歷整個數組,將數組中的元素依次插入最大堆中。
  3. 先將前k個元素先入隊
  4. 在進行后面元素入隊時,要先比較根節點值是否大于當前元素,如果根節點值>當前元素,就將根節點出隊,當前元素入隊
  5. 完成遍歷后,堆中剩余的 k 個元素即為不去重的最小的 k 個數。

代碼實現:
class MaxHeap {
    constructor() {
        this.heap = [];
    }

    getParentIndex(index) {
        return Math.floor((index - 1) / 2);
    }

    getLeftChildIndex(index) {
        return index * 2 + 1;
    }

    getRightChildIndex(index) {
        return index * 2 + 2;
    }

    swap(arr, i, j) {
        [arr[i], arr[j]] = [arr[j], arr[i]];
    }

    shiftUp(index) {
        if (index === 0) {
            return;
        }
        const parentIndex = this.getParentIndex(index);
        if (this.heap[parentIndex] < this.heap[index]) {
            this.swap(this.heap, parentIndex, index);
            this.shiftUp(parentIndex);
        }
    }

    shiftDown(index) {
        const leftChildIndex = this.getLeftChildIndex(index);
        const rightChildIndex = this.getRightChildIndex(index);
        let maxIndex = index;
        if (
            leftChildIndex < this.heap.length &&
            this.heap[leftChildIndex] > this.heap[maxIndex]
        ) {
            maxIndex = leftChildIndex;
        }
        if (
            rightChildIndex < this.heap.length &&
            this.heap[rightChildIndex] > this.heap[maxIndex]
        ) {
            maxIndex = rightChildIndex;
        }
        if (maxIndex !== index) {
            this.swap(this.heap, maxIndex, index);
            this.shiftDown(maxIndex);
        }
    }

    insert(value) {
        this.heap.push(value);
        this.shiftUp(this.heap.length - 1);
    }

    pop() {
        if (this.heap.length === 0) {
            return -1;
        }
        if (this.heap.length === 1) {
            return this.heap.pop();
        }
        const top = this.heap[0];
        this.heap[0] = this.heap.pop();
        this.shiftDown(0);
        return top;
    }

    peek() {
        if (this.heap.length === 0) {
            return -1;
        }
        return this.heap[0];
    }

    size() {
        return this.heap.length;
    }
}

function getLeastNumbers(numbers, k) {
    const n = numbers.length;
    if (k <= 0 || k > n) {
        return [];
    }
    const heap = new MaxHeap();
    for (let i = 0; i < n; i++) {
        if (i < k) {
            heap.insert(numbers[i]);
        } else if (numbers[i] < heap.heap[0]) {
            heap.pop();
            heap.insert(numbers[i]);
        }
    }
    const result = [];
    for (let i = 0; i < k; i++) {
        result.push(heap.pop());
    }

    return result;
}

// 測試示例
console.log(getLeastNumbers([4, 5, 1, 6, 2, 7, 3, 8], 4)); // 輸(shu)出(chu): [ 4, 3, 2, 1 ]

文章來自個人專欄
文章 | 訂閱
0條評論
作者已關閉評論
作者已關閉評論
0
0