|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 3, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from typing import List, Dict" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 8, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "class Solution:\n", |
| 19 | + " def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:\n", |
| 20 | + " l = []\n", |
| 21 | + " for i in words:\n", |
| 22 | + " l.extend(i.split(separator))\n", |
| 23 | + " return [i for i in l if '' != i]" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": 11, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "data": { |
| 33 | + "text/plain": [ |
| 34 | + "['one', 'two', 'three', 'four', 'five', 'six']" |
| 35 | + ] |
| 36 | + }, |
| 37 | + "execution_count": 11, |
| 38 | + "metadata": {}, |
| 39 | + "output_type": "execute_result" |
| 40 | + } |
| 41 | + ], |
| 42 | + "source": [ |
| 43 | + "words = [\"|||\"]; separator = \"|\"\n", |
| 44 | + "words = [\"$easy$\",\"$problem$\"]; separator = \"$\"\n", |
| 45 | + "words = [\"one.two.three\",\"four.five\",\"six\"]; separator = \".\"\n", |
| 46 | + "Solution().splitWordsBySeparator(words, separator)" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": null, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 65, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "class Solution:\n", |
| 63 | + " def maxArrayValue(self, nums: List[int]) -> int:\n", |
| 64 | + " length = len(nums)\n", |
| 65 | + " \n", |
| 66 | + " def rec(new_arr, length):\n", |
| 67 | + " max_ = float('-inf')\n", |
| 68 | + " \n", |
| 69 | + " for i in range(length):\n", |
| 70 | + " if new_arr[i] <= new_arr[i+1]:\n", |
| 71 | + " max_ = max( max_, rec(new_arr[:i] + [new_arr[i]+new_arr[i+1]] + new_arr[i+2:], length-1))\n", |
| 72 | + " \n", |
| 73 | + " if max_ == float('-inf'): \n", |
| 74 | + " return max(new_arr)\n", |
| 75 | + " \n", |
| 76 | + " return max_\n", |
| 77 | + " \n", |
| 78 | + " result = rec(nums, length-1)\n", |
| 79 | + " return result " |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": 162, |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [], |
| 87 | + "source": [ |
| 88 | + "from typing import List\n", |
| 89 | + "from functools import cache\n", |
| 90 | + "\n", |
| 91 | + "class Solution:\n", |
| 92 | + " def maxArrayValue(self, nums: List[int]) -> int:\n", |
| 93 | + " length = len(nums)\n", |
| 94 | + " \n", |
| 95 | + " memo = {}\n", |
| 96 | + " \n", |
| 97 | + " @cache\n", |
| 98 | + " def rec(new_arr, length):\n", |
| 99 | + " if length == 0: return new_arr[0]\n", |
| 100 | + " \n", |
| 101 | + " if (tuple(new_arr), length) in memo:\n", |
| 102 | + " return memo[(tuple(new_arr), length)]\n", |
| 103 | + " \n", |
| 104 | + " max_ = float('-inf')\n", |
| 105 | + " for i in range(length):\n", |
| 106 | + " if new_arr[i] <= new_arr[i+1]:\n", |
| 107 | + " new_val = new_arr[i] + new_arr[i+1]\n", |
| 108 | + " max_ = max(max_, rec( tuple(list(new_arr[:i]) + [new_val] + list(new_arr[i+2:])), length-1))\n", |
| 109 | + " \n", |
| 110 | + " if max_ == float('-inf'):\n", |
| 111 | + " memo[(tuple(new_arr), length)] = max(new_arr)\n", |
| 112 | + " return max(new_arr)\n", |
| 113 | + " \n", |
| 114 | + " memo[(tuple(new_arr), length)] = max_\n", |
| 115 | + " return max_\n", |
| 116 | + " \n", |
| 117 | + " result = rec(tuple(nums), length-1)\n", |
| 118 | + " return result\n" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": 167, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [ |
| 126 | + { |
| 127 | + "name": "stdout", |
| 128 | + "output_type": "stream", |
| 129 | + "text": [ |
| 130 | + "1207\n" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "data": { |
| 135 | + "text/plain": [ |
| 136 | + "0.8667755126953125" |
| 137 | + ] |
| 138 | + }, |
| 139 | + "execution_count": 167, |
| 140 | + "metadata": {}, |
| 141 | + "output_type": "execute_result" |
| 142 | + } |
| 143 | + ], |
| 144 | + "source": [ |
| 145 | + "nums = [2,3,7,9,3] # 21\n", |
| 146 | + "nums = [5,3,3] # 11\n", |
| 147 | + "\n", |
| 148 | + "nums = [40,15,35,98,77,79,24,62,53,84,97,16,30,22,49] # 781\n", |
| 149 | + "nums = [71,4,53,51,9,92,91,86,84,58,31,39,38,49,56,27,91,17,10,56,52,78,35,76,39] # 1254\n", |
| 150 | + "nums = [89,49,59,59,2,77,55,44,51,47,100,77,30,71,47,100,13,17,12,38,26,55,89,41] # 1207\n", |
| 151 | + "a = time.time()\n", |
| 152 | + "r = Solution().maxArrayValue(nums)\n", |
| 153 | + "print(r)\n", |
| 154 | + "b = time.time()\n", |
| 155 | + "b-a" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": 66, |
| 161 | + "metadata": {}, |
| 162 | + "outputs": [ |
| 163 | + { |
| 164 | + "name": "stdout", |
| 165 | + "output_type": "stream", |
| 166 | + "text": [ |
| 167 | + "3.76 μs ± 241 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" |
| 168 | + ] |
| 169 | + } |
| 170 | + ], |
| 171 | + "source": [ |
| 172 | + "%timeit Solution().maxArrayValue(nums)" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": 174, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "data": { |
| 182 | + "text/plain": [ |
| 183 | + "21" |
| 184 | + ] |
| 185 | + }, |
| 186 | + "execution_count": 174, |
| 187 | + "metadata": {}, |
| 188 | + "output_type": "execute_result" |
| 189 | + } |
| 190 | + ], |
| 191 | + "source": [ |
| 192 | + "from typing import List\n", |
| 193 | + "\n", |
| 194 | + "class Solution:\n", |
| 195 | + " def maxArrayValue(self, nums: List[int]) -> int:\n", |
| 196 | + " n = len(nums)\n", |
| 197 | + " \n", |
| 198 | + " if n == 1: return nums[0]\n", |
| 199 | + " \n", |
| 200 | + " prev_val = nums[n - 1]\n", |
| 201 | + " max_val = nums[n - 1]\n", |
| 202 | + " \n", |
| 203 | + " for i in range(n - 2, -1, -1):\n", |
| 204 | + " if nums[i] <= prev_val:\n", |
| 205 | + " prev_val = nums[i] + prev_val\n", |
| 206 | + " else:\n", |
| 207 | + " prev_val = nums[i]\n", |
| 208 | + " \n", |
| 209 | + " max_val = max(max_val, prev_val)\n", |
| 210 | + " \n", |
| 211 | + " return max_val\n", |
| 212 | + "\n", |
| 213 | + "nums = [2,3,7,9,3] # 21\n", |
| 214 | + "#nums = [5,3,3] # 11\n", |
| 215 | + "\n", |
| 216 | + "#nums = [40,15,35,98,77,79,24,62,53,84,97,16,30,22,49] # 781\n", |
| 217 | + "#nums = [71,4,53,51,9,92,91,86,84,58,31,39,38,49,56,27,91,17,10,56,52,78,35,76,39] # 1254\n", |
| 218 | + "#nums = [89,49,59,59,2,77,55,44,51,47,100,77,30,71,47,100,13,17,12,38,26,55,89,41] # 1207\n", |
| 219 | + "\n", |
| 220 | + "Solution().maxArrayValue(nums)\n" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "code", |
| 225 | + "execution_count": 198, |
| 226 | + "metadata": {}, |
| 227 | + "outputs": [ |
| 228 | + { |
| 229 | + "name": "stdout", |
| 230 | + "output_type": "stream", |
| 231 | + "text": [ |
| 232 | + "0\n" |
| 233 | + ] |
| 234 | + } |
| 235 | + ], |
| 236 | + "source": [ |
| 237 | + "from collections import defaultdict\n", |
| 238 | + "from typing import List\n", |
| 239 | + "\n", |
| 240 | + "class Node:\n", |
| 241 | + " def __init__(self, x):\n", |
| 242 | + " self.data = x\n", |
| 243 | + " self.children = []\n", |
| 244 | + "\n", |
| 245 | + "class Solution:\n", |
| 246 | + " def countPalindromePaths(self, parent: List[int], s: str) -> int:\n", |
| 247 | + " def checkPalin(freq):\n", |
| 248 | + " oddCount = 0\n", |
| 249 | + " for x in freq:\n", |
| 250 | + " if freq[x] % 2 == 1:\n", |
| 251 | + " oddCount += 1\n", |
| 252 | + " return oddCount <= 1\n", |
| 253 | + "\n", |
| 254 | + " def cntpalin(root, s, freq, ans_list):\n", |
| 255 | + " if root is None:\n", |
| 256 | + " return\n", |
| 257 | + "\n", |
| 258 | + " freq[root.data] += 1\n", |
| 259 | + "\n", |
| 260 | + " if root.data != 0:\n", |
| 261 | + " s += s_dict[root.data]\n", |
| 262 | + " u = root.data\n", |
| 263 | + " v = 0\n", |
| 264 | + " if parent[u] != -1:\n", |
| 265 | + " v = parent[u]\n", |
| 266 | + " path_chars = s\n", |
| 267 | + " while v != 0:\n", |
| 268 | + " path_chars += s_dict[v]\n", |
| 269 | + " v = parent[v]\n", |
| 270 | + " if checkPalin(freq):\n", |
| 271 | + " ans_list[0] += 1\n", |
| 272 | + "\n", |
| 273 | + " for child in root.children:\n", |
| 274 | + " cntpalin(child, s, freq, ans_list)\n", |
| 275 | + "\n", |
| 276 | + " freq[root.data] -= 1\n", |
| 277 | + "\n", |
| 278 | + " def construct_tree(parent):\n", |
| 279 | + " root = Node(0)\n", |
| 280 | + " for i, p in enumerate(parent):\n", |
| 281 | + " if i != 0:\n", |
| 282 | + " node = Node(i)\n", |
| 283 | + " root.children.append(node)\n", |
| 284 | + " return root\n", |
| 285 | + "\n", |
| 286 | + " n = len(parent)\n", |
| 287 | + " s_dict = {i: s[i] for i in range(n)}\n", |
| 288 | + " ans_list = [0] # A list to store the count of pairs as a mutable value\n", |
| 289 | + " root = construct_tree(parent)\n", |
| 290 | + " freq = defaultdict(int)\n", |
| 291 | + "\n", |
| 292 | + " cntpalin(root, '', freq, ans_list)\n", |
| 293 | + "\n", |
| 294 | + " return ans_list[0]\n", |
| 295 | + "\n", |
| 296 | + "# Test the function\n", |
| 297 | + "if __name__ == '__main__':\n", |
| 298 | + " parent = [-1, 0, 0, 1, 1, 2]\n", |
| 299 | + " s = \"acaabc\"\n", |
| 300 | + " solution = Solution()\n", |
| 301 | + " print(solution.countPalindromePaths(parent, s)) # Output: 8\n" |
| 302 | + ] |
| 303 | + }, |
| 304 | + { |
| 305 | + "cell_type": "code", |
| 306 | + "execution_count": 199, |
| 307 | + "metadata": {}, |
| 308 | + "outputs": [], |
| 309 | + "source": [ |
| 310 | + "from collections import Counter" |
| 311 | + ] |
| 312 | + }, |
| 313 | + { |
| 314 | + "cell_type": "code", |
| 315 | + "execution_count": 206, |
| 316 | + "metadata": {}, |
| 317 | + "outputs": [ |
| 318 | + { |
| 319 | + "data": { |
| 320 | + "text/plain": [ |
| 321 | + "['h', 't']" |
| 322 | + ] |
| 323 | + }, |
| 324 | + "execution_count": 206, |
| 325 | + "metadata": {}, |
| 326 | + "output_type": "execute_result" |
| 327 | + } |
| 328 | + ], |
| 329 | + "source": [ |
| 330 | + "s1 = 'Python'\n", |
| 331 | + "s2 = 'HackerEarth'\n", |
| 332 | + "d = Counter(s1) & Counter(s2)\n", |
| 333 | + "sorted(list(d.elements()))" |
| 334 | + ] |
| 335 | + } |
| 336 | + ], |
| 337 | + "metadata": { |
| 338 | + "kernelspec": { |
| 339 | + "display_name": "base", |
| 340 | + "language": "python", |
| 341 | + "name": "python3" |
| 342 | + }, |
| 343 | + "language_info": { |
| 344 | + "codemirror_mode": { |
| 345 | + "name": "ipython", |
| 346 | + "version": 3 |
| 347 | + }, |
| 348 | + "file_extension": ".py", |
| 349 | + "mimetype": "text/x-python", |
| 350 | + "name": "python", |
| 351 | + "nbconvert_exporter": "python", |
| 352 | + "pygments_lexer": "ipython3", |
| 353 | + "version": "3.9.13" |
| 354 | + }, |
| 355 | + "orig_nbformat": 4 |
| 356 | + }, |
| 357 | + "nbformat": 4, |
| 358 | + "nbformat_minor": 2 |
| 359 | +} |
0 commit comments