Code Monkey home page Code Monkey logo

Comments (2)

WanderHuang avatar WanderHuang commented on September 27, 2024
第k小的数字
function minK(num, k) {
  k = k - 1;
  function partial(num, start, end) {
    let pivot = num[start];
    while (start < end) {
      while (start < end && num[end] > pivot) end--;
      if (start < end) {
        num[start] = num[end];
        start++;
      }
      while (start < end && num[start] < pivot) start++;
      if (start < end) {
        num[end] = num[start];
        end--;
      }
    }
    num[start] = pivot;

    return start;
  }

  function findK(start, end) {
    let mid = partial(num, start, end);
    if (mid === k) return num[mid];
    if (mid > k) return findK(start, mid - 1);
    if (mid < k) return findK(mid + 1, end);
  }

  return findK(0, num.length - 1);
}

let arr = [1, 234, 4, 23, 88, 34, 90, 45, 63, 2, 3, 4, 555, 6, 7, 8, 10, 9];
console.log(arr);
console.log(minK(arr, 18));
console.log(arr.sort((a, b) => a - b));

from basic-programming-knowledge.

WanderHuang avatar WanderHuang commented on September 27, 2024

合并K个有序链表

  1. 解决两条有序链表的合并
  2. 解决规模划分,即两两划分
  3. 递归处理子问题,注意当数组单位为1时,直接返回该链表,奇数长度数组又一个元素作为单位为1的处理
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode[]} lists
 * @return {ListNode}
 */
var mergeKLists = function (lists) {
    if (!lists || !lists.length) return null
    // 合并两排序链表
    function mergeTwo(a, b) {
        if (!a) return b;
        if (!b) return a;

        let head = new ListNode(0);
        let tail = head;

        while (a && b) {
            if (a.val < b.val) {
                tail.next = a;
                a = a.next;
            } else {
                tail.next = b;
                b = b.next;
            }
            tail = tail.next;
        }
        tail.next = (a ? a : b);
        return head.next;
    }

    // 分治算法 两两合并
    function division(lists, l, r) {
        if (!lists) return null;
        if (l === r) return lists[l]
        let all = [];
        while(l < r) {
            all.push(mergeTwo(lists[l++], lists[r--]));
        }
        if (l === r) all.push(lists[l])

        return division(all, 0, all.length - 1)
    }

    return division(lists, 0, lists.length - 1)
};

from basic-programming-knowledge.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.