|
| 1 | +<h2><a href="https://leetcode.com/problems/24-game">679. 24 Game</a></h2><h3>Hard</h3><hr><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>['+', '-', '*', '/']</code> and the parentheses <code>'('</code> and <code>')'</code> to get the value 24.</p> |
| 2 | + |
| 3 | +<p>You are restricted with the following rules:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>The division operator <code>'/'</code> represents real division, not integer division. |
| 7 | + |
| 8 | + <ul> |
| 9 | + <li>For example, <code>4 / (1 - 2 / 3) = 4 / (1 / 3) = 12</code>.</li> |
| 10 | + </ul> |
| 11 | + </li> |
| 12 | + <li>Every operation done is between two numbers. In particular, we cannot use <code>'-'</code> as a unary operator. |
| 13 | + <ul> |
| 14 | + <li>For example, if <code>cards = [1, 1, 1, 1]</code>, the expression <code>"-1 - 1 - 1 - 1"</code> is <strong>not allowed</strong>.</li> |
| 15 | + </ul> |
| 16 | + </li> |
| 17 | + <li>You cannot concatenate numbers together |
| 18 | + <ul> |
| 19 | + <li>For example, if <code>cards = [1, 2, 1, 2]</code>, the expression <code>"12 + 12"</code> is not valid.</li> |
| 20 | + </ul> |
| 21 | + </li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p>Return <code>true</code> if you can get such expression that evaluates to <code>24</code>, and <code>false</code> otherwise.</p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>Input:</strong> cards = [4,1,8,7] |
| 31 | +<strong>Output:</strong> true |
| 32 | +<strong>Explanation:</strong> (8-4) * (7-1) = 24 |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong class="example">Example 2:</strong></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>Input:</strong> cards = [1,2,1,2] |
| 39 | +<strong>Output:</strong> false |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>cards.length == 4</code></li> |
| 47 | + <li><code>1 <= cards[i] <= 9</code></li> |
| 48 | +</ul> |
0 commit comments