Tags » Merge

Simple Things

“I hide behind simple things so you’ll find me;/ if you don’t find me, you’ll find the things,/ you’ll touch what my hands have touched,/our hand-prints will merge.” -Yannis Ritsos, 

6 more words
Love

[LeetCode] Intersection of Two Arrays II

题目:
求两个数组的交集,需要输出duplicate的交集元素
https://leetcode.com/problems/intersection-of-two-arrays-ii/

解法:
sort, then merge

Java代码:

public class Solution {
    // accepted
    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        
        int i = 0, j = 0;
        List<Integer> list = new ArrayList<>();
        while(i < nums1.length && j < nums2.length) {
            if (nums1[i] < nums2[j]) {
                i++;
            } else if (nums1[i] > nums2[j]) {
                j++;
            } else {
                list.add(nums1[i]);
                i++;
                j++;
            }
        }
        
        int[] res = new int;
        int idx = 0;
        for(Integer elem : list) {
            res = elem;
        }
        
        return res;
    }
}

[LeetCode] Super Ugly Number

题目:
对于任意一组prime factors,求出它对应的第n个super ugly number
https://leetcode.com/problems/super-ugly-number/

解法:
思路和ugly number II完全一样

Java代码:

public class Solution {
    // accepted
    public int nthSuperUglyNumber(int n, int[] primes) {
        int[] idx = new int;
        List<Integer> res = new ArrayList<>();
        res.add(1);
        
        int ugly = 1;
        int[] candidate = new int;
        for(int k = 2; k <= n; k++) {
            ugly = Integer.MAX_VALUE;
            for(int i = 0; i < primes.length; i++) {
                candidate[i] = primes[i] * res.get(idx[i]);
                ugly = Math.min(ugly, candidate[i]);
            }
            
            res.add(ugly);
            for(int i = 0; i < primes.length; i++) {
                if (candidate[i] == ugly) {
                    idx[i]++;
                }
            }
        }
        
        return res.get(res.size() - 1);
    }
}

[LeetCode] Ugly Number II

题目:
求出第n个丑数。丑数的定义是它的质因子只有2,3,5
https://leetcode.com/problems/ugly-number-ii/

解法:
更大的丑数只可能由更小的丑数产生,如果一开始给了2,3,5,那么可以分别乘以2,3,4,5,6,8,9。
想象下有如下三个有序的队列
(1) 2->4->6->8->10->12->16->18
(2) 3->6->9->12->15->18->24->27
(3) 5->10->15->20->25->30->40->45

每次从三个队列里拿出最小的元素,直到取到第n个数字。
如果有相等的队头,都要pop掉。
这里需要把已经产生的丑数都记录下来,用于expand每个丑数队列

整个过程类似k个链表merge

Java代码:

public class Solution {
    // accepted
    public int nthUglyNumber(int n) {
        int[] factor = new int[3];
        int[] idx = new int[3];
        
        factor[0] = 2;
        factor[1] = 3;
        factor[2] = 5;
        
        idx[0] = 0;
        idx[1] = 0;
        idx[0] = 0;
        
        List<Integer> res = new ArrayList<>();
        res.add(1);
        
        int ugly = 1;
        for(int k = 2; k <= n; k++) {
            int[] candidate = new int[3];
            for(int i = 0; i < 3; i++) {
                candidate[i] = factor[i] * res.get(idx[i]);
            }

            ugly = Math.min(candidate[0], Math.min(candidate[1], candidate[2]));
            res.add(ugly);
            for(int i = 0; i < 3; i++) {
                if (candidate[i] == ugly) {
                    idx[i]++;
                }
            }
        }
        
        return res.get(res.size() - 1);
    }
}

#Travel #500px : SORANO street by YusufGurel by YusufGurel #photography#IFTTT

Sorano is a town and comune in the province of Grosseto, southern Tuscany (Italy).

It as an ancient medieval hill town hanging from a tuff stone over the Lente River. 8 more words

IFTTT

[LeetCode] Sort List

题目:
排序一个链表,要求时间复杂度O(nlogn),常数空间
https://leetcode.com/problems/sort-list/

解法:
merge sort,divide and conquer
需要用快慢指针找到中点

如果不考虑递归的stack frame带来的space,那就是常数空间。

Java代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    // accepted, 7ms, 1 time AC
    public ListNode sortList(ListNode head) {
        // base case
        if (head == null || head.next == null) {
            return head;
        }
        
        // find mid point
        ListNode slow, fast;
        slow = fast = head;
        while(fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        
        ListNode rightHead = slow.next;
        slow.next = null;
        
        // divide and conquer
        return merge(sortList(head), sortList(rightHead));
    }
    
    private ListNode merge(ListNode left, ListNode right) {
        ListNode p = left, q = right;
        ListNode dummy = new ListNode(0);
        ListNode prev = dummy;
        
        while(p != null && q != null) {
            if(p.val < q.val) {
                prev.next = p;
                prev = p;
                p = p.next;
            } else {
                prev.next = q;
                prev = q;
                q = q.next;
            }
        }
        
        if (p != null) {
            prev.next = p;
        } else if (q != null) {
            prev.next = q;
        }
        
        return dummy.next;
    }
}