Shu-How Zの小窝

Loading...

990.等式方程的可满足性

990.等式方程的可满足性

1:40…

/等式的可传递性;小写zASCII=97;

class UnionFind {
    // 构造函数初始化并查集
    constructor(n) {
        this.parent = new Array(n).fill(0).map((item, index) => index);
        this.rank = new Array(n).fill(1);
        this.count = n;
    }

    // 查找元素的根节点,并进行路径压缩
    find(x) {
        if (this.parent[x] === x) {
            return x;
        }
        this.parent[x] = this.find(this.parent[x]);
        return this.parent[x];
    }

    // 合并两个集合,按秩合并优化
    union(x, y) {
        const rootX = this.find(x);
        const rootY = this.find(y);

        if (rootX === rootY) {
            return; // 已经在同一个集合中
        }

        if (this.rank[rootX] < this.rank[rootY]) {
            this.parent[rootX] = rootY;
        } else if (this.rank[rootX] > this.rank[rootY]) {
            this.parent[rootY] = rootX;
        } else {
            this.parent[rootY] = rootX;
            this.rank[rootX]++;
        }
        this.count--; // 合并后集合数量减少
    }
};

/**
 * @param {string[]} equations
 * @return {boolean}
 */
var equationsPossible = function(equations) {
    const uf = new UnionFind(26); // 26个小写字母

    // 先处理所有等式
    for (const eq of equations) {
        if (eq[1] === '=') {
            const x = eq.charCodeAt(0) - 'a'.charCodeAt(0);
            const y = eq.charCodeAt(3) - 'a'.charCodeAt(0);
            uf.union(x, y);
        }
    }

    // 再处理所有不等式
    for (const eq of equations) {
        if (eq[1] === '!') {
            const x = eq.charCodeAt(0) - 'a'.charCodeAt(0);
            const y = eq.charCodeAt(3) - 'a'.charCodeAt(0);
            if (uf.find(x) === uf.find(y)) {
                return false; // 如果不等式两边相等,则不满足
            }
        }
    }

    return true; // 所有条件都满足
};



console.log(equationsPossible(["a==b","b!=a"])); // 输出 false


//dfs
/**
 * @param {string[]} equations
 * @return {boolean}
 */
var equationsPossible = function(equations) {
    const graph = {}; // 图的邻接表表示

    // 初始化图
    for (const eq of equations) {
        if (eq[1] === '=') {
            const var1 = eq[0];
            const var2 = eq[3];
            if (!graph[var1]) graph[var1] = [];
            if (!graph[var2]) graph[var2] = [];
            graph[var1].push(var2);
            graph[var2].push(var1);
        }
    }

    // DFS查找函数
    function find(visited, begin, end) {
        if (visited.has(begin)) {
            return begin === end;
        }
        visited.add(begin);
        if (begin === end) {
            return true;
        }
        for (const neighbor of graph[begin] || []) {
            if (find(visited, neighbor, end)) {
                return true;
            }
        }
        return false;
    }

    // 检查不等式
    for (const eq of equations) {
        if (eq[1] === '!') {
            const var1 = eq[0];
            const var2 = eq[3];
            if (find(new Set(), var1, var2)) {
                return false;
            }
        }
    }

    return true;
};

let arr = ["c==c", "b==d", "x!=z"];
console.log(equationsPossible(arr)); // 应该输出 true

6

posted @ 2025-03-01 18:38  KooTeam  阅读(10)  评论(0)    收藏  举报