Define
V
i
such that
V
0
=
∅
and
V
n
=
P
(
V
n
−
1
)
when
n
>
0
.
Given any set
K
,
S
(
K
)
is the smallest integer
m
such that
K
∈
V
m
.
Compute the value of S for this set.
Details and Assumptions:
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Nice solution, @Arulx Z ! I really liked the one liner SSmin B = max ( ( SSmin a i ) + 1 .
I've added syntax highlighting for your code by changing 'C' to 'c'.
Why do you think it is O ( n 2 ) ? There is only one linear pass
Log in to reply
There's a recursive call, line 40. Now the question is why recursion was necessary at all.
Consider this set { { … { } … } } . Here I first over it's only member set, then it's member set and so on so time required is c ( n − 2 ) + c ( n − 4 ) + … (code is badly written). I thought that this would be easier to write but as Zach said, the recursion isn't really that necessary and this code can be written just as well without it.
Here's some GNU x86_64 code that'll do the trick. In the spirit of the other solution, it only scans the file once (that's O(n)). I figure I can assume the string is constructed correctly (or else the problem would be unsolvable). So, really, all I did was count the max depth of the braces. As running time depends on hardware and other factors, it's really not super important (but if you must know, it was too small to be measured by my zsh's time function).
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
Example run:
1 2 3 |
|
Here's the same algorithm in Python (auto grabs the internet file):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Just curious, why did you choose to use assembly?
It's a hobby. I kind of just repurposed a file reader program I wrote some time ago, though. I think it's on Rosetta Code.
And the solution was easy enough, so I felt like getting a little practice.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Problem Loading...
Note Loading...
Set Loading...
To make the hunt easier, consider a set B = { a 1 , a 2 , … , a n } . I claim that S ( B ) = max ( S ( a i ) ) + 1 for 1 ≤ i ≤ n . This is true because if a ∈ V i and b ∈ V j where j ≥ i . Then a ∈ V j .
So now the problem is reduced to parsing and recursion.
Time taken is 0.05s. Complexity of my code is O ( n 2 ) . However, as Zach pointed out, my code is just finding the maximum depth so the code can be written in Θ ( n ) time quite easily (check Zach's answer).