Created
May 3, 2024 03:37
-
-
Save knowsudhanshu/b9d3e620e0d7d672980ca3a101e4f2fd to your computer and use it in GitHub Desktop.
Number of ways to score total N when a player can score either of 3,5,10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func waysToScore(n: Int) -> Int { | |
var arr: [Int] = [Int](repeating: 0, count: n + 1) | |
arr[0] = 1 | |
for index in 1...n { | |
// print("index: \(index)") | |
if index - 3 >= 0 { | |
// print("index - 3 > 0 ----Start--------") | |
// print("\(arr[index])") | |
// print("\(arr[index - 3])") | |
arr[index] += arr[index - 3] | |
// print("\(arr[index])") | |
// print("index - 3 > 0 ----End--------") | |
} | |
if index - 5 >= 0 { | |
// print("index - 5 > 0 ----Start--------") | |
// print("\(arr[index])") | |
// print("\(arr[index - 5])") | |
arr[index] += arr[index - 5] | |
// print("\(arr[index])") | |
// print("index - 5 > 0 ----End--------") | |
} | |
if index - 10 >= 0 { | |
// print("index - 10 > 0 ----Start--------") | |
// print("\(arr[index])") | |
// print("\(arr[index - 10])") | |
arr[index] += arr[index - 10] | |
// print("\(arr[index])") | |
// print("index - 10 > 0 ----End--------") | |
} | |
print("pass-\(index): \(arr)") | |
} | |
return arr[n] | |
} | |
let output = waysToScore(n: 13) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment