Advertisement
1988coder

Permutations I

Sep 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. /*
  2. Backtracking (Using TempList approach)
  3.  
  4. Time Complexity : O(n * (n!))
  5. Number of Permutations = n P n = n!
  6. Time taken to create each permutation = O(n)
  7.  
  8. Space Complexity = O(n) -> Max depth of the tree + size of the visited array.
  9. */
  10. class Solution {
  11.     public List<List<Integer>> permute(int[] nums) {
  12.         List<List<Integer>> result = new ArrayList<>();
  13.         if (nums == null) {
  14.             return result;
  15.         }
  16.         if (nums.length == 0) {
  17.             result.add(new ArrayList<Integer>());
  18.             return result;
  19.         }
  20.        
  21.         boolean[] visited = new boolean[nums.length];
  22.        
  23.         backtrack(result, new ArrayList<Integer>(), nums, visited);
  24.         return result;
  25.     }
  26.    
  27.     public void backtrack(List<List<Integer>> list, ArrayList<Integer> tempList, int[] nums, boolean[] visited) {
  28.         if (tempList.size() == nums.length) {
  29.             list.add(new ArrayList<>(tempList));
  30.             return;
  31.         }
  32.         for (int i = 0; i < nums.length; i++) {
  33.             if (visited[i] == true) {
  34.                 continue;
  35.             }
  36.             visited[i] = true;
  37.             tempList.add(nums[i]);
  38.             backtrack(list, tempList, nums, visited);
  39.             visited[i] = false;
  40.             tempList.remove(tempList.size() - 1);
  41.         }
  42.     }
  43. }
  44.  
  45. /*
  46. Backtracking (Swapping apporach)
  47.  
  48. Time Complexity : O(n * (n!))
  49. Number of Permutations = n P n = n!
  50. Time taken to create each permutation = O(n)
  51.  
  52. Space Complexity = O(n) -> Max depth of the tree
  53. */
  54. // class Solution {
  55. //     public List<List<Integer>> permute(int[] nums) {
  56. //         List<List<Integer>> result = new ArrayList<>();
  57. //         if (nums == null) {
  58. //             return result;
  59. //         }
  60. //         if (nums.length == 0) {
  61. //             result.add(new ArrayList<Integer>());
  62. //             return result;
  63. //         }
  64.        
  65. //         backtrack(result, nums, 0);
  66. //         return result;
  67. //     }
  68.    
  69. //     public void backtrack(List<List<Integer>> list, int[] nums, int start) {
  70. //         if (start == nums.length) {
  71. //             List<Integer> tempList = new ArrayList<>();
  72. //             for (int num : nums) {
  73. //                 tempList.add(num);
  74. //             }
  75. //             list.add(tempList);
  76. //             return;
  77. //         }
  78. //         for (int i = start; i < nums.length; i++) {
  79. //             swap(nums, i, start);
  80. //             backtrack(list, nums, start+1);
  81. //             swap(nums, i, start);
  82. //         }
  83. //     }
  84.    
  85. //     public void swap(int[] nums, int i, int j) {
  86. //         int temp = nums[i];
  87. //         nums[i] = nums[j];
  88. //         nums[j] = temp;
  89. //     }
  90. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement