Smarking Algorithm Contest 2

435. Non-overlapping Intervals

Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.

Note:
1. You may assume the interval’s end point is always bigger than its start point.
2. Intervals like [1,2] and [2,3] have borders “touching” but they don’t overlap each other.

给定一组区间,你可以移除一些区间让这组区间互不重叠。

做法是:
1. 首先将区间按照起点大小排序。
2. 将区间中所有完全覆盖其他区间的区间移除掉。
3. 在剩下的区间中,如果有区间,和它之前的区间有重叠,就移除它。

代码如下:

type arrI []Interval
func (a arrI)Len() int{
    return len(a)
}
func (a arrI)Less(i,j int) bool{
    if a[i].Start!=a[j].Start{
        return a[i].Start<a[j].Start
    }
    return a[i].End>=a[j].End
}

func (a arrI)Swap(i,j int){
    a[i],a[j] = a[j],a[i]
}

func eraseOverlapIntervals(intervals []Interval) int {
    if len(intervals) <= 1{
        return 0
    }
    tmp := arrI(intervals)
    sort.Sort(tmp)
    res := tmp[0]
    count :=0
    ret := []Interval{}
    for i:=0;i<len(tmp);i++{
        flag := false
        if len(ret)>0 && tmp[i].Start <=  ret[len(ret)-1].Start{
            flag = true
            count ++
        }else{
            for j:=i+1;j<len(tmp);j++{
                if tmp[i].End <= tmp[j].Start{
                    break
                }
                if tmp[i].End >= tmp[j].End{
                    flag = true
                    count ++
                    break
                }

            }
        }
        if !flag{
            ret = append(ret,tmp[i])
        }
    }
    if len(ret) == 0{
        return count
    }
    res =ret[0]
    for i:=1;i<len(ret);i++{
        if ret[i].Start >= res.End{
            res = ret[i]
        }else{
            count ++
        }
    }
    return count
}

436. Find Right Interval

Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the “right” of i.

For any interval i, you need to store the minimum interval j’s index, which means that the interval j has the minimum start point to build the “right” relationship for interval i. If the interval j doesn’t exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.

Note:
1. You may assume the interval’s end point is always bigger than its start point.
2. You may assume none of these intervals have the same start point.
给定一组区间,对于每一个区间,如果有其他区间的起点大于等于这个区间的终点,那么就称这个区间有右区间。找出最近的右区间的下标。没有右区间则该区间的最小右区间为-1。

这题直接用o(n*n)的算法就能过。即一个个找右区间。

func findRightInterval(intervals []Interval) []int {
    ret := make([]int,len(intervals))
    for i:=0;i<len(intervals);i++{
        min := i
        for j:=0;j<len(intervals);j++{
            if j == i{
                continue
            }
            if intervals[j].Start >= intervals[i].End{
                if min == i || intervals[j].Start < intervals[min].Start{
                     min = j
                }
            }
        }
        if min == i{
            min = -1
        }
        ret[i] = min
    }
    return ret
}

441. Arranging Coins

You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.

Given n, find the total number of full staircase rows that can be formed.

n is a non-negative integer and fits within the range of a 32-bit signed integer.

水题,没啥好说的。

func arrangeCoins(n int) int {
    idx  :=1;
    for (n>=idx){
        n -=idx
        idx ++
    }
    return idx-1
}

444. Sequence Reconstruction

Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 10^4. Reconstruction means building a shortest common supersequence of the sequences in seqs (i.e., a shortest sequence so that all sequences in seqs are subsequences of it). Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence.

link
没做出来,贴个别人的代码。

class Solution {
public:
    bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {
        int n = org.size();
        vector<int> to(n), in(n);
        vector<bool> vis(n);
        vector<vector<int>> adj(n);
        int k = 0;
        for (auto &x : org) {
            x--;
            to[x] = k++;
        }
        for (auto &vec : seqs) {
            for (auto &c : vec) {
                c--;
                if (c < 0 || c >= n) {
                    return false;
                }
                vis[to[c]] = true;
            }
            for (int i = 1; i < vec.size(); i++) {
                auto x = to[vec[i - 1]], y = to[vec[i]];
                adj[x].push_back(y);
                in[y]++;
                if (x > y) {
                    return false;
                }
            }
        }
        queue<int> q;
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                return false;
            }
            if (!in[i]) {
                q.push(i);
            }
        }
        vector<int> sorted;
        while (!q.empty()) {
            if (q.size() > 1) {
                return false;
            }
            auto s = q.front();
            q.pop();
            sorted.push_back(s);
            for (auto v : adj[s]) {
                if (!--in[v]) {
                    q.push(v);
                }
            }
        }
        for (int i = 0; i < n; i++) {
            if (i != sorted[i]) {
                return false;
            }
        }
        return true;
    }
};
























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值