question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count-nodes-with-the-highest-score | Python3: clear and simple DFS solution | python3-clear-and-simple-dfs-solution-by-qatx | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | sleepingbear | NORMAL | 2025-03-04T18:08:56.684076+00:00 | 2025-03-04T18:08:56.684076+00:00 | 7 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
count-nodes-with-the-highest-score | [Java] Topology order to calculate sub tree size | java-topology-order-to-calculate-sub-tre-mrgv | IntuitionUse topology order to calculate sub tree size for each nodeComplexity
Time complexity: O(N)
Space complexity: O(N)
Code | joyforce | NORMAL | 2025-02-23T01:22:57.735015+00:00 | 2025-02-23T01:22:57.735015+00:00 | 15 | false | # Intuition
Use topology order to calculate sub tree size for each node
# Complexity
- Time complexity: O(N)
- Space complexity: O(N)
# Code
```java []
class Solution {
public int countHighestScoreNodes(int[] parents) {
// topology order to calculate sub tree size
int n = parents.length;
... | 0 | 0 | ['Java'] | 0 |
count-nodes-with-the-highest-score | Adjacency List | DFS | Binary Tree | Hash Map | Easy Solution | adjacency-list-dfs-binary-tree-hash-map-xd5l3 | IntuitionTo find the size of sub-forests connected to given node. Sub-forests formed by children can be found using dfs traversal and sub-forest formed by remai | mynk_mishra | NORMAL | 2025-02-08T11:55:59.171329+00:00 | 2025-02-08T11:55:59.171329+00:00 | 16 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
To find the size of sub-forests connected to given node. Sub-forests formed by children can be found using dfs traversal and sub-forest formed by remaining is (size of total tree) - (size of all children sub-forest) - 1(for current node).
... | 0 | 0 | ['Array', 'Hash Table', 'Tree', 'Depth-First Search', 'Binary Tree', 'Java'] | 0 |
count-nodes-with-the-highest-score | Easy to read and understand solution. O(n) time and space complexity | easy-to-read-and-understand-solution-on-8lq3z | IntuitionWe need to calculate sum of subtrees on every node, than we can find out how many nodes will be in the tree without any one.ApproachBuild a map of node | vladpron2016 | NORMAL | 2025-02-06T18:54:48.544301+00:00 | 2025-02-06T18:54:48.544301+00:00 | 16 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We need to calculate sum of subtrees on every node, than we can find out how many nodes will be in the tree without any one.
# Approach
<!-- Describe your approach to solving the problem. -->
Build a map of nodes, than calculcate number of ... | 0 | 0 | ['Java'] | 0 |
count-nodes-with-the-highest-score | 2049. Count Nodes With the Highest Score | 2049-count-nodes-with-the-highest-score-q9jck | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | G8xd0QPqTy | NORMAL | 2025-01-17T14:34:50.206454+00:00 | 2025-01-17T14:34:50.206454+00:00 | 10 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
count-nodes-with-the-highest-score | Beats 100% ---> Simple Approach | beats-100-simple-approach-by-vatan999-oj21 | IntuitionThe goal is to calculate the number of nodes in the tree whose removal maximizes the product of the sizes of its disconnected components. This requires | vatan999 | NORMAL | 2025-01-17T07:03:43.894808+00:00 | 2025-01-17T07:03:43.894808+00:00 | 10 | false | # Intuition
The goal is to calculate the number of nodes in the tree whose removal maximizes the product of the sizes of its disconnected components. This requires traversing the tree, computing the product for each node's removal, and determining the maximum product and its frequency.
# Approach
1. **Tree Constructio... | 0 | 0 | ['C++'] | 0 |
count-nodes-with-the-highest-score | DFS | dfs-by-up41guy-goqr | null | Cx1z0 | NORMAL | 2025-01-15T08:16:20.813363+00:00 | 2025-01-15T08:16:20.813363+00:00 | 5 | false | ```javascript []
/**
* @param {number[]} parents
* @return {number}
*/
const countHighestScoreNodes = function (parents) {
const tree = Array.from({ length: parents.length }, () => []);
for (let i = 1; i < parents.length; i++) {
tree[parents[i]].push(i);
}
let maxScore = 0, count = 0;
const dfs = (n... | 0 | 0 | ['Array', 'Tree', 'Depth-First Search', 'Binary Tree', 'JavaScript'] | 0 |
count-nodes-with-the-highest-score | Proper DFS Solution || C++ most intuitive Solution | proper-dfs-solution-c-most-intuitive-sol-iu5d | Intuitionjust keep track of the number of nodes in the left subtree and right subtree of a node and the number of nodes in the upper part
number of nodes in the | reggie_ledoux | NORMAL | 2025-01-10T19:28:24.853296+00:00 | 2025-01-10T19:28:24.853296+00:00 | 17 | false | # Intuition
just keep track of the number of nodes in the left subtree and right subtree of a node and the number of nodes in the upper part
`number of nodes in the upper part = n - (left subtree nodes+ right subtree nodes +1(the node itself))` perform the dfs just like we do in tree and update populate the nodes in... | 0 | 0 | ['C++'] | 0 |
count-nodes-with-the-highest-score | The bigger picture | the-bigger-picture-by-tonitannoury01-f8zt | IntuitionApproachComplexity
Time complexity:
O(n)
Space complexity:
O(n)
Code | tonitannoury01 | NORMAL | 2025-01-03T16:28:51.913440+00:00 | 2025-01-03T16:28:51.913440+00:00 | 5 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
O(n)
- Space complexity:
O(n)
# Code
```javascript []
/**
* @param {number[]} parents
* @return {number}
*/
var countHighestScoreNodes... | 0 | 0 | ['JavaScript'] | 0 |
count-nodes-with-the-highest-score | Python dfs simple | python-dfs-simple-by-vineel97-442o | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | vineel97 | NORMAL | 2025-01-03T04:14:11.191507+00:00 | 2025-01-03T04:14:11.191507+00:00 | 13 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
count-nodes-with-the-highest-score | My DAG Solution, | my-dag-solution-by-jackson1-0hqc | IntuitionUse DAG iterative not DFS with recursion.ApproachComplexity
Time complexity:
Space complexity:
Code | Jackson1 | NORMAL | 2025-01-02T17:41:51.125386+00:00 | 2025-01-02T17:41:51.125386+00:00 | 16 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Use DAG iterative not DFS with recursion.
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space co... | 0 | 0 | ['Java'] | 0 |
count-nodes-with-the-highest-score | Simple DFS backtracking solution | simple-dfs-backtracking-solution-by-kulk-nxi6 | IntuitionTypical problem of backtracking. In the question the binary tree isn't given in the nodal form. Thus create graph#nodes=#nodes.left+#nodes.right+1Compl | kulkarnipinaki | NORMAL | 2024-12-28T16:58:22.254759+00:00 | 2024-12-28T16:58:22.254759+00:00 | 10 | false | # Intuition
Typical problem of backtracking. In the question the binary tree isn't given in the nodal form. Thus create graph
#nodes=#nodes.left+#nodes.right+1
# Complexity
- Time complexity: **O(n)**
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: **O(n)**
<!-- Add your space complexity he... | 0 | 0 | ['Python3'] | 0 |
count-nodes-with-the-highest-score | DFS + node counting + freq | Easy | dfs-node-counting-freq-easy-by-krystek-ign6 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Krystek | NORMAL | 2024-12-25T23:38:11.930938+00:00 | 2024-12-25T23:38:11.930938+00:00 | 7 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
count-nodes-with-the-highest-score | Easy 2 DFS | Very Intuitive | easy-2-dfs-very-intuitive-by-akashkumari-pehd | Code | akashkumariitian360 | NORMAL | 2024-12-23T20:13:35.210920+00:00 | 2024-12-23T20:14:05.541053+00:00 | 9 | false |
# Code
```cpp []
#define ll long long
class Solution {
public:
vector<int>v;
ll ans;
void dfs(int node,int par,vector<vector<int>>&adj){
v[node]=1;
for(auto child:adj[node]){
if(child!=par){
dfs(child,node,adj);
v[node]+=v[child];
}
... | 0 | 0 | ['Array', 'Depth-First Search', 'Binary Tree', 'C++'] | 0 |
count-nodes-with-the-highest-score | C++ Postorder traversal | c-postorder-traversal-by-galster-6ioh | IntuitionThe score of a node is the multiplication of the number of children on its right and his left and the tree which its parent belongs to.ApproachFirst cr | galster | NORMAL | 2024-12-21T06:29:41.186880+00:00 | 2024-12-21T06:29:41.186880+00:00 | 6 | false | # Intuition
The score of a node is the multiplication of the number of children on its right and his left and the tree which its parent belongs to.
# Approach
First create an array Children which will have for each node a pair of left childe, right child.
Do a post order traversal of the Children array and for each ... | 0 | 0 | ['C++'] | 0 |
count-nodes-with-the-highest-score | Clean Python code with easy to understand explanation. | clean-python-code-with-easy-to-understan-t6y2 | Intuition\n Describe your first thoughts on how to solve this problem. \nThree Cases\nRoot node\nLeaf node\nMid node (neither noot nor leaf node)\nn = length of | git_cat_99 | NORMAL | 2024-11-10T22:01:21.610637+00:00 | 2024-11-10T22:01:21.610664+00:00 | 11 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThree Cases\nRoot node\nLeaf node\nMid node (neither noot nor leaf node)\nn = length of parents array\n1. Leaf node leaves behind (n - 1) nodes\n2. Root node leaves behind nodes in left subtree and right subtree\n3. Mid node leaves behind... | 0 | 0 | ['Depth-First Search', 'Recursion', 'Binary Tree', 'Python3'] | 0 |
count-nodes-with-the-highest-score | pure C | pure-c-by-ekko3add-qssr | Intuition\nMy first thought was to use DFS in a post-order traversal manner to compute the number of nodes in each subtree.\nBy traversing each node\'s left and | ekko3add | NORMAL | 2024-11-05T13:18:25.134780+00:00 | 2024-11-05T13:18:25.134811+00:00 | 3 | false | # Intuition\nMy first thought was to use DFS in a post-order traversal manner to compute the number of nodes in each subtree.\nBy traversing each node\'s left and right children, we can calculate the number of nodes in the left and right subtrees, then compute the "score" using the below formula:\n```\nscore = left * r... | 0 | 0 | ['Depth-First Search', 'C', 'Binary Tree', 'Counting'] | 0 |
count-nodes-with-the-highest-score | DFS || O(N) Time and space | dfs-on-time-and-space-by-bhaskarv2000-iye1 | Intuition\nUse DFS, recursively move to the leaf nodes, calculate score, and return total child nodes from there.\n\n# Approach\nScore of a node = Non-zero Numb | BhaskarV2000 | NORMAL | 2024-10-31T07:50:21.124125+00:00 | 2024-10-31T07:50:21.124158+00:00 | 2 | false | # Intuition\nUse DFS, recursively move to the leaf nodes, calculate score, and return total child nodes from there.\n\n# Approach\nScore of a node = Non-zero Number of Left child nodes * Non-zero Number of right child nodes * Non-zero Number of remaiming nodes except child nodes and self node.\n\n# Complexity\n- Time c... | 0 | 0 | ['Python3'] | 0 |
count-nodes-with-the-highest-score | count-nodes-with-the-highest-score C# Solution | count-nodes-with-the-highest-score-c-sol-s7cx | \n\n# Code\ncsharp []\npublic class Solution {\n Dictionary<int, int[]> parentDict = new();\n int result = 0;\n long maxScore = 0;\n \n public in | himashusharma | NORMAL | 2024-10-18T06:36:55.998027+00:00 | 2024-10-18T06:36:55.998065+00:00 | 8 | false | \n\n# Code\n```csharp []\npublic class Solution {\n Dictionary<int, int[]> parentDict = new();\n int result = 0;\n long maxScore = 0;\n \n public int CountHighestScoreNodes(int[] parents) {\n for(int i=0; i< parents.Length; i++){\n parentDict.TryAdd(i, new int[2]);\n }\n f... | 0 | 0 | ['Array', 'Tree', 'Depth-First Search', 'Binary Tree', 'C#'] | 0 |
count-nodes-with-the-highest-score | Easy Solution || DFS | easy-solution-dfs-by-abhi5114-7dii | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Abhi5114 | NORMAL | 2024-10-15T14:48:12.856316+00:00 | 2024-10-15T14:48:32.196647+00:00 | 32 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Java'] | 0 |
count-nodes-with-the-highest-score | Simple Java Solution | simple-java-solution-by-sakshikishore-rgar | Code\njava []\npublic class Node {\n int x;\n int y;\n\n public Node(int i, int j) {\n x = i;\n y = j;\n }\n}\n\nclass Solution {\n | sakshikishore | NORMAL | 2024-10-14T03:35:29.072838+00:00 | 2024-10-14T03:35:29.072868+00:00 | 13 | false | # Code\n```java []\npublic class Node {\n int x;\n int y;\n\n public Node(int i, int j) {\n x = i;\n y = j;\n }\n}\n\nclass Solution {\n public int countHighestScoreNodes(int[] parents) {\n long max=0;\n HashMap<Integer,Node> map=new HashMap<Integer,Node>();\n HashMap<I... | 0 | 0 | ['Java'] | 0 |
count-nodes-with-the-highest-score | DFS | dfs-by-drk-89zg | python3 []\nclass Solution:\n def countHighestScoreNodes(self, parents: List[int]) -> int:\n graph = defaultdict(list)\n for i in range(len(par | drk_ | NORMAL | 2024-10-05T18:29:45.418492+00:00 | 2024-10-05T18:29:45.418512+00:00 | 1 | false | ```python3 []\nclass Solution:\n def countHighestScoreNodes(self, parents: List[int]) -> int:\n graph = defaultdict(list)\n for i in range(len(parents)) :\n graph[parents[i]].append(i)\n \n total = len(parents) - 1 \n answer = defaultdict(int)\n\n def dfs(node) :\... | 0 | 0 | ['Python3'] | 0 |
count-nodes-with-the-highest-score | Python3 Recursive DFS Solution with Explanation | python3-recursive-dfs-solution-with-expl-uin4 | Approach\n Describe your approach to solving the problem. \nBuild a tree using the parents array. Keep track of maxScore and frequency with a dictionary. Track | sage_ | NORMAL | 2024-10-03T16:34:59.659648+00:00 | 2024-10-03T16:34:59.659685+00:00 | 7 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nBuild a tree using the parents array. Keep track of maxScore and frequency with a dictionary. Track number of nodes in a subtree with `countNodes` DFS. \n\nThere are 3 subtrees that we will need to count and multiply:\n1 Parent subtree up to node remo... | 0 | 0 | ['Python3'] | 0 |
count-nodes-with-the-highest-score | TreeNode + PostOrder to calculate left and right subtree sums | treenode-postorder-to-calculate-left-and-nad0 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | lex217 | NORMAL | 2024-10-03T03:14:31.779088+00:00 | 2024-10-03T03:14:31.779122+00:00 | 1 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | 0 | ['Python'] | 0 |
count-nodes-with-the-highest-score | C# Tree Traversal and Subtree Size Calculation Solution using DFS | c-tree-traversal-and-subtree-size-calcul-616t | Intuition\n Describe your first thoughts on how to solve this problem. The problem can be approached by leveraging tree traversal techniques, specifically DFS, | GetRid | NORMAL | 2024-09-25T14:37:36.023492+00:00 | 2024-09-25T14:37:36.023536+00:00 | 8 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->The problem can be approached by leveraging tree traversal techniques, specifically DFS, to gather essential information about the tree structure, such as subtree sizes. This data allows simulating the removal of each node efficiently, whic... | 0 | 0 | ['Array', 'Tree', 'Depth-First Search', 'C#'] | 0 |
count-nodes-with-the-highest-score | Python 3 | Graph, DFS, O(N) | python-3-graph-dfs-on-by-anik_mahmud-jljs | \nclass Solution:\n def countHighestScoreNodes(self, parents: List[int]) -> int:\n n = len(parents)\n graph: List[List[int]] = [[] for _ in ran | anik_mahmud | NORMAL | 2024-09-16T07:31:49.570173+00:00 | 2024-09-16T07:31:49.570211+00:00 | 1 | false | ```\nclass Solution:\n def countHighestScoreNodes(self, parents: List[int]) -> int:\n n = len(parents)\n graph: List[List[int]] = [[] for _ in range(n)]\n\n\n for idx, val in enumerate(parents):\n if val == -1: \n continue\n graph[idx].append(val)\n ... | 0 | 0 | ['Depth-First Search'] | 0 |
permutation-sequence | "Explain-like-I'm-five" Java Solution in O(n) | explain-like-im-five-java-solution-in-on-il4e | EDIT: I\'m tired of some of you commenting on the O(n)-ness of this and especially those of you with snarky condescending tones. It\'s not difficult to implemen | tso | NORMAL | 2015-06-28T18:07:10+00:00 | 2021-10-09T06:29:48.514211+00:00 | 131,719 | false | **EDIT: I\'m tired of some of you commenting on the O(n)-ness of this and especially those of you with snarky condescending tones. It\'s not difficult to implement your own data structure that can do O(1) "list" remove. I\'m not going to put all that code here and dilute the the main solution which is the pattern of so... | 1,487 | 20 | [] | 160 |
permutation-sequence | C++ | Very Easy and Detailed Explanation (Idea + code) | c-very-easy-and-detailed-explanation-ide-ury6 | Let us first take an example to under the idea:\nSuppose n = 4.\nSo we have elements - 1,2,3,4\nThere are total n!= 4! = 24 permutations possible. We can see a | ashwinfury1 | NORMAL | 2020-06-20T10:22:04.767459+00:00 | 2020-06-20T10:29:53.964163+00:00 | 21,865 | false | Let us first take an example to under the idea:\nSuppose n = 4.\nSo we have elements - 1,2,3,4\nThere are total n!= 4! = 24 permutations possible. We can see a specific pattern here:\n\n```\narr\n[ 1 2 3 4]\n1 2 3 4 2 1 3 4 3 1 2 4 4 1 2 3\n1 2 4 3 2 1 4 3 3 1 4 2 4 ... | 244 | 3 | [] | 23 |
permutation-sequence | Share my Python solution with detailed explanation | share-my-python-solution-with-detailed-e-kc2j | The idea is as follow:\n\nFor permutations of n, the first (n-1)! permutations start with 1, next (n-1)! ones start with 2, ... and so on. And in each group of | dasheng2 | NORMAL | 2015-07-20T15:59:53+00:00 | 2018-10-26T05:51:06.071872+00:00 | 26,484 | false | The idea is as follow:\n\nFor permutations of n, the first (n-1)! permutations start with 1, next (n-1)! ones start with 2, ... and so on. And in each group of (n-1)! permutations, the first (n-2)! permutations start with the smallest remaining number, ...\n\ntake n = 3 as an example, the first 2 (that is, (3-1)! ) per... | 168 | 5 | ['Python'] | 21 |
permutation-sequence | An iterative solution for reference | an-iterative-solution-for-reference-by-a-7nwq | Recursion will use more memory, while this problem can be solved by iteration. I solved this problem before, but I didn't realize that using k = k-1 would avoid | adeath | NORMAL | 2014-11-13T22:02:06+00:00 | 2018-10-02T21:45:40.797903+00:00 | 35,695 | false | Recursion will use more memory, while this problem can be solved by iteration. I solved this problem before, but I didn't realize that using k = k-1 would avoid dealing with case k%(n-1)!==0. Rewrote this code, should be pretty concise now. \n\nOnly thing is that I have to use a list to store the remaining numbers, nei... | 140 | 2 | ['Java'] | 21 |
permutation-sequence | ✔️C++🔥100%✅Fastest solution || Best approach with good explanation || Easy to understand. 1 | c100fastest-solution-best-approach-with-zc3z8 | Intution:-\n\nSince this is permutaion we can assume that there are four positions that need to be filled using the four numbers of the sequence. First, we need | pranjal9424 | NORMAL | 2022-08-30T01:22:37.376807+00:00 | 2022-12-12T19:00:10.183413+00:00 | 7,993 | false | **Intution:-**\n\nSince this is permutaion we can assume that there are four positions that need to be filled using the four numbers of the sequence. First, we need to decide which number is to be placed at the first index. Once the number at the first index is decided we have three more positions and three more numers... | 106 | 0 | ['Math', 'Backtracking', 'Recursion', 'C', 'Iterator', 'C++'] | 10 |
permutation-sequence | Easy understand, Most concise C++ solution, minimal memory required | easy-understand-most-concise-c-solution-tr47n | This problem is recursive like dynamic programming.\nKth Permutation sequence can be formed by choosing the 1st digit and then the rest of the digits one by one | 1337beef | NORMAL | 2014-09-13T13:37:26+00:00 | 2020-02-01T03:27:46.977059+00:00 | 31,022 | false | This problem is recursive like dynamic programming.\nKth Permutation sequence can be formed by choosing the 1st digit and then the rest of the digits one by one.\nVisually:\n1 + (permutations of rest of digits)\n2 + (permutations of ...)\nso on...\n\nFor N=3,\nwe have the permutations:\n1|2,3\n1|3,2\n2|1,3\n2|3,1\n3|1,... | 85 | 4 | [] | 16 |
permutation-sequence | Clean Java Solution | clean-java-solution-by-mo10-1mpm | The basic idea is to decide which is the correct number starting from the highest digit.\nUse k divide the factorial of (n-1), the result represents the ith not | mo10 | NORMAL | 2015-08-12T00:13:01+00:00 | 2018-10-02T20:57:08.496107+00:00 | 12,686 | false | The basic idea is to decide which is the correct number starting from the highest digit.\nUse k divide the factorial of (n-1), the result represents the ith not used number.\nThen update k and the factorial to decide next digit.\n\n\n public String getPermutation(int n, int k) {\n\n LinkedList<Integer> notUsed = ... | 78 | 0 | [] | 8 |
permutation-sequence | C++ | 100% time, space efficient | Iterative Solution | Detailed Explanation with Example | c-100-time-space-efficient-iterative-sol-1ra2 | The approach is mathematical. The idea is to keep selecting a digit and eliminating it from further selection based on value of K. \n\nFor example:\n\nGiven, N | dimpleryp | NORMAL | 2020-06-20T15:17:22.565346+00:00 | 2020-06-21T19:50:13.454958+00:00 | 6,703 | false | The approach is mathematical. The idea is to keep selecting a digit and eliminating it from further selection based on value of K. \n\nFor example:\n\n**Given, N = 4, K = 9**\n\nThere are 6 numbers starting with 1: 1234, 1243, 1324, 1342, 1423, 1432\nThere are 6 numbers starting with 2: 2134, 2143, 2314, 2341, 2413, 24... | 73 | 1 | ['Math', 'C', 'Iterator', 'C++'] | 4 |
permutation-sequence | [Python] Math solution + Oneliner, both O(n^2), expained | python-math-solution-oneliner-both-on2-e-1nji | The simplest way to solve this problem is use backtracking, where you just generate all sequences, with complexity O(k) = O(n!). We can do better. Let us consid | dbabichev | NORMAL | 2020-06-20T07:41:57.029541+00:00 | 2020-06-20T16:28:50.210321+00:00 | 3,901 | false | The simplest way to solve this problem is use backtracking, where you just generate all sequences, with complexity `O(k) = O(n!)`. We can do better. Let us consider an example: `n=6`, `k=314`. How we can find the first digit? There are `5! = 120` permutations, which start with `1`, there are also `120` permutations, wh... | 68 | 6 | ['Math'] | 6 |
permutation-sequence | Sharing my straightforward C++ solution with explanation | sharing-my-straightforward-c-solution-wi-lxzs | string getPermutation(int n, int k) {\n int pTable[10] = {1};\n for(int i = 1; i <= 9; i++){\n pTable[i] = i * pTable[i - 1];\n | zxyperfect | NORMAL | 2015-01-08T23:12:28+00:00 | 2015-01-08T23:12:28+00:00 | 17,435 | false | string getPermutation(int n, int k) {\n int pTable[10] = {1};\n for(int i = 1; i <= 9; i++){\n pTable[i] = i * pTable[i - 1];\n }\n string result;\n vector<char> numSet;\n numSet.push_back('1');\n numSet.push_back('2');\n numSet.push_back('3');\n ... | 61 | 2 | ['Hash Table', 'C++'] | 17 |
permutation-sequence | Backtrack Summary: General Solution for 10 Questions!!!!!!!! Python (Combination Sum, Subsets, Permutation, Palindrome) | backtrack-summary-general-solution-for-1-i2fn | For Java version, please refer to isssac3's answer.\n\n39. Combination Sum\nhttps://leetcode.com/problems/combination-sum/\n\n def combinationSum(self, candi | dichen001 | NORMAL | 2016-12-24T05:18:39.108000+00:00 | 2018-10-10T04:30:27.984809+00:00 | 6,892 | false | For Java version, please refer to [isssac3's answer.](https://discuss.leetcode.com/topic/46162/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-partioning)\n\n**39. Combination Sum**\nhttps://leetcode.com/problems/combination-sum/\n```\n def combinationSum(self, ca... | 52 | 0 | [] | 5 |
permutation-sequence | 0ms C++ 12-line concise solution (no recursion, no helper function) | 0ms-c-12-line-concise-solution-no-recurs-h4iv | Attached please find my solution.\n\nIdea:\n\n- For an n-element permutation, there are (n-1)! permutations started with '1', (n-1)! permutations started with ' | zhiqing_xiao | NORMAL | 2015-07-19T15:03:20+00:00 | 2015-07-19T15:03:20+00:00 | 7,400 | false | Attached please find my solution.\n\nIdea:\n\n- For an n-element permutation, there are (n-1)! permutations started with '1', (n-1)! permutations started with '2', and so forth. Therefore we can determine the value of the first element.\n\n- After determining the first element, there are (n-1) candidates left. Then the... | 32 | 0 | [] | 1 |
permutation-sequence | Simple 0s C++ solution | simple-0s-c-solution-by-kenigma-4vgm | since n will be between 1 and 9 inclusive. pre-calculate the factorials is faster.\n\n class Solution {\n public:\n string getPermutation(int n, in | kenigma | NORMAL | 2016-02-21T19:33:16+00:00 | 2016-02-21T19:33:16+00:00 | 5,752 | false | since n will be between 1 and 9 inclusive. pre-calculate the factorials is faster.\n\n class Solution {\n public:\n string getPermutation(int n, int k) {\n string res;\n string nums = "123456789";\n int f[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};\n -... | 29 | 0 | ['C++'] | 4 |
permutation-sequence | C++ Recursive Solution | Maths Explained | 0ms | Faster than 100 % | c-recursive-solution-maths-explained-0ms-3efi | Question is to find the Kth permutation of a given number.\n\nNaive approach to solve this problem is to store all the possible permutations in an array and the | dhairyabahl | NORMAL | 2021-06-29T22:59:57.768611+00:00 | 2021-06-29T23:03:10.428518+00:00 | 2,342 | false | **Question is to find the Kth permutation of a given number.**\n\nNaive approach to solve this problem is to store all the possible permutations in an array and then simply return the kth one. This approach works perfectly fine but is having poor time complexity \uD83D\uDE11\n\nThe most optimised approach of this quest... | 26 | 1 | ['Math', 'Backtracking', 'C++'] | 4 |
permutation-sequence | Solution | solution-by-deleted_user-1ewv | C++ []\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n vector<int> v={0};\n int tmp=1;\n for(int i=1;i<=n;i++){\n | deleted_user | NORMAL | 2023-01-03T14:04:02.442857+00:00 | 2023-03-06T13:23:22.368110+00:00 | 3,927 | false | ```C++ []\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n vector<int> v={0};\n int tmp=1;\n for(int i=1;i<=n;i++){\n v.push_back(i);\n tmp*=i;\n }\n string s;\n cout<<tmp<<" ";\n for(int i=n;i>=2;i--){\n tmp/=i;\n ... | 25 | 0 | ['C++', 'Java', 'Python3'] | 1 |
permutation-sequence | Permutation Sequence Java Solution|| 1.Bruteforce Approach || 2.Optimal Approach | permutation-sequence-java-solution-1brut-9gxn | \n1. First Approach\n//generate all permutation for 1 to n\n//store all permutation in some data structure\n//return kth term from data structure\n\n//Example : | palpradeep | NORMAL | 2022-11-04T08:58:21.839763+00:00 | 2022-11-06T18:06:03.033198+00:00 | 2,896 | false | ```\n1. First Approach\n//generate all permutation for 1 to n\n//store all permutation in some data structure\n//return kth term from data structure\n\n//Example :- n=3 , k=4\n//Generate permutations(LeetCode Qus. 46 :- Permutations) 1 to 3 i.e. \n123\n132\n213\n231\n312\n321\n//k=4th term is 231\n//our ans will be 231... | 25 | 0 | ['Math', 'Backtracking', 'Recursion', 'Java'] | 2 |
permutation-sequence | C++ | Solution with Comments | Brute Force and Optimal || Easy Understanding | c-solution-with-comments-brute-force-and-zkgy | Please Upvote if it helps you...\n\n\n\nSolution 1: Brute Force Solution\n\n\nApproach:\n\nThe extreme naive solution is to generate all the possible permutatio | Tejender_Upadhyay | NORMAL | 2022-07-09T05:54:14.865103+00:00 | 2022-07-10T05:25:00.946215+00:00 | 1,659 | false | ***Please Upvote if it helps you...***\n\n\n***\n***Solution 1: Brute Force Solution***\n***\n***\n***Approach:***\n***\nThe extreme naive solution is to generate all the possible permutations of the given sequ... | 24 | 0 | ['Recursion', 'C', 'C++'] | 2 |
permutation-sequence | Does anyone have a better idea? Share my accepted python code here | does-anyone-have-a-better-idea-share-my-93uas | It's obvious that if we try to come up with n! solutions one by one until it reach kth element - O(k), it will exceed the time limit. Therefore, I tried to impl | paullo | NORMAL | 2014-05-25T10:19:12+00:00 | 2014-05-25T10:19:12+00:00 | 12,961 | false | It's obvious that if we try to come up with n! solutions one by one until it reach kth element - O(k), it will exceed the time limit. Therefore, I tried to implement a mathematical solution as follows:\n\n class Solution:\n # @return a string\n def getPermutation(self, n, k):\n \n ll = [... | 24 | 1 | ['Probability and Statistics', 'Python'] | 12 |
permutation-sequence | Python3 Solution Explained With a Tip For Faster Execution | Beats 99.8% | python3-solution-explained-with-a-tip-fo-r7uu | My solution is basically the same with the many others but here is another explanation:\n\nLet\'s go over an example:\n\nn=4 k=9\n1234 ------ start here\n1243 | aysusayin | NORMAL | 2020-06-20T13:26:24.604251+00:00 | 2020-06-20T21:20:05.257458+00:00 | 2,448 | false | My solution is basically the same with the many others but here is another explanation:\n\nLet\'s go over an example:\n```\nn=4 k=9\n1234 ------ start here\n1243 ------ third digit changes here \n1324 ------ second digit changes here \n1342\n1423\n1432 \n2134 ------ first digit changes here \n2143\n2314 -> k=9\n2341\n... | 23 | 0 | ['Python', 'Python3'] | 2 |
permutation-sequence | Java Solution With Complete Explanation | java-solution-with-complete-explanation-f9o1f | \nSolution 1: Brute Force Solution\n\nApproach: \n\nThe extreme naive solution is to generate all the possible permutations of the given sequence. This is ach | ananya19 | NORMAL | 2022-09-10T12:05:34.147469+00:00 | 2022-09-17T18:34:26.379859+00:00 | 1,589 | false | \n**Solution 1: Brute Force Solution**\n\n**Approach:** \n\nThe extreme naive solution is to generate all the possible permutations of the given sequence. This is achieved using recursion and every permutation generated is stored in some other data structure (here we have used a vector). Finally, we sort the data str... | 19 | 0 | ['Java'] | 3 |
permutation-sequence | ✅C++✅||😁Hinglish||👍Easy-Explanation👌|| Recursion😍 || Interview Prep👨💻🔥 | chinglisheasy-explanation-recursion-inte-fwae | Approach\n- Explained properly in the code\n\n# Code\n\nclass Solution\n{\n public:\n \t//Logics\n\n \t//Naive Solution\n \t//1. Maanlo 1,2 | modisanskar5 | NORMAL | 2023-07-13T22:26:38.715468+00:00 | 2023-07-13T22:26:38.715509+00:00 | 1,450 | false | # Approach\n- Explained properly in the code\n\n# Code\n```\nclass Solution\n{\n public:\n \t//Logics\n\n \t//Naive Solution\n \t//1. Maanlo 1,2,3,4 diya hain to ek tareeka to apna tha ki saare permutations nikal lenge like (Permutations 2) then we will find the K the one.\n \t//2. But it is ... | 17 | 0 | ['Math', 'Recursion', 'C++'] | 1 |
permutation-sequence | c++ 0ms 100% with algorithm explanation | c-0ms-100-with-algorithm-explanation-by-svhq7 | as we know 4! = 24 = 4*(3!) that means the first layer looks like '1'[][][],'2'[][][],'3'[][][],'4'[][][] then we can search which position number 9 w | somone23412 | NORMAL | 2018-11-09T08:59:18.121727+00:00 | 2018-11-09T08:59:18.121765+00:00 | 2,002 | false | as we know
4! = 24 = 4*(3!)
that means the first layer looks like
'1'[][][],'2'[][][],'3'[][][],'4'[][][]
then we can search which position number 9 will be at this layer:
position = 9/3! = 1.5
and we know the first number position will be [1.5] = 2, which represent '2'[][][],we know the first number i... | 16 | 0 | [] | 4 |
permutation-sequence | Editorial Easy Explanation! | editorial-easy-explanation-by-akshay0406-r3x6 | *using some maths\n*for ex: you have n = 4\nthen there will be 4! permutations and k=17\nthen if we are using 0 based index then we have to find 16th index\nNow | Akshay0406 | NORMAL | 2021-07-01T19:31:33.576811+00:00 | 2021-07-01T19:31:33.576857+00:00 | 891 | false | # ****using some maths\n**for ex: you have n = 4\nthen there will be 4! permutations and k=17\nthen if we are using 0 based index then we have to find 16th index\nNow as it is lexicographically sorted\nthen first digit 1 , other digits {2,3,4} permutations then\nfirst digit 2 , other digits {1,3,4} perm then\nfirst dig... | 15 | 0 | ['Math', 'Java'] | 2 |
permutation-sequence | C++ easy with next_permutation | c-easy-with-next_permutation-by-oleksam-fmya | \n// Please, Upvote :-)\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n string s = "";\n for (int i = 1; i <= n; i++)\n | oleksam | NORMAL | 2020-06-20T09:40:26.596592+00:00 | 2020-06-20T09:40:26.596628+00:00 | 1,158 | false | ```\n// Please, Upvote :-)\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n string s = "";\n for (int i = 1; i <= n; i++)\n s += to_string(i);\n int curPerm = 1;\n while (curPerm < k) {\n curPerm++;\n next_permutation(s.begin(), s.end()... | 15 | 5 | ['C', 'C++'] | 1 |
permutation-sequence | Python concise solution | python-concise-solution-by-oldcodingfarm-x2p9 | \n # TLE\n def getPermutation(self, n, k):\n nums = range(1, n+1)\n for i in xrange(k-1):\n self.nextPermutation(nums)\n r | oldcodingfarmer | NORMAL | 2015-09-08T11:14:16+00:00 | 2015-09-08T11:14:16+00:00 | 3,339 | false | \n # TLE\n def getPermutation(self, n, k):\n nums = range(1, n+1)\n for i in xrange(k-1):\n self.nextPermutation(nums)\n return "".join(map(str, nums))\n \n def nextPermutation(self, nums):\n l = d = m = len(nums)-1\n while l > 0 and nums[l] <= nums[... | 14 | 1 | ['Python'] | 0 |
permutation-sequence | C++, backtracking / STL / Math solutions | c-backtracking-stl-math-solutions-by-zef-0vii | Solution 1. Backtracking\n\nRun Time: 266ms\n\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n string s = "", res = "";\n f | zefengsong | NORMAL | 2017-09-06T04:15:21.964000+00:00 | 2017-09-06T04:15:21.964000+00:00 | 1,511 | false | **Solution 1.** Backtracking\n\nRun Time: 266ms\n```\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n string s = "", res = "";\n for(int i = 1; i <= n; i++) s.push_back(i + '0');\n string path = s;\n int count = 0;\n DFS(s, 0, count, n, k, path, res);\n r... | 14 | 0 | ['C++'] | 0 |
permutation-sequence | ✅ [Solution] Swift: Permutation Sequence (+ test cases) | solution-swift-permutation-sequence-test-kua2 | swift\nclass Solution {\n func getPermutation(_ n: Int, _ k: Int) -> String {\n var numbers = [Int](1...n)\n var k = k, factorial = 1, diff = n | AsahiOcean | NORMAL | 2022-02-02T08:58:24.340469+00:00 | 2022-02-02T08:58:24.340496+00:00 | 935 | false | ```swift\nclass Solution {\n func getPermutation(_ n: Int, _ k: Int) -> String {\n var numbers = [Int](1...n)\n var k = k, factorial = 1, diff = n - 1, result = ""\n \n for i in 1..<n { factorial *= i }\n \n for _ in 0..<n {\n for (i, num) in numbers.enumerated() ... | 12 | 0 | ['Swift'] | 0 |
permutation-sequence | 💡JavaScript Solution | javascript-solution-by-aminick-bovz | The idea\n1. Some facts about permutation:\n\t* For n, it has n! permutations\n2. Use k to find which permutation set it\'s in, and keep deciding inner permutat | aminick | NORMAL | 2020-02-06T07:03:02.321497+00:00 | 2020-02-06T07:03:02.321551+00:00 | 1,281 | false | ### The idea\n1. Some facts about permutation:\n\t* For `n`, it has `n!` permutations\n2. Use `k` to find which permutation set it\'s in, and keep deciding inner permutation sets.\n\n```\nn = 4, k = 9\n```\n\n| Current K: 9 | | | |\n|--------------|-------------|-------------------|... | 11 | 0 | ['JavaScript'] | 3 |
permutation-sequence | PYTHON3 CLEAN CODE| BEAT 90% TIME AND SPACE| MATH | python3-clean-code-beat-90-time-and-spac-kve7 | Intuition\n Describe your first thoughts on how to solve this problem. \nThis is one of the most interesting problems I\'ve ever solved. Using mathematical, rec | vietz22 | NORMAL | 2024-04-02T04:36:16.868702+00:00 | 2024-04-02T04:36:16.868734+00:00 | 825 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is one of the most interesting problems I\'ve ever solved. Using mathematical, recursion and greedy effectively. You can jump into the Dry Run for a better understand\nThis is a more detail explaination from @UtkarshDubey_19\n\n# For... | 10 | 0 | ['Math', 'Greedy', 'Recursion', 'Python3'] | 2 |
permutation-sequence | C++ next_permutation||Best O(n log n) by set beats 100% | c-next_permutationbest-on-log-n-by-set-b-ck2u | Intuition\n Describe your first thoughts on how to solve this problem. \nC++ STL makes such question so easy. Use iota to create a char array.\nThen use next_pe | anwendeng | NORMAL | 2023-08-28T11:44:21.031888+00:00 | 2023-08-29T04:17:54.877464+00:00 | 1,623 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nC++ STL makes such question so easy. Use iota to create a char array.\nThen use next_permutation k-1 times to find the answer!\n\nnext_permutation takes $O(n)$ time to find the next permutation.\n\nThe second method does not use next_perm... | 10 | 0 | ['Ordered Set', 'C++'] | 1 |
permutation-sequence | ✔️ 100% Fastest Swift Solution | 100-fastest-swift-solution-by-sergeylesc-f4de | \nclass Solution {\n func getPermutation(_ n: Int, _ k: Int) -> String {\n var digitals: [Int] = []\n var res: String = ""\n var val = k | sergeyleschev | NORMAL | 2022-04-04T05:32:51.028128+00:00 | 2022-04-04T05:32:51.028154+00:00 | 263 | false | ```\nclass Solution {\n func getPermutation(_ n: Int, _ k: Int) -> String {\n var digitals: [Int] = []\n var res: String = ""\n var val = k\n var m = n\n\n for i in 1...n { digitals.append(i) }\n\n while res.count < n && val > 0 {\n let i = Int(ceil(Double(val) / ... | 10 | 1 | ['Swift'] | 0 |
permutation-sequence | Java beats 100% | java-beats-100-by-deleted_user-sfu8 | Java beats 100%\n\n\n\n\n# Code\n\nclass Solution {\n private static int[] fact = {0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};\n \n private String | deleted_user | NORMAL | 2024-05-14T11:21:30.349569+00:00 | 2024-05-14T11:21:30.349601+00:00 | 1,332 | false | Java beats 100%\n\n\n\n\n# Code\n```\nclass Solution {\n private static int[] fact = {0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};\n \n private String getPermutation(int n, int k, boolean[] nums... | 9 | 0 | ['Java'] | 0 |
permutation-sequence | O(1) time complexity god solution | o1-time-complexity-god-solution-by-suraj-xjzv | \nclass Solution {\n public String getPermutation(int n, int k) {\n\t\tint[] a3={123, 132, 213, 231, 312, 321};\n\t\tint[] a4={1234, 1243, 1324, 1342, 1423, | SurajAg | NORMAL | 2023-06-27T20:31:40.119321+00:00 | 2023-06-27T20:35:58.827472+00:00 | 2,285 | false | ```\nclass Solution {\n public String getPermutation(int n, int k) {\n\t\tint[] a3={123, 132, 213, 231, 312, 321};\n\t\tint[] a4={1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321};\n\t\tint[] a5={12345, 12354, 12435, 12453,... | 9 | 2 | ['Java'] | 20 |
permutation-sequence | An efficient Java solution, without extra space or previous calculation of factorial | an-efficient-java-solution-without-extra-6v6p | This problem consists of two parts.\n\nPart one, find the array A[0..n-1] that satisfies:\n\nk-1 = (n-1)!A[0] + (n-2)!A[1] + ... + 2!A[n-3] + 1!A[n-2] | vision57 | NORMAL | 2014-12-24T08:31:27+00:00 | 2014-12-24T08:31:27+00:00 | 4,246 | false | This problem consists of two parts.\n\nPart one, find the array A[0..n-1] that satisfies:\n\n**k-1 = (n-1)!*A[0] + (n-2)!*A[1] + ... + 2!*A[n-3] + 1!*A[n-2] + 0!*A[n-1]**\n\nand **0 <= A[i] < n-i** (so the last item in the formula above is always 0).\n\nIt's obvious that the array A[0..n-1] can be calculated ... | 9 | 0 | ['Java'] | 1 |
permutation-sequence | Permutation Sequence [C++] | permutation-sequence-c-by-moveeeax-vy1r | IntuitionThe problem involves generating permutations of numbers in lexicographic order, and the key is to directly compute the k-th permutation without generat | moveeeax | NORMAL | 2025-01-18T09:31:45.262009+00:00 | 2025-01-18T09:31:45.262009+00:00 | 1,233 | false | # Intuition
The problem involves generating permutations of numbers in lexicographic order, and the key is to directly compute the k-th permutation without generating all permutations explicitly. This is achieved using factorials to determine the placement of digits.
# Approach
1. Precompute factorials for efficient c... | 8 | 0 | ['C++'] | 0 |
permutation-sequence | Backtracking recursion base solution in C++ | backtracking-recursion-base-solution-in-oqhcc | class Solution {\npublic:\n int cnt = 0; // counting the number of permutations done\nint helper(int n, int k, string &s, int p[])\n {\n if(s.size( | animesh_karmakar-46 | NORMAL | 2019-09-28T16:09:57.878941+00:00 | 2019-09-28T16:09:57.878991+00:00 | 1,627 | false | ``` class Solution {\npublic:\n int cnt = 0; // counting the number of permutations done\nint helper(int n, int k, string &s, int p[])\n {\n if(s.size() == n) {\n cnt += 1;\n // cnt is receive to k so flash all function from call stack \n if(cnt == k) {\n ret... | 8 | 0 | ['Backtracking', 'Recursion'] | 2 |
permutation-sequence | Share my easy understand solution with comments - Java | share-my-easy-understand-solution-with-c-j8tk | public int nFatorial(int n ) {\n \tif(n == 0)\n \t\treturn 1;\n \treturn n * nFatorial(n - 1);\n }\n \n public String getPermuta | liwang | NORMAL | 2015-02-25T19:45:55+00:00 | 2015-02-25T19:45:55+00:00 | 4,827 | false | public int nFatorial(int n ) {\n \tif(n == 0)\n \t\treturn 1;\n \treturn n * nFatorial(n - 1);\n }\n \n public String getPermutation(int n, int k) {\n \tif(n == 0)\n \t\treturn "";\n \t\n \tString res = "";\n \n \t// numbers to be added to result... | 8 | 0 | [] | 2 |
permutation-sequence | Beats 100% | Explained mathematics in detail | beats-100-explained-mathematics-in-detai-3b3b | IntuitionCode | dikshamehta4214 | NORMAL | 2025-03-25T18:23:35.931207+00:00 | 2025-03-25T18:23:35.931207+00:00 | 433 | false | # Intuition


# Code
```cpp []
class Solution {
public:
string getPermutation(int n, ... | 7 | 0 | ['C++'] | 4 |
permutation-sequence | 2 MS | JAVA | EXPLAINED ✔ | 2-ms-java-explained-by-4ryangautam-fcpu | Intuition\nDivide and conquer\u274C\nRemove and conquer\u2714\n\n# Approach\nIn this code you have to only understand three lines \nlets start with \n1. ans = a | 4ryangautam | NORMAL | 2023-07-23T20:46:01.936891+00:00 | 2023-07-23T20:46:01.936917+00:00 | 1,051 | false | # Intuition\nDivide and conquer\u274C\nRemove and conquer\u2714\n\n# Approach\nIn this code you have to only understand three lines \nlets start with \n1. ans = ans + num.get(k/fact); \nSo lets take an example 1 as example , we have given n = 3 and k = 3\nif we pick 1 then we left with 2 , 3;\nif we pick 2 then we left... | 7 | 0 | ['Java'] | 0 |
permutation-sequence | python 20ms clean code. | python-20ms-clean-code-by-texasroh-qg4z | \nclass Solution:\n def getPermutation(self, n: int, k: int) -> str:\n def fact(n):\n r = 1\n for i in range(2,n+1):\n | texasroh | NORMAL | 2020-02-06T05:03:50.064223+00:00 | 2020-02-06T05:03:50.064265+00:00 | 919 | false | ```\nclass Solution:\n def getPermutation(self, n: int, k: int) -> str:\n def fact(n):\n r = 1\n for i in range(2,n+1):\n r *= i\n return r\n \n nums = [str(i) for i in range(1,n+1)]\n s=\'\'\n while(nums):\n div = fact(len... | 7 | 0 | ['Python'] | 2 |
permutation-sequence | Java 100%/100% | O(n^2)/O(n) With Explanation | java-100100-on2on-with-explanation-by-ft-9gyh | See code comments for explanation\n\nclass Solution {\n public String getPermutation(int n, int k) {\n // Idea: calculate this by math\n //\n | ftkftw | NORMAL | 2020-01-15T07:33:38.808897+00:00 | 2020-01-15T18:44:16.969736+00:00 | 1,126 | false | See code comments for explanation\n```\nclass Solution {\n public String getPermutation(int n, int k) {\n // Idea: calculate this by math\n //\n // Observation 1: (k - 1) / (n - 1)! + 1 determines\n // which number in [1, n] comes first\n // E.g. If n = 3, k = 3,\n // then ... | 7 | 0 | ['Math', 'Java'] | 0 |
permutation-sequence | Share my 0ms C++ solution with explanation | share-my-0ms-c-solution-with-explanation-mai1 | The question is:\n\nThe set [1,2,3,\u2026,n] contains a total of n ! unique permutations.\n\nBy listing and labeling all of the permutations in order,\nWe get t | kjer | NORMAL | 2016-03-13T04:29:04+00:00 | 2016-03-13T04:29:04+00:00 | 1,358 | false | The question is:\n\nThe **set *[1,2,3,\u2026,n]*** contains a total of ***n !*** ***unique* permutations**.\n\nBy listing and labeling all of the permutations ***in order***,\nWe get the following sequence (ie, for ***n = 3***):\n\n - 1 "***1*** 23"\n - 2 "***1*** 32"\n - 3 "***2*** 13"\n - 4 "***2*** 31"\n - 5 "*... | 7 | 1 | ['Math', 'C++'] | 1 |
permutation-sequence | Best O(N*N) Solution | best-onn-solution-by-kumar21ayush03-803b | Approach\nMath\n\n# Complexity\n- Time complexity:\nO(n*n)\n\n- Space complexity:\nO(n)\n\n# Code\n\nclass Solution {\npublic:\n string getPermutation(int n, | kumar21ayush03 | NORMAL | 2023-08-24T12:45:43.224274+00:00 | 2023-08-26T05:52:34.562830+00:00 | 683 | false | # Approach\nMath\n\n# Complexity\n- Time complexity:\n$$O(n*n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n vector <int> nums;\n int fact = 1;\n for (int i = 1; i <= n; i++) {\n nums.push_back(i);\n f... | 6 | 0 | ['C++'] | 0 |
permutation-sequence | C++ | Recursion | Backtracking | Easy | Brute Force | ~21% Space ~5% Time | c-recursion-backtracking-easy-brute-forc-f18q | \nclass Solution {\npublic:\n void recur(string s, string temp, vector<bool> &vis, vector<string> &ans, int &cnt, int k){\n if(temp.size() == s.size()){ | amanswarnakar | NORMAL | 2023-03-01T09:32:53.637459+00:00 | 2023-03-01T09:32:53.637503+00:00 | 3,250 | false | ```\nclass Solution {\npublic:\n void recur(string s, string temp, vector<bool> &vis, vector<string> &ans, int &cnt, int k){\n if(temp.size() == s.size()){\n ans.emplace_back(temp);\n cnt++;\n if(cnt == k) return;\n }\n for(int i = 0; i < s.size(); i++){\n if(!vis[i]){\n ... | 6 | 0 | ['Backtracking', 'Recursion', 'C', 'C++'] | 1 |
permutation-sequence | ✅Accepted | | ✅Easy solution || ✅Short & Simple || ✅Best Method | accepted-easy-solution-short-simple-best-dcbt | \n# Code\n\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n int fact=1;\n vector<int> v;\n for(int i=1;i<=n;i++)\n | sanjaydwk8 | NORMAL | 2023-01-23T17:10:42.006082+00:00 | 2023-01-23T17:10:42.006128+00:00 | 567 | false | \n# Code\n```\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n int fact=1;\n vector<int> v;\n for(int i=1;i<=n;i++)\n {\n v.push_back(i);\n if(i!=n)\n fact*=i;\n }\n k-=1;\n string s;\n for(int i=0;i<n;i++)\n... | 6 | 0 | ['C++'] | 0 |
permutation-sequence | ✅ [Python] just 6 lines to build it digit by digit (with detailed comments) | python-just-6-lines-to-build-it-digit-by-ajpu | \u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.\n*\nThis solution employs remainder calculation to get permutation\'s most significant digit on each iteration. | stanislav-iablokov | NORMAL | 2022-11-03T20:35:15.644966+00:00 | 2022-11-04T18:50:56.755767+00:00 | 695 | false | **\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\nThis solution employs remainder calculation to get permutation\'s most significant digit on each iteration. Time complexity is quadratic: **O(N\\*N)**. Space complexity is linear: **O(N)**.\n\n**Comment**. Every time we generate yet another `(n-1)!` permutati... | 6 | 0 | [] | 2 |
permutation-sequence | Java Mathy Solution Explained 100% Recursive thinking | java-mathy-solution-explained-100-recurs-6dms | Given n = 4, k = 9, we know that there at 6 permutation sequences that start with 1\n\nThere are 6 because after the first number there are 3 numbers for the 2n | qwerjkl112 | NORMAL | 2020-10-15T04:13:34.065466+00:00 | 2020-10-15T04:14:06.725839+00:00 | 279 | false | Given n = 4, k = 9, we know that there at 6 permutation sequences that start with `1`\n\nThere are **6** because after the first number there are 3 numbers for the `2nd` position and 2 numbers for the `3th` position and 1 number for the `4th`\n\n`1XXX`\n`1XXX`\n`1XXX`\n`1XXX`\n`1XXX`\n`1XXX`\n`2XXX`\n`2XXX`\n`2XXX`\n`2... | 6 | 0 | [] | 2 |
permutation-sequence | [C++] 100% time 80% space, fully explained and easy to understand iterative solution | c-100-time-80-space-fully-explained-and-mxb1s | The base idea is that the first character can be found knowing that it is repeated (n - 1)! times.\n\nIf you have 1234 as your own base, for example, all the pe | ajna | NORMAL | 2020-06-20T14:41:28.466401+00:00 | 2020-06-20T14:44:21.192843+00:00 | 1,004 | false | The base idea is that the first character can be found knowing that it is repeated `(n - 1)!` times.\n\nIf you have `1234` as your own base, for example, all the permutations starting with, say, `2`, are going to be like this:\n\n`2134`\n`2143`\n`2314`\n`2341`\n`2413`\n`2431`\n\nSince you can indeed only permutate rema... | 6 | 0 | ['C', 'Combinatorics', 'Iterator', 'C++'] | 1 |
permutation-sequence | Python I think this is clean code. with some of my explanation | python-i-think-this-is-clean-code-with-s-ak3q | If we have n numbers then the total combinations would be factorial(n) which means same starting number should have (n - 1)! sequences. \n\nIf we do k mod (n - | winstonchi | NORMAL | 2016-02-16T15:46:22+00:00 | 2016-02-16T15:46:22+00:00 | 1,981 | false | If we have n numbers then the total combinations would be factorial(n) which means same starting number should have (n - 1)! sequences. \n\nIf we do k mod (n - 1)! then we can get the corresponding starting number and append to the result.\n\nNote that we need to maintain another array to mark visited numbers(I take r... | 6 | 1 | [] | 4 |
permutation-sequence | Java Recursive and Iterative | java-recursive-and-iterative-by-crickey1-hd76 | Iterative:\n\n public String getPermutation(int n, int k) {\n \n // e.g n = 5\n // [1][1][2][6][24]\n int[] factorial = new int[n | crickey180 | NORMAL | 2016-10-31T08:35:21.280000+00:00 | 2016-10-31T08:35:21.280000+00:00 | 2,389 | false | Iterative:\n```\n public String getPermutation(int n, int k) {\n \n // e.g n = 5\n // [1][1][2][6][24]\n int[] factorial = new int[n];\n factorial[0] = 1;\n \n for(int i = 1; i < n; i++) {\n factorial[i] = factorial[i-1] * i;\n }\n \n A... | 6 | 1 | [] | 2 |
permutation-sequence | [C++] Clean Code with Explanation | c-clean-code-with-explanation-by-alexand-fnik | Given n digits there will be total of (n * (n-1) * ... * 2 * 1) different permutations. - the 1st digit you have n options, 2nd digit you have n-1, ... last dig | alexander | NORMAL | 2017-12-20T01:34:13.392000+00:00 | 2017-12-20T01:34:13.392000+00:00 | 866 | false | 1. Given n digits there will be total of `(n * (n-1) * ... * 2 * 1)` different permutations. - the 1st digit you have `n` options, 2nd digit you have `n-1`, ... last digit you only have `1` option.\n2. Your choices to pick different number at each digit will form different groups of permutations. Example: Your choice f... | 6 | 0 | [] | 0 |
permutation-sequence | SIMPLE CODE || CLEAN AND GREEN IN DEPTH EXPLAINED. | simple-code-clean-and-green-in-depth-exp-wmmt | Intuition\n Describe your first thoughts on how to solve this problem. \nProblem:\n\n- The code takes two integers n and k as input.\n- n represents the total n | Abhishekkant135 | NORMAL | 2024-06-07T10:25:12.916054+00:00 | 2024-06-07T10:25:12.916102+00:00 | 1,004 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Problem:**\n\n- The code takes two integers `n` and `k` as input.\n- `n` represents the total number of elements (1 to n).\n- `k` represents the index (1-based) of the desired permutation among all possible permutations of numbers from ... | 5 | 0 | ['Math', 'Java'] | 0 |
permutation-sequence | Java beats 100% | C++ beats 100% | java-beats-100-c-beats-100-by-deleted_us-vz0x | Java beats 100% | C++ beats 100%\n\n\n\n\n\n\n\n# Code\n\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n vector<int> v={0};\n | deleted_user | NORMAL | 2024-05-14T11:12:45.289361+00:00 | 2024-05-14T11:12:45.289401+00:00 | 1,528 | false | Java beats 100% | C++ beats 100%\n\n\n\n\n\n\n\n# Code\n```\nclass Solution {\npublic:\n s... | 5 | 0 | ['C++', 'Java'] | 0 |
permutation-sequence | C++ Brute Force ->Optimal Solution (backtracking and Math) 🔥🔥🔥 | c-brute-force-optimal-solution-backtrack-pk1w | Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\nB | hk342064 | NORMAL | 2024-02-07T16:47:43.170057+00:00 | 2024-02-07T16:47:43.170083+00:00 | 742 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\nBrute Force(Backtracking)\n- Ti... | 5 | 0 | ['Math', 'Backtracking', 'C++'] | 0 |
permutation-sequence | 5 line code c++ | 5-line-code-c-by-ayumsh-ln96 | \nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n vector<int> vec;\n for(int i=0;i<n;i++) vec.push_back(i+1);\n for( | ayumsh | NORMAL | 2023-07-31T13:39:40.492753+00:00 | 2023-07-31T13:39:40.492777+00:00 | 751 | false | ```\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n vector<int> vec;\n for(int i=0;i<n;i++) vec.push_back(i+1);\n for(int i=0;i<k-1;i++) next_permutation(vec.begin(),vec.end());\n string s;\n for(int i=0;i<n;i++) s+=\'0\'+vec[i];\n return s;\n }\n};\n... | 5 | 0 | ['C++'] | 2 |
permutation-sequence | ✅Accepted | | ✅Easy solution || ✅Short & Simple || ✅Best Method | accepted-easy-solution-short-simple-best-pj5i | \n# Code\n\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n int fact=1;\n vector<int> v;\n for(int i=1;i<=n;i++)\n | sanjaydwk8 | NORMAL | 2023-01-23T17:06:05.930961+00:00 | 2023-01-23T17:06:05.931011+00:00 | 724 | false | \n# Code\n```\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n int fact=1;\n vector<int> v;\n for(int i=1;i<=n;i++)\n {\n v.push_back(i);\n if(i!=n)\n fact*=i;\n }\n k-=1;\n string s;\n for(int i=0;i<n;i++)\n... | 5 | 0 | ['C++'] | 0 |
permutation-sequence | Easy solution by using STL, Permutation Sequence | easy-solution-by-using-stl-permutation-s-hyjs | \n\n# Code\n\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n \n string s;\n for(int i=1; i<=n; i++){\n s | Ashish_49 | NORMAL | 2023-01-07T13:02:40.943626+00:00 | 2023-01-07T13:02:40.943672+00:00 | 29 | false | \n\n# Code\n```\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n \n string s;\n for(int i=1; i<=n; i++){\n s+=to_string(i);\n }\n vector<string>ans;\n do\n {\n ans.push_back(s);\n }while(next_permutation(s.begin(),s.en... | 5 | 0 | ['C++'] | 2 |
permutation-sequence | 15 ms 🫡Easy Understanding | 15-ms-easy-understanding-by-ayon_ssp-4i3f | Complexity\n- Time complexity: O(n) -> Linear\n- Space complexity: O(n) -> To store the list[]\n\n# Code\n\nimport math\nclass Solution(object):\n def getPer | ayon_ssp | NORMAL | 2022-12-17T12:26:59.095370+00:00 | 2022-12-17T12:26:59.095414+00:00 | 995 | false | # Complexity\n- Time complexity: O(n) -> Linear\n- Space complexity: O(n) -> To store the list[]\n\n# Code\n```\nimport math\nclass Solution(object):\n def getPermutation(self, n, k):\n stToRet = ""\n lst = [str(i) for i in range(1,n+1)]\n \n while len(lst):\n n = len(lst)\n ... | 5 | 0 | ['Python'] | 0 |
permutation-sequence | C++ Solution || 4 Approaches || STL|| Backtracking || Iterative ||Recursion | c-solution-4-approaches-stl-backtracking-gq08 | 1st Appraoch\n(USING STL - next_permutation)\n\n\tstring getPermutation(int n, int k) {\n string s;\n for( int i=1; i<=n; i++)\n s.push | 9891YKYASH | NORMAL | 2022-08-04T02:38:31.219629+00:00 | 2022-08-04T02:39:42.731955+00:00 | 534 | false | **1st Appraoch\n(USING STL - next_permutation)**\n```\n\tstring getPermutation(int n, int k) {\n string s;\n for( int i=1; i<=n; i++)\n s.push_back( i+\'0\');\n \n while( k!=1){\n next_permutation(s.begin(), s.end());\n k--;\n }\n return s;\n ... | 5 | 0 | ['Backtracking', 'Recursion', 'C', 'Iterator', 'C++'] | 0 |
permutation-sequence | Python O(N^2) 96% faster | python-on2-96-faster-by-abhigamez-c05r | \nimport math\nclass Solution:\n def getPermutation(self, n: int, k: int) -> str:\n ans = ""\n nums = [i for i in range(1,n+1)]\n for i | abhigamez | NORMAL | 2021-09-05T06:29:54.119769+00:00 | 2021-09-05T06:30:37.492599+00:00 | 927 | false | ```\nimport math\nclass Solution:\n def getPermutation(self, n: int, k: int) -> str:\n ans = ""\n nums = [i for i in range(1,n+1)]\n for i in range(1,n+1):\n index = 0\n c = math.factorial(n-i)\n \n while c < k:\n index +=1\n ... | 5 | 0 | ['Python', 'Python3'] | 0 |
permutation-sequence | 0ms C++ solution explained | 0ms-c-solution-explained-by-swathi_venne-xp1e | This problem can be solved with recursive approach. \n\n### OBSERVATION:\n Given n, we\'ll be having (n-1)! permutations starting with 1, (n-1)! permutations st | swathi_vennela | NORMAL | 2020-06-21T15:20:14.562043+00:00 | 2020-06-21T17:51:41.614748+00:00 | 203 | false | This problem can be solved with recursive approach. \n\n### OBSERVATION:\n* Given n, we\'ll be having (n-1)! permutations starting with 1, (n-1)! permutations starting with 2, and so on.. So, each block is of size (n-1!).\n* So, this can be used to find out the first digit of the required sequence. Say, n=4 and k=15, t... | 5 | 0 | [] | 0 |
permutation-sequence | Python 3 - Just a function composition, fully explained | python-3-just-a-function-composition-ful-uman | \nclass Solution:\n def getPermutation(self, n: int, k: int) -> str:\n return \'\'.join(next(dropwhile(lambda t: t[0] < k, enumerate(permutations(list | geandbe | NORMAL | 2020-06-20T14:23:13.997882+00:00 | 2020-06-20T14:48:39.863149+00:00 | 804 | false | ```\nclass Solution:\n def getPermutation(self, n: int, k: int) -> str:\n return \'\'.join(next(dropwhile(lambda t: t[0] < k, enumerate(permutations(list("123456789"[0:n])))))[1])\n```\n\nThe solution just get composed from the existing function toolset following the thought train:\n\n`list("123456789"[0:n])`... | 5 | 0 | ['Python3'] | 1 |
permutation-sequence | Java Solution With Explanation | java-solution-with-explanation-by-skakol-bikm | I was searching for proper answer got explanation in the below link. I just added code and improved on explanation\ncourtesy : https://www.lintcode.com/problem | skakollu | NORMAL | 2019-08-01T02:08:34.928892+00:00 | 2019-08-01T02:14:49.723648+00:00 | 634 | false | I was searching for proper answer got explanation in the below link. I just added code and improved on explanation\ncourtesy : https://www.lintcode.com/problem/permutation-sequence/note/187602\nSince we need to fix one position each time me need next we need to get permutations for (n-1)!\n\n\nwhen n = 4, the list is ... | 5 | 0 | [] | 0 |
permutation-sequence | Python two solutions backtracking(40ms) and math(36ms). | python-two-solutions-backtracking40ms-an-q4uf | Approach 1: backtracking.\nfirstly, we permute and count, got one and k-=1, when k==0, that is the kth. But it\'s too slow, consider that, we permute1234, whe | darktiantian | NORMAL | 2019-04-04T06:04:56.596073+00:00 | 2019-04-04T06:04:56.596150+00:00 | 874 | false | Approach 1: backtracking.\nfirstly, we permute and count, got one and `k-=1`, when k==0, that is the kth. But it\'s too slow, consider that, we permute`1234`, when choose `1`, there are `a = factorial(len(rest))` permutations, if k > a, we don\'t need permute starts with `1`. And minus the total `a`.\n\u6B64\u9898\u5... | 5 | 0 | ['Math', 'Backtracking', 'Python3'] | 0 |
permutation-sequence | Easy to understand solution with sample | easy-to-understand-solution-with-sample-c1uy8 | class Solution {\n int getPermutationNumber(int n) {\n int result = 1;\n for(int i=1;i<=n;++i) {\n result *=i;\n | like2 | NORMAL | 2015-11-29T13:15:18+00:00 | 2015-11-29T13:15:18+00:00 | 1,651 | false | class Solution {\n int getPermutationNumber(int n) {\n int result = 1;\n for(int i=1;i<=n;++i) {\n result *=i;\n }\n \n return result;\n }\n \n public:\n string getPermutation(int n, int k) {\n // 1234, 1... | 5 | 0 | [] | 0 |
permutation-sequence | 44ms python solution | 44ms-python-solution-by-janejingya-v3zb | from math import factorial \n class Solution(object):\n \n def getPermutation(self, n, k):\n """\n :type n: int\n | janejingya | NORMAL | 2015-12-22T19:26:11+00:00 | 2015-12-22T19:26:11+00:00 | 2,215 | false | from math import factorial \n class Solution(object):\n \n def getPermutation(self, n, k):\n """\n :type n: int\n :type k: int\n :rtype: str\n """\n res = []\n nums = [i for i in xrange(1, n+1)]\n while n-1 >= 0:\n... | 5 | 1 | ['Python'] | 1 |
permutation-sequence | || Permutation Sequence || 3ms || TC: O(n²) || SC: O(n) || best explanation in detail || | permutation-sequence-3ms-tc-on2-sc-on-be-p5s3 | IntuitionThe problem asks us to find the k-th permutation sequence of the numbers from 1 to n.
Instead of generating all permutations (which is computationally | Ritik-Saxena | NORMAL | 2025-01-24T19:18:00.413687+00:00 | 2025-01-24T19:18:00.413687+00:00 | 1,042 | false | # Intuition
The problem asks us to find the k-th permutation sequence of the numbers from 1 to n.
Instead of generating all permutations (which is computationally expensive), we can directly construct the k-th permutation using factorials to determine the block in which the k-th permutation lies.
The permutations of n... | 4 | 0 | ['Java'] | 3 |
permutation-sequence | Used recursion -- Time com = O(n) | used-recursion-time-com-on-by-satya78550-j7e7 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | satya78550 | NORMAL | 2024-10-18T06:38:56.864752+00:00 | 2024-10-18T06:38:56.864801+00:00 | 414 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 4 | 0 | ['Python3'] | 0 |
permutation-sequence | AFTER LOOKING THIS YOU WON'T BELIVE THIS IS HARD☠️🤯 | after-looking-this-you-wont-belive-this-xca2k | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | abhiyadav05 | NORMAL | 2023-05-01T13:51:46.455902+00:00 | 2023-05-01T13:51:46.455963+00:00 | 578 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->O(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$... | 4 | 1 | ['String', 'Java'] | 0 |
permutation-sequence | Easy solution by using STL, Permutation Sequence | easy-solution-by-using-stl-permutation-s-apyc | \n\n# Code\n\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n \n string s;\n for(int i=1; i<=n; i++){\n s | Ashish_49 | NORMAL | 2023-01-07T13:02:37.066976+00:00 | 2023-01-07T13:02:37.067073+00:00 | 15 | false | \n\n# Code\n```\nclass Solution {\npublic:\n string getPermutation(int n, int k) {\n \n string s;\n for(int i=1; i<=n; i++){\n s+=to_string(i);\n }\n vector<string>ans;\n do\n {\n ans.push_back(s);\n }while(next_permutation(s.begin(),s.en... | 4 | 0 | ['C++'] | 0 |
permutation-sequence | C++ math solution | c-math-solution-by-iamdon-nesd | \nclass Solution {\npublic:\n string getPermutation(int n, int k) \n {\n int fact[10] = {1,1,2,6,24,120,720,5040,40320,362880};\n string ans | IamDon | NORMAL | 2020-07-15T21:52:40.782051+00:00 | 2020-07-15T21:52:40.782104+00:00 | 404 | false | ```\nclass Solution {\npublic:\n string getPermutation(int n, int k) \n {\n int fact[10] = {1,1,2,6,24,120,720,5040,40320,362880};\n string ans="";\n string num="123456789";\n k--;\n for(int i=n; i>0; i--)\n {\n int j=k/fact[i-1];\n k = k%fact[i-1];\... | 4 | 0 | ['Math', 'C', 'C++'] | 1 |
permutation-sequence | [JAVA] Clean Code, O(N) Time Complexity, 0 ms Time, 98.57% Faster | java-clean-code-on-time-complexity-0-ms-bbugm | \nclass Solution {\n \n\tpublic String getPermutation (int n, int k) {\n\t\n\t\tint[] factorial = new int[n];\n\t\tList<Integer> nums = new ArrayList<>();\n\t\t | anii_agrawal | NORMAL | 2020-06-20T20:20:21.855369+00:00 | 2020-06-20T20:22:15.167211+00:00 | 214 | false | ```\nclass Solution {\n \n\tpublic String getPermutation (int n, int k) {\n\t\n\t\tint[] factorial = new int[n];\n\t\tList<Integer> nums = new ArrayList<>();\n\t\t\n\t\tfor (int i = 0; i < n; i++) {\n\t\t\tnums.add (i + 1);\n\t\t\tfactorial[i] = i == 0 ? 1 : i * factorial[i - 1];\n\t\t}\n\t\t\n\t\tStringBuilder ans = n... | 4 | 0 | [] | 1 |
permutation-sequence | 1ms Easy to understand solution | 1ms-easy-to-understand-solution-by-gorks-dzqo | ```\npublic String getPermutation(int n, int k) {\n List num = new LinkedList();\n for (int i = 1; i <= n; i++) \n \tnum.add(i);\n in | gorkshaozar | NORMAL | 2020-06-20T07:25:28.790947+00:00 | 2020-06-20T07:25:28.790984+00:00 | 583 | false | ```\npublic String getPermutation(int n, int k) {\n List<Integer> num = new LinkedList<Integer>();\n for (int i = 1; i <= n; i++) \n \tnum.add(i);\n int[] fact = new int[n]; // factorial\n fact[0] = 1;\n for (int i = 1; i < n; i++) \n \tfact[i] = i*fact[i-1];\n k ... | 4 | 0 | ['Java'] | 1 |
permutation-sequence | Simple Java Solution using Backtracking | simple-java-solution-using-backtracking-wj0gn | \npublic String getPermutation(int n, int k) {\n List<String> result = new ArrayList<>();\n recursion(result,"", n, k,new boolean[n+1]);\n | erels | NORMAL | 2019-11-17T10:21:31.973615+00:00 | 2019-11-17T10:23:11.323559+00:00 | 686 | false | ```\npublic String getPermutation(int n, int k) {\n List<String> result = new ArrayList<>();\n recursion(result,"", n, k,new boolean[n+1]);\n return result.get(k-1);\n }\n \nprivate void recursion(List<String> result, String temp, int max, int k, boolean[] seen){\n \n if(temp.le... | 4 | 0 | ['Backtracking'] | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.