2.30

1 我的日程安排表 I

https://leetcode-cn.com/problems/my-calendar-i/

 思路:

插入每个区间的时候,只需要看两点
(1)start的上一个区间的结尾是否超过了start
(2)start的下一个区间的开头是否被end超过

 java 和c++代码

class MyCalendar {
    private TreeMap<Integer,Integer> tree ;
    public MyCalendar() {
        this.tree = new TreeMap<>();
    }

    public boolean book(int start, int end) {
        if(start>=end) return false; //时间不合法
        Integer floorKey = tree.floorKey(start);//找到小于等于start的最大键
        Integer ceilingKey = tree.ceilingKey(start);//找到大于start的最小键
        if(floorKey!=null&&tree.get(floorKey)>start) return false;
        if(ceilingKey!=null&&ceilingKey<end) return false;
        tree.put(start,end);
        return true;
    }
}
typedef pair<int, int> PII;
const int INF = 2e9;

class MyCalendar {
public:
    set<PII> S;

    MyCalendar() {
        S.insert({-INF, -INF});
        S.insert({INF, INF});
    }

    bool check(PII a, PII b) {
        if (a.second <= b.first || b.second <= a.first) return false;
        return true;
    }

    bool book(int start, int end) {
        auto i = S.lower_bound({start, -INF});
        auto j = i;
        j -- ;
        PII t(start, end);
        if (check(*i, t) || check(*j, t)) return false;
        S.insert(t);
        return true;
    }
};

2 统计不同回文子序列

 思路:参考acwing王子辰https://www.acwing.com/solution/content/4105/

 java和c++代码

class Solution {

    public int countPalindromicSubsequences(String S) {
        int n = S.length();
        int mod = (int)1e9+7;
        int[][] dp = new int[n+5][n+5];
        for (int[] d : dp) {
            Arrays.fill(d, 1);
        }
        for(int i=1;i<=n;i++) dp[i][i] = 2;
        for(int len=2;len<=n;len++){
            Deque<Integer>[] queue = new Deque[4];
            for(int i=0;i<4;i++) queue[i] = new LinkedList<>();
            for(int i=1;i<=n;i++){
                queue[S.charAt(i-1)-'a'].add(i); //将当前位置加入队列
                int j = i-len+1;
                if(j>=1){
                    for(int k=0;k<4;k++){
                        while (queue[k].size()>0&&queue[k].peekFirst()<j) queue[k].pollFirst();
                        if(queue[k].size()>0){
                            dp[j][i] ++;
                            int l = queue[k].peekFirst();
                            int r = queue[k].peekLast();
                            if(l<r) dp[j][i] = (dp[j][i]+dp[l+1][r-1])%mod;
                        }
                    }
                }
            }
        }
        return (dp[1][n]+mod-1)%mod;
    }
}
class Solution {
public:
    int countPalindromicSubsequences(string s) {
        int n = s.size(), MOD = 1e9 + 7;
        vector<vector<int>> f(n + 2, vector<int>(n + 2, 1));
        for (int i = 1; i <= n; i ++ ) f[i][i] ++ ;
        for (int len = 2; len <= n; len ++ ) {
            deque<int> q[4];
            for (int i = 1; i <= n; i ++ ) {
                q[s[i - 1] - 'a'].push_back(i);
                int j = i - len + 1;
                if (j >= 1) {
                    for (int k = 0; k < 4; k ++ ) {
                        while (q[k].size() && q[k].front() < j) q[k].pop_front();
                        if (q[k].size()) {
                            f[j][i] ++ ;
                            int l = q[k].front(), r = q[k].back();
                            if (l < r)
                                f[j][i] = (f[j][i] + f[l + 1][r - 1]) % MOD;
                        }
                    }
                }
            }
        }
        return (f[1][n] + MOD - 1) % MOD;
    }
};

 

posted @ 2021-03-30 18:07  键盘_书生  阅读(73)  评论(0编辑  收藏  举报