FPEvalRepoPublic/LeetCodeMetaData
0399
1{2 "id": 3783,3 "name": "permutations-iv",4 "difficulty": "Hard",5 "link": "https://leetcode.com/problems/permutations-iv/",6 "date": "2025-02-15",7 "task_description": "Given two integers, `n` and `k`, an **alternating permutation** is a permutation of the first `n` positive integers such that no **two** adjacent elements are both odd or both even. Return the **k-th** **alternating permutation** sorted in _lexicographical order_. If there are fewer than `k` valid **alternating permutations**, return an empty list. **Example 1:** **Input:** n = 4, k = 6 **Output:** [3,4,1,2] **Explanation:** The lexicographically-sorted alternating permutations of `[1, 2, 3, 4]` are: `[1, 2, 3, 4]` `[1, 4, 3, 2]` `[2, 1, 4, 3]` `[2, 3, 4, 1]` `[3, 2, 1, 4]` `[3, 4, 1, 2]` ← 6th permutation `[4, 1, 2, 3]` `[4, 3, 2, 1]` Since `k = 6`, we return `[3, 4, 1, 2]`. **Example 2:** **Input:** n = 3, k = 2 **Output:** [3,2,1] **Explanation:** The lexicographically-sorted alternating permutations of `[1, 2, 3]` are: `[1, 2, 3]` `[3, 2, 1]` ← 2nd permutation Since `k = 2`, we return `[3, 2, 1]`. **Example 3:** **Input:** n = 2, k = 3 **Output:** [] **Explanation:** The lexicographically-sorted alternating permutations of `[1, 2]` are: `[1, 2]` `[2, 1]` There are only 2 alternating permutations, but `k = 3`, which is out of range. Thus, we return an empty list `[]`. **Constraints:** `1 <= n <= 100` `1 <= k <= 1015`",8 "test_case": [9 {10 "label": "Example 1",11 "input": "n = 4, k = 6",12 "output": "[3,4,1,2] "13 },14 {15 "label": "Example 2",16 "input": "n = 3, k = 2",17 "output": "[3,2,1] "18 },19 {20 "label": "Example 3",21 "input": "n = 2, k = 3",22 "output": "[] "23 }24 ],25 "constraints": [26 "1 <= n <= 100",27 "1 <= k <= 1015"28 ],29 "python_template": "class Solution(object):\n def permute(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: List[int]\n \"\"\"\n ",30 "java_template": "class Solution {\n public int[] permute(int n, long k) {\n \n }\n}",31 "metadata": {32 "func_name": "permute"33 }34}