|
21 | 21 | },
|
22 | 22 | {
|
23 | 23 | "cell_type": "code",
|
24 |
| - "execution_count": null, |
| 24 | + "execution_count": 5, |
25 | 25 | "metadata": {},
|
26 |
| - "outputs": [], |
| 26 | + "outputs": [ |
| 27 | + { |
| 28 | + "name": "stdout", |
| 29 | + "output_type": "stream", |
| 30 | + "text": [ |
| 31 | + "1\n" |
| 32 | + ] |
| 33 | + } |
| 34 | + ], |
27 | 35 | "source": [
|
28 | 36 | "import hashlib\n",
|
29 | 37 | "\n",
|
30 | 38 | "def hash_to_range(input_string: str) -> int:\n",
|
31 | 39 | " hash_object = hashlib.sha256(input_string.encode())\n",
|
32 | 40 | " hash_int = int(hash_object.hexdigest(), 16)\n",
|
33 | 41 | " return (hash_int % 3) + 1\n",
|
34 |
| - "input_string = \"your_first_name_here\"\n", |
| 42 | + "input_string = \"antonela\"\n", |
35 | 43 | "result = hash_to_range(input_string)\n",
|
36 | 44 | "print(result)\n"
|
37 | 45 | ]
|
|
80 | 88 | },
|
81 | 89 | {
|
82 | 90 | "cell_type": "code",
|
83 |
| - "execution_count": null, |
| 91 | + "execution_count": 6, |
84 | 92 | "metadata": {},
|
85 |
| - "outputs": [], |
| 93 | + "outputs": [ |
| 94 | + { |
| 95 | + "name": "stdout", |
| 96 | + "output_type": "stream", |
| 97 | + "text": [ |
| 98 | + "1\n" |
| 99 | + ] |
| 100 | + } |
| 101 | + ], |
86 | 102 | "source": [
|
87 |
| - "# Definition for a binary tree node.\n", |
88 |
| - "# class TreeNode(object):\n", |
89 |
| - "# def __init__(self, val = 0, left = None, right = None):\n", |
90 |
| - "# self.val = val\n", |
91 |
| - "# self.left = left\n", |
92 |
| - "# self.right = right\n", |
93 |
| - "def is_duplicate(root: TreeNode) -> int:\n", |
94 |
| - " # TODO" |
| 103 | + "import hashlib\n", |
| 104 | + "\n", |
| 105 | + "def hash_to_range(input_string: str) -> int:\n", |
| 106 | + " hash_object = hashlib.sha256(input_string.encode())\n", |
| 107 | + " hash_int = int(hash_object.hexdigest(), 16)\n", |
| 108 | + " return (hash_int % 3) + 1\n", |
| 109 | + "input_string = \"antonela\"\n", |
| 110 | + "result = hash_to_range(input_string)\n", |
| 111 | + "print(result)\n", |
| 112 | + "\n" |
95 | 113 | ]
|
96 | 114 | },
|
97 | 115 | {
|
|
128 | 146 | "#### Starter Code for Question 2"
|
129 | 147 | ]
|
130 | 148 | },
|
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": 24, |
| 152 | + "metadata": {}, |
| 153 | + "outputs": [], |
| 154 | + "source": [ |
| 155 | + "from collections import deque\n", |
| 156 | + "\n", |
| 157 | + "#definition for a node in a binary tree\n", |
| 158 | + "class TreeNode:\n", |
| 159 | + " def __init__(self, val = 0, left = None, right = None):\n", |
| 160 | + " self.val = val\n", |
| 161 | + " self.left = left\n", |
| 162 | + " self.right = right\n", |
| 163 | + "\n", |
| 164 | + "def is_duplicate(root: TreeNode) -> int:\n", |
| 165 | + " if not root:\n", |
| 166 | + " return -1\n", |
| 167 | + " \n", |
| 168 | + " seen = set()\n", |
| 169 | + " queue = deque([root])\n", |
| 170 | + " \n", |
| 171 | + " while queue:\n", |
| 172 | + " node = queue.popleft()\n", |
| 173 | + " \n", |
| 174 | + " if node.val in seen:\n", |
| 175 | + " return node.val\n", |
| 176 | + " seen.add(node.val)\n", |
| 177 | + " \n", |
| 178 | + " if node.left:\n", |
| 179 | + " queue.append(node.left)\n", |
| 180 | + " if node.right:\n", |
| 181 | + " queue.append(node.right)\n", |
| 182 | + " \n", |
| 183 | + " return -1\n", |
| 184 | + "#If duplicates are not found\n" |
| 185 | + ] |
| 186 | + }, |
131 | 187 | {
|
132 | 188 | "cell_type": "code",
|
133 | 189 | "execution_count": null,
|
|
140 | 196 | "# self.val = val\n",
|
141 | 197 | "# self.left = left\n",
|
142 | 198 | "# self.right = right\n",
|
143 |
| - "def bt_path(root: TreeNode) -> List[List[int]]:\n", |
144 |
| - " # TODO" |
| 199 | + "#def bt_path(root: TreeNode) -> List[List[int]]:\n" |
145 | 200 | ]
|
146 | 201 | },
|
147 | 202 | {
|
|
182 | 237 | },
|
183 | 238 | {
|
184 | 239 | "cell_type": "code",
|
185 |
| - "execution_count": null, |
| 240 | + "execution_count": 13, |
186 | 241 | "metadata": {},
|
187 | 242 | "outputs": [],
|
188 | 243 | "source": [
|
189 |
| - "def missing_num(nums: List) -> int:\n", |
190 |
| - " # TODO" |
| 244 | + "from typing import List, Union\n", |
| 245 | + "\n", |
| 246 | + "def missing_num(nums: List[int]) -> Union[List[int], int]:\n", |
| 247 | + " n = max(len(nums), max(nums, default=-1)) #Ensures the range includes the highest number in nums\n", |
| 248 | + " expected_range = set(range(n + 1))\n", |
| 249 | + " present_set = set(nums)\n", |
| 250 | + " \n", |
| 251 | + " missing_numbers = sorted(list(expected_range - present_set))\n", |
| 252 | + " \n", |
| 253 | + " if not missing_numbers:\n", |
| 254 | + " return -1\n", |
| 255 | + " return missing_numbers\n", |
| 256 | + " " |
| 257 | + ] |
| 258 | + }, |
| 259 | + { |
| 260 | + "cell_type": "code", |
| 261 | + "execution_count": 14, |
| 262 | + "metadata": {}, |
| 263 | + "outputs": [ |
| 264 | + { |
| 265 | + "name": "stdout", |
| 266 | + "output_type": "stream", |
| 267 | + "text": [ |
| 268 | + "[1]\n", |
| 269 | + "[2, 3, 4]\n", |
| 270 | + "[4, 9]\n" |
| 271 | + ] |
| 272 | + } |
| 273 | + ], |
| 274 | + "source": [ |
| 275 | + "# Test cases\n", |
| 276 | + "# Example 1: nums = [0, 2]\n", |
| 277 | + "print(missing_num([0, 2]))\n", |
| 278 | + "\n", |
| 279 | + "# Example 2: nums = [5, 0, 1]\n", |
| 280 | + "print(missing_num([5, 0, 1]))\n", |
| 281 | + "\n", |
| 282 | + "# Example 3: nums = [6, 8, 2, 3, 5, 7, 0, 1, 10]\n", |
| 283 | + "print(missing_num([6, 8, 2, 3, 5, 7, 0, 1, 10]))\n" |
191 | 284 | ]
|
192 | 285 | },
|
193 | 286 | {
|
|
201 | 294 | ]
|
202 | 295 | },
|
203 | 296 | {
|
204 |
| - "cell_type": "code", |
205 |
| - "execution_count": null, |
| 297 | + "cell_type": "markdown", |
206 | 298 | "metadata": {},
|
207 |
| - "outputs": [], |
208 | 299 | "source": [
|
209 |
| - "# Your answer here" |
| 300 | + "The key approach is to detect and manage all missing numbers within the specified range while handling duplicates, even as the input list length varies. If any numbers are missing, the function returns -1.\n" |
210 | 301 | ]
|
211 | 302 | },
|
212 | 303 | {
|
|
218 | 309 | },
|
219 | 310 | {
|
220 | 311 | "cell_type": "code",
|
221 |
| - "execution_count": null, |
| 312 | + "execution_count": 20, |
222 | 313 | "metadata": {},
|
223 |
| - "outputs": [], |
| 314 | + "outputs": [ |
| 315 | + { |
| 316 | + "name": "stdout", |
| 317 | + "output_type": "stream", |
| 318 | + "text": [ |
| 319 | + "2\n", |
| 320 | + "10\n" |
| 321 | + ] |
| 322 | + } |
| 323 | + ], |
224 | 324 | "source": [
|
225 |
| - "# Your answer here" |
| 325 | + "# Your answer here\n", |
| 326 | + "\n", |
| 327 | + "#Input for both examples\n", |
| 328 | + "root1 = TreeNode(1)\n", |
| 329 | + "root1.left = TreeNode(2, TreeNode(3), TreeNode(5))\n", |
| 330 | + "root1.right = TreeNode(2, TreeNode(6), TreeNode(7))\n", |
| 331 | + "\n", |
| 332 | + "root2 = TreeNode(1)\n", |
| 333 | + "root2.left = TreeNode(10, TreeNode(3), TreeNode(10))\n", |
| 334 | + "root2.right = TreeNode(2, TreeNode(12), TreeNode(12))\n", |
| 335 | + "\n", |
| 336 | + "#Retults\n", |
| 337 | + "print(is_duplicate(root1))\n", |
| 338 | + "print(is_duplicate(root2))\n" |
226 | 339 | ]
|
227 | 340 | },
|
228 | 341 | {
|
|
235 | 348 | },
|
236 | 349 | {
|
237 | 350 | "cell_type": "code",
|
238 |
| - "execution_count": null, |
| 351 | + "execution_count": 21, |
239 | 352 | "metadata": {},
|
240 | 353 | "outputs": [],
|
241 | 354 | "source": [
|
242 |
| - "# Your answer here" |
| 355 | + "# Your answer here\n", |
| 356 | + "\n", |
| 357 | + "class TreeNode(object):\n", |
| 358 | + " def __init__(self, val=0, left=None, right=None):\n", |
| 359 | + " self.val = val\n", |
| 360 | + " self.left = left\n", |
| 361 | + " self.right = right\n", |
| 362 | + "\n", |
| 363 | + "def is_duplicate(root: TreeNode) -> int:\n", |
| 364 | + " if not root:\n", |
| 365 | + " return -1\n", |
| 366 | + " \n", |
| 367 | + " seen = set()\n", |
| 368 | + " queue = deque([root])\n", |
| 369 | + " \n", |
| 370 | + " while queue:\n", |
| 371 | + " node = queue.popleft()\n", |
| 372 | + " \n", |
| 373 | + " if node.val in seen:\n", |
| 374 | + " return node.val\n", |
| 375 | + " seen.add(node.val)\n", |
| 376 | + " \n", |
| 377 | + " if node.left:\n", |
| 378 | + " queue.append(node.left)\n", |
| 379 | + " if node.right:\n", |
| 380 | + " queue.append(node.right)\n", |
| 381 | + " \n", |
| 382 | + " return -1\n" |
| 383 | + ] |
| 384 | + }, |
| 385 | + { |
| 386 | + "cell_type": "code", |
| 387 | + "execution_count": 22, |
| 388 | + "metadata": {}, |
| 389 | + "outputs": [ |
| 390 | + { |
| 391 | + "name": "stdout", |
| 392 | + "output_type": "stream", |
| 393 | + "text": [ |
| 394 | + "2\n", |
| 395 | + "-1\n" |
| 396 | + ] |
| 397 | + } |
| 398 | + ], |
| 399 | + "source": [ |
| 400 | + "#Test cases\n", |
| 401 | + "#New Example 1: root = [1, 2, 3, 4, 2]\n", |
| 402 | + "root1 = TreeNode(1)\n", |
| 403 | + "root1.left = TreeNode(2)\n", |
| 404 | + "root1.right = TreeNode(3)\n", |
| 405 | + "root1.left.left = TreeNode(4)\n", |
| 406 | + "root1.left.right = TreeNode(2)\n", |
| 407 | + "\n", |
| 408 | + "#New Example 2: root = [1, 2, 3, 4, 5, 6, 7]\n", |
| 409 | + "root2 = TreeNode(1)\n", |
| 410 | + "root2.left = TreeNode(2)\n", |
| 411 | + "root2.right = TreeNode(3)\n", |
| 412 | + "root2.left.left = TreeNode(4)\n", |
| 413 | + "root2.left.right = TreeNode(5)\n", |
| 414 | + "root2.right.left = TreeNode(6)\n", |
| 415 | + "root2.right.right = TreeNode(7)\n", |
| 416 | + "\n", |
| 417 | + "print(is_duplicate(root1)) # Output: 2\n", |
| 418 | + "print(is_duplicate(root2)) # Output: -1\n" |
243 | 419 | ]
|
244 | 420 | },
|
245 | 421 | {
|
|
251 | 427 | ]
|
252 | 428 | },
|
253 | 429 | {
|
254 |
| - "cell_type": "code", |
255 |
| - "execution_count": null, |
| 430 | + "cell_type": "markdown", |
256 | 431 | "metadata": {},
|
257 |
| - "outputs": [], |
258 | 432 | "source": [
|
259 |
| - "# Your answer here" |
| 433 | + "My solution works by using BFS, which means going through the tree level by level using a queue. I also keep track of the values I’ve seen in a set. As I go through the tree, if I find a value that’s already in the set, I return it as the first duplicate (the one closest to the root). If there are no duplicates, I return -1.\n" |
260 | 434 | ]
|
261 | 435 | },
|
262 | 436 | {
|
|
268 | 442 | ]
|
269 | 443 | },
|
270 | 444 | {
|
271 |
| - "cell_type": "code", |
272 |
| - "execution_count": null, |
| 445 | + "cell_type": "markdown", |
273 | 446 | "metadata": {},
|
274 |
| - "outputs": [], |
275 | 447 | "source": [
|
276 |
| - "# Your answer here" |
| 448 | + "The time complexity is O(N) because the solution goes through each node in the tree once. The space complexity is also O(N) because it stores data in the recursion stack and keeps track of paths in a list.\n" |
277 | 449 | ]
|
278 | 450 | },
|
279 | 451 | {
|
|
285 | 457 | ]
|
286 | 458 | },
|
287 | 459 | {
|
288 |
| - "cell_type": "code", |
289 |
| - "execution_count": null, |
| 460 | + "cell_type": "markdown", |
290 | 461 | "metadata": {},
|
291 |
| - "outputs": [], |
292 | 462 | "source": [
|
293 |
| - "# Your answer here" |
| 463 | + "To find duplicates:\n", |
| 464 | + "Use Depth-First Search to find the first duplicate value as you go deeper into the tree. Keep track of values in a set: \n", |
| 465 | + "- If a value is already in the set, return it. \n", |
| 466 | + "- If not, add it and continue searching. \n", |
| 467 | + "- If no duplicates are found, return -1. \n", |
| 468 | + "\n", |
| 469 | + "To find all paths: \n", |
| 470 | + "Use Breadth-First Search to find all root-to-leaf paths. Keep a queue to track nodes and their paths: \n", |
| 471 | + "- If a node is a leaf, save its path. \n", |
| 472 | + "- If it has children, add them to the queue with updated paths. \n", |
| 473 | + "- Continue until all paths are collected.\n" |
294 | 474 | ]
|
295 | 475 | },
|
296 | 476 | {
|
|
338 | 518 | ],
|
339 | 519 | "metadata": {
|
340 | 520 | "kernelspec": {
|
341 |
| - "display_name": "Python 3", |
| 521 | + "display_name": "dsi_participant", |
342 | 522 | "language": "python",
|
343 | 523 | "name": "python3"
|
344 | 524 | },
|
|
352 | 532 | "name": "python",
|
353 | 533 | "nbconvert_exporter": "python",
|
354 | 534 | "pygments_lexer": "ipython3",
|
355 |
| - "version": "3.11.7" |
| 535 | + "version": "3.9.20" |
356 | 536 | }
|
357 | 537 | },
|
358 | 538 | "nbformat": 4,
|
|
0 commit comments