-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathContents.swift
78 lines (41 loc) · 1.28 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
/*
Question 1: https://www.hackerrank.com/challenges/minimum-draws
Copy and paste your code:
What is the big O runtime of your code?:
Question 2: https://www.hackerrank.com/challenges/handshake
Copy and paste your code:
What is the big O runtime of your code?:
Question 3: https://www.hackerrank.com/challenges/connecting-towns
Copy and paste your code:
What is the big O runtime of your code?:
*/
//1. The number of draws he makes in the worst case scenario is 3. I don't know how to express that in code.
//2.
func factorial(var value: Int, var result: Int = 1) -> Int {
if (value == 0) {
return result
}
result *= value
value--
return factorial(value, result: result)
}
func handshakes(attendees: Int) -> Int {
let num = factorial(attendees)
let den = (factorial(2) * factorial(attendees - 2))
let total = num / den
return total
}
handshakes(4)
handshakes(2)
//3.
func gandalfsRoute(routes: Int, towns: Int) ->Int {
let num = factorial(routes)
let den = factorial(routes - towns)
let total = num / den
return total
}
gandalfsRoute(2, towns: 2)
gandalfsRoute(3, towns: 2)