Weever's Road

Just another WordPress site

Category: hard

287. Find the Duplicate Number (Hard??)

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:
You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
 public int findDuplicate(int[] nums) {
        if(nums == null)
            return -1;
        boolean[] check = new boolean[nums.length];
        for(int i=0 ; i< nums.length; i++){
            if(check[nums[i]])
                return nums[i];
            else
                check[nums[i]] = true;
        }
        return -1;
    }

56. Merge Intervals (hard)

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

Solution:
1. If the given list is sort by start.
2. Check the current intervals is overlap or inside the previous one.
if true: combine the overlap intervals, go next interval
if false: no overlap, save the previous one, set current, go next interval

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */

public class Solution {
    public List<Interval> merge(List<Interval> intervals) {
        if(intervals == null || intervals.size() == 0)
            return intervals;
         Collections.sort(intervals, new Comparator<Interval>() {
            public int compare(Interval o1, Interval o2) {
                return o1.start - o2.start;
            }
        });
        List<Interval> result = new ArrayList<>();
        Interval current = intervals.get(0);
        for(int i = 1; i < intervals.size(); i++){
            if(intervals.get(i).end <= current.end){
                 current = intervals.get(i);
            }
            else if( intervals.get(i).start <= current.end){
                current = new Interval(current.start,intervals.get(i).end);
            } else {
                result.add(current);
                current = intervals.get(i);
            }
        }
        result.add(current);
        return result;
    }
}

InterviewBit solution:
1. assume the List is sorted, insert the new element in the list
2. Do the logic above

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
        if (newInterval.end  < newInterval.start)
            newInterval = new Interval(newInterval.end, newInterval.start);
        ArrayList<Interval> result = new ArrayList();
        if(intervals == null || intervals.size() == 0){
            result.add(newInterval);
            return result;
        }
        int i = 0;
        while(i < intervals.size() && newInterval.start > intervals.get(i).start)
            i++;
        if(i == intervals.size())
            intervals.add(newInterval);
        else
            intervals.add(i,newInterval);
       
        Interval current = intervals.get(0);
        for(int j=1;j<intervals.size();j++){
            if(intervals.get(j).start <= current.end)
                current = new Interval(current.start, Math.max(current.end,intervals.get(j).end));
            else{
                result.add(current);
                current = intervals.get(j);
            }
        }
        result.add(current);
        return result;
    }

Powered by WordPress & Theme by Anders Norén