Sort List -- LeetCode

本文介绍了一种使用归并排序算法对链表进行排序的方法,包括算法原理、实现步骤以及如何避免使用额外栈空间的问题。此外,文章还提到了排序算法在面试中的重要性以及与其他算法思想的联系。
原题链接:  http://oj.leetcode.com/problems/sort-list/  
这道题跟Insertion Sort List类似,要求我们用O(nlogn)算法对链表进行排序,但是并没有要求用哪一种排序算法,我们可以使用归并排序,快速排序,堆排序等满足要求的方法来实现。对于这道题比较容易想到的是归并排序,因为我们已经做过 Merge Two Sorted Lists ,这是归并排序的一个subroutine。剩下我们需要做的就是每次找到中点,然后对于左右进行递归,最后用 Merge Two Sorted Lists 把他们合并起来。代码如下:
public ListNode sortList(ListNode head) {
    return mergeSort(head);
}
private ListNode mergeSort(ListNode head)
{
    if(head == null || head.next == null)
        return head;
    ListNode walker = head;
    ListNode runner = head;
    while(runner.next!=null && runner.next.next!=null)
    {
        walker = walker.next;
        runner = runner.next.next;
    }
    ListNode head2 = walker.next;
    walker.next = null;
    ListNode head1 = head;
    head1 = mergeSort(head1);
    head2 = mergeSort(head2);
    return merge(head1, head2);
}
private ListNode merge(ListNode head1, ListNode head2)
{
    ListNode helper = new ListNode(0);
    helper.next = head1;
    ListNode pre = helper;
    while(head1!=null && head2!=null)
    {
        if(head1.val<head2.val)
        {
            head1 = head1.next;
        }
        else
        {
            ListNode next = head2.next;
            head2.next = pre.next;
            pre.next = head2;
            head2 = next;
        }
        pre = pre.next;
    }
    if(head2!=null)
    {
        pre.next = head2;
    }
    return helper.next;
}
不过用归并排序有个问题就是这里如果把栈空间算上的话还是需要O(logn)的空间的。对于其他排序算法,用兴趣的同学可以实现一下哈。
排序是面试中比较基础的一个主题,所以对于各种常见的排序算法大家还是要熟悉,不了解的朋友可以参见 排序算法 - Wiki 。特别是算法的原理,很多题目虽然没有直接考察排序的实现,但是用到了其中的思想,比如非常经典的topK问题,就用到了快速排序的原理,关于这个问题在 Median of Two Sorted Arrays 中有提到,有兴趣的朋友可以看看。
### LeetCode 475 Heaters Problem Solution and Explanation In this problem, one needs to find the minimum radius of heaters so that all houses can be warmed. Given positions of `houses` and `heaters`, both represented as integer arrays, the task is to determine the smallest maximum distance from any house to its nearest heater[^1]. To solve this issue efficiently: #### Binary Search Approach A binary search on answer approach works well here because increasing the radius monotonically increases the number of covered houses. Start by sorting the list of heaters' locations which allows using binary search for finding closest heater distances quickly. ```python def findRadius(houses, heaters): import bisect houses.sort() heaters.sort() max_distance = 0 for house in houses: pos = bisect.bisect_left(heaters, house) dist_to_right_heater = abs(heaters[pos] - house) if pos < len(heaters) else float('inf') dist_to_left_heater = abs(heaters[pos-1] - house) if pos > 0 else float('inf') min_dist_for_house = min(dist_to_right_heater, dist_to_left_heater) max_distance = max(max_distance, min_dist_for_house) return max_distance ``` This code snippet sorts the lists of houses and heaters first. For each house, it finds the nearest heater either directly or indirectly (to the left side). It calculates the minimal distance between these two options and updates the global maximal value accordingly[^3]. The constraints specify that numbers of houses and heaters do not exceed 25000 while their positions range up to \(10^9\)[^2], making efficient algorithms like binary search necessary due to large input sizes involved.
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值