“21 天好习惯”第一期-18

目录

离散:

概率论知识点:

leetcode每日一题:


离散:

续sevent:

        对 自反 , 反自反 , 对称 , 反对称 , 传递 的明白了当的解释:

                自反: I_{a}\subseteq R.I_{a} 恒等关系. \forall x\in A \wedge <x,x>

                反自反: R\cap I_{a} = \varnothing.

                对称: R = R^{-1}.

                反对称: R\cap R^{-1}\subseteq I_{a}. ( \O \in I_{a}.

                传递: R\circ R\subseteq R. \circ 是右复合. 结果 \rightarrow <x,y> \leftarrow <x,t>\wedge <t,y>.

概率论知识点:

第一章:

        频率与概率:

                频率: 1) 非负. 2)规范. 3)可加.

                *频率在基数大时 , 无限接近于 P (概率.

                概率: 事件发生内在的因素.

        公理化: 1)描述. 2)古典. 3)几何. 4)统计.

                性质: 同样的 1) 非负. 2)规范. 3)可加.

                        a. P(\O )=0.

                        b. P(A_{1} + A_{2} + A_{3}...+ A_{n}) = P(A_{1}) + P(A_{2}) + P(A_{3})...+ P(A_{n}).

                        c. P(\bar{A}) = 1 - P(A).

                        d.  P(A - B) = P(A) - P(AB). *推论: A = (A-B)\cup AB.

                        e. (加法) P(A + B) = P(A) + P(B) - P(AB). *推论: P(A+B+C) = P(A) + P(B) `+P(C) - P(AB) - P(AC) -P(BC) +P(ABC) .

                        *可对无限个事件满足.

        几何概型: 概率为 0 , 也有可能发生.

        条件概率: P(A|B) \rightarrow 在B已经发生的概率下 , A发生的概率.

                \left\{\begin{matrix} P(A) & & \\ P(A|B) & & \end{matrix}\right. 对比: P(A): 无条件概率 \rightarrow 样本空间为R. P(A|B): 条件概率 \rightarrow 样本空间为B.

                计算公式: 1) \frac{P(AB)}{P(B)}. 2)\frac{n_{AB}}{n_{B}}.

                乘法公式: P(AB) = P(A)P(B|A) = P(B)P(A|B).

                *推论: P(ABC) = P(A)P(B|A)P(C|AB).

                贝叶斯公式: 知果推因.

                事件的独立性 \Leftrightarrow P(AB) = P(A)P(B). 定义: A的概率不被B影响.

leetcode每日一题:

次元门

不会写orz. 让我水过去吧.

struct State{
    string board;
    string hand;
    int step;
    State(const string & board, const string & hand, int step) {
        this->board = board;
        this->hand = hand;
        this->step = step;
    }
};

class Solution {
public:
    string clean(const string & str){
        string res;
        vector<pair<char,int>> st;
        
        for (auto c : str) {
            while (!st.empty() && c != st.back().first && st.back().second >= 3) {
                st.pop_back();
            }
            if (st.empty() || c != st.back().first) {
                st.push_back({c,1});
            } else {
                st.back().second++;
            }
        }
        if (!st.empty() && st.back().second >= 3) {
            st.pop_back();
        }
        for (int i = 0; i < st.size(); ++i) {
            for (int j = 0; j < st[i].second; ++j) {
                res.push_back(st[i].first);
            }
        }
        return res;
    }

    int findMinStep(string board, string hand) {
        unordered_set<string> visited;
        sort(hand.begin(), hand.end());

        visited.insert(board + " " + hand);
        queue<State> qu;
        qu.push(State(board, hand, 0));
        while (!qu.empty()) {
            State curr = qu.front();
            qu.pop();

            for (int j = 0; j < curr.hand.size(); ++j) {
                // 第 1 个剪枝条件: 当前选择的球的颜色和前一个球的颜色相同
                if (j > 0 && curr.hand[j] == curr.hand[j - 1]) {
                    continue;
                }
                for (int i = 0; i <= curr.board.size(); ++i) {
                    // 第 2 个剪枝条件: 只在连续相同颜色的球的开头位置插入新球
                    if (i > 0 && curr.board[i - 1] == curr.hand[j]) {
                        continue;
                    }

                    // 第 3 个剪枝条件: 只在以下两种情况放置新球
                    bool choose = false;
                    //   第 1 种情况 : 当前球颜色与后面的球的颜色相同
                    if (i < curr.board.size() && curr.board[i] == curr.hand[j]) {
                        choose = true;
                    }  
                    //   第 2 种情况 : 当前后颜色相同且与当前颜色不同时候放置球
                    if (i > 0 && i < curr.board.size() && curr.board[i - 1] == curr.board[i] && curr.board[i] != curr.hand[j]){
                        choose = true;
                    }
                    if (choose) {
                        string new_board = clean(curr.board.substr(0,i) + curr.hand[j] + curr.board.substr(i));
                        string new_hand = curr.hand.substr(0,j) + curr.hand.substr(j + 1);
                        if (new_board.size() == 0) {
                            return curr.step + 1;
                        }
                        if (!visited.count(new_board + " " + new_hand)) {
                            qu.push(State(new_board, new_hand, curr.step + 1));
                            visited.insert(new_board + " " + new_hand);
                        }
                    }
                }
            }
        }

        return -1;  
    }
};

// 作者:LeetCode-Solution
// 链接:https://leetcode-cn.com/problems/zuma-game/solution/zu-ma-you-xi-by-leetcode-solution-lrp4/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

一八年夏天的蝉 , 便以为抓住了整个夏天.Day Eighteen -- 一八年夏至.

猜你喜欢

转载自blog.csdn.net/EX_fish/article/details/121239403