Permutation + Backtracking
Intuition\nTo solve this problem, we need to check if it\'s possible to combine the given numbers in such a way that they result in 24. We can achieve this by t
# Intuition\nTo solve this problem, we need to check if it\'s possible to combine the given numbers in such a way that they result in 24. We can achieve this by trying out all possible permutations of the numbers and applying mathematical operations to them. If any permutation yields 24, we return true; otherwise, we return false.\n\n# Approach\nWe\'ll define two functions: `valid` to check if given numbers can result in 24 when combined, and `get_perm` to generate all permutations of the given numbers and check if any permutation satisfies the condition.\n\nIn the `valid` function, we\'ll check if the given numbers can result in 24 by performing addition, subtraction, multiplication, and division while considering floating-point precision.\n\nIn the `get_perm` function, we\'ll generate all permutations of the given numbers using backtracking. We\'ll swap numbers at different positions to explore all possible combinations. Once we find a permutation that results in 24, we\'ll return true; otherwise, we\'ll return false.\n\n# Complexity\n- Time complexity: O(1) for the `valid` function as it performs a constant number of operations. For the `get_perm` function, it depends on the number of permutations, which is at most 4!, so it\'s also constant.\n- Space complexity: O(1) for the `valid` function as it uses constant space. For the `get_perm` function, it uses a set to track seen numbers during backtracking, which can have at most 4 elements, so it\'s also constant.\n\n# Code\n```cpp\nclass Solution {\npublic:\n    bool valid(double a, double b) {\n        return fabs(a + b - 24.0) < 0.0001 or fabs(a - b - 24.0) < 0.0001 or\n               fabs(a * b - 24.0) < 0.0001 or (b != 0 and fabs(a / b - 24.0) < 0.0001);\n    }\n    \n    bool valid(double a, double b, double c) {\n        return valid(a + b, c) or valid(a, b + c) or valid(a - b, c) or\n               valid(a, b - c) or valid(a * b, c) or valid(a, b * c) or\n               valid(a / b, c) or valid(a, b / c);\n    }\n    \n    bool get_perm(int idx, vector<double>& cards) {\n        if (idx == 4) {\n            return valid(cards[0] + cards[1], cards[2], cards[3]) or\n                   valid(cards[0], cards[1] + cards[2], cards[3]) or\n                   valid(cards[0], cards[1], cards[2] + cards[3]) or\n                   valid(cards[0] - cards[1], cards[2], cards[3]) or\n                   valid(cards[0], cards[1] - cards[2], cards[3]) or\n                   valid(cards[0], cards[1], cards[2] - cards[3]) or\n                   valid(cards[0] * cards[1], cards[2], cards[3]) or\n                   valid(cards[0], cards[1] * cards[2], cards[3]) or\n                   valid(cards[0], cards[1], cards[2] * cards[3]) or\n                   valid(cards[0] / cards[1], cards[2], cards[3]) or\n                   valid(cards[0], cards[1] / cards[2], cards[3]) or\n                   valid(cards[0], cards[1], cards[2] / cards[3]);\n        }\n        \n        bool res = false;\n        unordered_set<int> s;\n        for (int i = idx; i < 4 and !res; i++) {\n            if (s.count(cards[i]))\n                continue;\n            s.insert(cards[i]);\n            swap(cards[idx], cards[i]);\n            res |= get_perm(idx + 1, cards);\n            swap(cards[idx], cards[i]);\n        }\n        return res;\n    }\n    \n    bool judgePoint24(vector<int>& nums) {\n        vector<double> cards;\n        for (auto& x : nums)\n            cards.push_back((double)x);\n        return get_perm(0, cards);\n    }\n};\n```
['Array', 'Math', 'Backtracking', 'C++']