<\/span><\/h2>\n\n\n\nThere is a horizontal row of n<\/em><\/strong> cubes. The length of each cube is given. You need to create a new vertical pile of cubes. The new pile should follow these directions: if cube<\/em>[i<\/em>]<\/strong> is on top of cube<\/em>[j<\/em>]<\/strong> then sideLength<\/em>|j<\/em>| => sideLength<\/em>|i<\/em>|<\/strong>.<\/p>\n\n\n\nWhen stacking the cubes, you can only pick up either the leftmost or the rightmost cube each time. Print Yes<\/code> if it is possible to stack the cubes. Otherwise, print No<\/code>.<\/p>\n\n\n\nExample<\/strong><\/p>\n\n\n\nblocks = [1, 2, 3, 8, 7]<\/strong><\/p>\n\n\n\nResult: No<\/code><\/p>\n\n\n\nAfter choosing the rightmost element, 7<\/strong>, choose the leftmost element, 1<\/strong>. After than, the choices are 2<\/strong> and 8<\/strong>. These are both larger than the top block of size 1<\/strong>.<\/p>\n\n\n\nblocks <\/em>= [1, 2, 3, 7, 8]<\/strong><\/p>\n\n\n\nResult: Yes<\/code><\/p>\n\n\n\nChoose blocks from right to left in order to successfully stack the blocks.<\/p>\n\n\n\n
<\/span>Input Format <\/strong><\/span><\/h2>\n\n\n\nThe first line contains a single integer T<\/strong><\/em>, the number of test cases.
For each test case, there are 2<\/strong> lines.
The first line of each test case contains n<\/em><\/strong>, the number of cubes.
The second line contains n<\/em><\/strong> space separated integers, denoting the sideLengths<\/em> of each cube in that order.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= T<\/em> <= 5<\/strong><\/li>
- 1 <= n<\/em> <= 105<\/sup><\/strong><\/li>
- 1 <= <\/strong>sideLength <\/strong><\/em>< 231<\/sup><\/strong><\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nFor each test case, output a single line containing either Yes<\/code> or No<\/code>.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\nSTDIN Function\n----- --------\n2 T = 2\n6 blocks[] size n = 6\n4 3 2 1 3 4 blocks = [4, 3, 2, 1, 3, 4]\n3 blocks[] size n = 3\n1 3 2 blocks = [1, 3, 2]<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\nYes\nNo<\/code><\/pre>\n\n\n\nExplanation<\/strong><\/p>\n\n\n\nIn the first test case, pick in this order: left – 4, right – 4, left – 3, right – 3, left – 2, right – 1<\/strong>.
In the second test case, no order gives an appropriate arrangement of vertical cubes. 3<\/strong> will always come after either 1<\/strong> or 2<\/strong>.<\/p>\n\n\n\n<\/span>Solution – Piling Up! in Python <\/strong><\/span><\/h2>\n\n\n\n# Enter your code here. Read input from STDIN. Print output to STDOUT\nANS = []\nT = int(input())\n\nfor _ in range(T):\n n = int(input())\n sl = list(map(int, input().split()))\n\n for _ in range(n-1):\n if sl[0] >= sl[len(sl)-1]:\n a = sl[0]\n sl.pop(0)\n elif sl[0] < sl[len(sl)-1]:\n a = sl[len(sl)-1]\n sl.pop(len(sl)-1)\n else:\n pass\n\n if len(sl) == 1:\n ANS.append(\"Yes\")\n\n if((sl[0] > a) or (sl[len(sl)-1] > a)):\n ANS.append(\"No\")\n break\n\nprint(\"\\n\".join(ANS))<\/pre>\n\n\n\n