<p>You are given an integer array <code>cards</code> of length <code>4</code>. You have four cards, each containing a number in the range <code>[1, 9]</code>. You should arrange the numbers on these cards in a mathematical expression using the operators <code>[&#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;]</code> and the parentheses <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code> to get the value 24.</p>

<p>You are restricted with the following rules:</p>

<ul>
	<li>The division operator <code>&#39;/&#39;</code> represents real division, not integer division.

	<ul>
		<li>For example, <code>4 / (1 - 2 / 3) = 4 / (1 / 3) = 12</code>.</li>
	</ul>
	</li>
	<li>Every operation done is between two numbers. In particular, we cannot use <code>&#39;-&#39;</code> as a unary operator.
	<ul>
		<li>For example, if <code>cards = [1, 1, 1, 1]</code>, the expression <code>&quot;-1 - 1 - 1 - 1&quot;</code> is <strong>not allowed</strong>.</li>
	</ul>
	</li>
	<li>You cannot concatenate numbers together
	<ul>
		<li>For example, if <code>cards = [1, 2, 1, 2]</code>, the expression <code>&quot;12 + 12&quot;</code> is not valid.</li>
	</ul>
	</li>
</ul>

<p>Return <code>true</code> if you can get such expression that evaluates to <code>24</code>, and <code>false</code> otherwise.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> cards = [4,1,8,7]
<strong>Output:</strong> true
<strong>Explanation:</strong> (8-4) * (7-1) = 24
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> cards = [1,2,1,2]
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>cards.length == 4</code></li>
	<li><code>1 &lt;= cards[i] &lt;= 9</code></li>
</ul>

class Solution {
    unordered_map<int, unordered_set<float>> checked;
    unordered_set<float> helper(vector<int>& cards){
        int key=0;
        for(auto c : cards){
            key = key*10+c;
        }
        
        if (checked.find(key)!=checked.end()){
            return checked[key];
        }

        if (cards.size()==1){
            checked[key]={float(cards[0])};
        }
        else if(cards.size()==2){
            checked[key] = {float(cards[0] + cards[1])};
            checked[key].insert(float(cards[0]-cards[1]));
            checked[key].insert(float(cards[1]-cards[0]));
            checked[key].insert(float(cards[0])/float(cards[1]));
            checked[key].insert(float(cards[1])/float(cards[0]));
            checked[key].insert(float(cards[1]*cards[0]));
            
        }
        else if(cards.size()==3){
            checked[key] = {};
            for (int i=0; i<3; i++){
                vector<int> tmp;
                if(i>0 && cards[i]==cards[i-1]){
                    continue;
                }
                if (i==0){
                    tmp = {cards[1], cards[2]};
                }
                else if(i==1){
                    tmp = {cards[0], cards[2]};
                }
                else{
                    tmp = {cards[0], cards[1]};
                }
                unordered_set<float> v = helper(tmp);
                for (auto vv : v){
                    checked[key].insert(float(cards[i])+vv);
                    checked[key].insert(float(cards[i])-vv);
                    checked[key].insert(float(cards[i])*vv);
                    checked[key].insert(float(cards[i])/vv);
                    checked[key].insert(vv-float(cards[i]));
                    checked[key].insert(vv/float(cards[i]));
                   
                }
                
            }
        
            
        }
        return checked[key];
    }
public:
    bool judgePoint24(vector<int>& cards) {
        sort(cards.begin(), cards.end());
        for(int i=0; i<4; i++){
            if(i>0 && cards[i]==cards[i-1]){
                continue;
            }
            vector<int> tmp;
            if(i==0){
                tmp = {cards[1], cards[2], cards[3]};
            }
            else if(i==1){
                tmp =  {cards[0], cards[2], cards[3]};
            }
            else if(i==2){
                tmp =  {cards[0], cards[1], cards[3]};
            }
            else if(i==3){
                tmp =  {cards[0], cards[1], cards[2]};
            }
            unordered_set<float> v = helper(tmp);
            for(auto vv : v){
                if (float(cards[i])+vv == 24)
                    return true;
                if (float(cards[i])-vv == 24)
                    return true;
                if (float(cards[i])*vv == 24)
                    return true;
                if (float(cards[i])/vv >= 23.999 && float(cards[i])/vv <= 24.001)
                    return true;
                if (float(vv-cards[i]) == 24)
                    return true;
                if (float(vv/cards[i]) == 24)
                    return true;
            }
        }
        for (int i=1; i<4; i++){
            if (i>1 && cards[i]==cards[i-1]){
                continue;
            }
            vector<int> tmp1={cards[0], cards[i]};
            vector<int> tmp2;
            if (i==1){
                tmp2 = {cards[2], cards[3]};
            }
            else if(i==2){
                tmp2 = {cards[1], cards[3]};
            }
            else{
                tmp2 = {cards[1], cards[2]};
            }
            unordered_set<float> v = helper(tmp1);
            unordered_set<float> w = helper(tmp2);
            for(auto vv : v){
                for(auto ww : w){
                    if (vv+ ww == 24)
                        return true;
                    if (vv-ww == 24)
                        return true;
                    if (vv*ww == 24)
                        return true;
                    if (vv/ww==24)
                        return true;
                    if (ww-vv==24)
                        return true;
                    if(ww/vv==24)
                        return true;
                }
            }
        }
        return false;
    }
};
13453