Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for 754 & 1345 & 1457 #311

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add 526. Beautiful Arrangement
TerryHuangHD committed Jan 4, 2021
commit 08fceb92179c39765a1e3f4de89aaa5732776ed3
21 changes: 21 additions & 0 deletions DFS/BeautifulArrangement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
func countArrangement(_ n: Int) -> Int {
var arr = Array(repeating: false, count: n)
return countArr(1, &arr)
}

func countArr(_ c: Int, _ arr: inout [Bool]) -> Int {
if c > arr.count { return 1 }

var count = 0
for x in 0..<arr.count {
if !arr[x] && ((x+1) % c == 0 || c % (x+1) == 0) {
arr[x] = true
count += countArr(c+1, &arr)
arr[x] = false
}
}

return count
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -274,6 +274,7 @@
## Depth-first search
| Title | Solution | Difficulty | Time | Space |
| ----- | -------- | ---------- | ---- | ----- |
[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)| [Swift](./DFS/BeautifulArrangement.swift)| Medium| O(n)| O(n)|
[Permutations](https://leetcode.com/problems/permutations/)| [Swift](./DFS/Permutations.swift)| Medium| O(n^n)| O(n)|
[Permutations II](https://leetcode.com/problems/permutations-ii/)| [Swift](./DFS/PermutationsII.swift)| Medium| O(n^n)| O(n)|
[Subsets](https://leetcode.com/problems/subsets/)| [Swift](./DFS/Subsets.swift)| Medium| O(n^n)| O(n)|
@@ -549,6 +550,7 @@
| [Swift](./LinkedList/PseudoPalindromicPathsInABinaryTree.swift) | 1457 | [Pseudo-Palindromic Paths in a Binary Tree](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/) | Medium |
| [Swift](./BFS/JumpGameIV.swift) | 1345 | [Jump Game IV](https://leetcode.com/problems/jump-game-iv/) | Hard |
| [Swift](./Math/ReachANumber.swift) | 754 | [Reach a Number](https://leetcode.com/problems/reach-a-number/) | Medium |
| [Swift](./DFS/BeautifulArrangement.swift) | 526 | [Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/) | Medium |
| [Swift](./LinkedList/LFUCache.swift) | 460 | [LFU Cache](https://oj.leetcode.com/problems/lfu-cache/) | Hard |
| [Swift](./Array/FindDisappearedNumbers.swift)| 448| [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/)| Easy|
| [Swift](./DFS/CombinationSumIV.swift) | 377 | [Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/) | Medium