<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p>

<ul>
	<li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li>
	<li>The first integer is <code>0</code>,</li>
	<li>An integer appears <strong>no more than once</strong> in the sequence,</li>
	<li>The binary representation of every pair of <strong>adjacent</strong> integers differs by <strong>exactly one bit</strong>, and</li>
	<li>The binary representation of the <strong>first</strong> and <strong>last</strong> integers differs by <strong>exactly one bit</strong>.</li>
</ul>

<p>Given an integer <code>n</code>, return <em>any valid <strong>n-bit gray code sequence</strong></em>.</p>

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

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> [0,1,3,2]
<strong>Explanation:</strong>
The binary representation of [0,1,3,2] is [00,01,11,10].
- 0<u>0</u> and 0<u>1</u> differ by one bit
- <u>0</u>1 and <u>1</u>1 differ by one bit
- 1<u>1</u> and 1<u>0</u> differ by one bit
- <u>1</u>0 and <u>0</u>0 differ by one bit
[0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01].
- <u>0</u>0 and <u>1</u>0 differ by one bit
- 1<u>0</u> and 1<u>1</u> differ by one bit
- <u>1</u>1 and <u>0</u>1 differ by one bit
- 0<u>1</u> and 0<u>0</u> differ by one bit
</pre>

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

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> [0,1]
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 16</code></li>
</ul>

"""
# The binary representation of the first and last integers differs by exactly one bit.
# and
# The first integer is 0
# so
# the last integer is a power of 2
"""
class Solution:
    def grayCode(self, n: int) -> List[int]:
        seq = [0]
        visited = set([0])

        def dfs():
            # print(seq)
            if len(seq) == 2**n:
                return True
            
            for i in range(n):
                # wheter the integer differs by exactly one bit (from last inserted num)
                if seq[-1] & 2**i:
                    # already set, remove that bit
                    next_num = seq[-1] - 2**i # improve this
                else:
                    next_num = seq[-1] | 2**i
                if next_num not in visited:
                    seq.append(next_num)
                    visited.add(next_num)
                    if dfs():
                        # found
                        return True
                    visited.remove(next_num)
                    seq.pop()

            return False

        dfs()
        return seq
    
    def _differ_by_exactly_one_bit(self, int1, int2):
        c = 0

        for i in range(16):
            if int1 & 2**i != int2 & 2**i:
                c += 1
        
        return c == 1
        # find difference bit(s)
        # check if difference is power of 2


"""
000000 -> 
"""

# [0,1,3,2,6,7,5,4,12,13,15,14,10,11,9,8]
# [0,1,3,2,6,7,5,4,12,13,15,14,10,11,9,8,24,25,27,26,30,31,29,28,20,21,23,22,18,19,17,16,48,49,51,50,54,55,53,52,60,61,63,62,58,59,57,56,40,41,43,42,46,47,45,44,36,37,39,38,34,35,33,32]
34800