|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Question 1: Count Negative Numbers in a Sorted Matrix, by Amazon and Apple\n", |
| 8 | + "- Given a m x n matrix grid which is sorted in non-increasing order both row-wise and column-wise, return the number of negative numbers in grid.\n", |
| 9 | + "- https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 14, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [ |
| 17 | + { |
| 18 | + "data": { |
| 19 | + "text/plain": [ |
| 20 | + "8" |
| 21 | + ] |
| 22 | + }, |
| 23 | + "execution_count": 14, |
| 24 | + "metadata": {}, |
| 25 | + "output_type": "execute_result" |
| 26 | + } |
| 27 | + ], |
| 28 | + "source": [ |
| 29 | + "# solution 1: brutal force \n", |
| 30 | + "def countNegatives(grid):\n", |
| 31 | + " count =0 \n", |
| 32 | + " for i in range(len(grid)):\n", |
| 33 | + " for j in range(len(grid[0])):\n", |
| 34 | + " if grid[i][j]<0:\n", |
| 35 | + " count+=1\n", |
| 36 | + " return count\n", |
| 37 | + "\n", |
| 38 | + "# test case \n", |
| 39 | + "grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]\n", |
| 40 | + "countNegatives(grid)" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 15, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [ |
| 48 | + { |
| 49 | + "data": { |
| 50 | + "text/plain": [ |
| 51 | + "8" |
| 52 | + ] |
| 53 | + }, |
| 54 | + "execution_count": 15, |
| 55 | + "metadata": {}, |
| 56 | + "output_type": "execute_result" |
| 57 | + } |
| 58 | + ], |
| 59 | + "source": [ |
| 60 | + "# solution 2: import package \n", |
| 61 | + "def countNegatives(grid):\n", |
| 62 | + " import numpy as np\n", |
| 63 | + " count = 0 \n", |
| 64 | + " grip = np.array(grid)\n", |
| 65 | + " result = grip.flatten()\n", |
| 66 | + " for i in result:\n", |
| 67 | + " if i < 0:\n", |
| 68 | + " count+=1\n", |
| 69 | + " return count\n", |
| 70 | + "\n", |
| 71 | + "# test case \n", |
| 72 | + "grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]\n", |
| 73 | + "countNegatives(grid)" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 21, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [ |
| 81 | + { |
| 82 | + "data": { |
| 83 | + "text/plain": [ |
| 84 | + "8" |
| 85 | + ] |
| 86 | + }, |
| 87 | + "execution_count": 21, |
| 88 | + "metadata": {}, |
| 89 | + "output_type": "execute_result" |
| 90 | + } |
| 91 | + ], |
| 92 | + "source": [ |
| 93 | + "# solution 3: binary search \n", |
| 94 | + "def countNegatives(grid):\n", |
| 95 | + " \n", |
| 96 | + " n = len(grid[0])-1 # column numbers\n", |
| 97 | + " \n", |
| 98 | + " count = 0 # initialize the count\n", |
| 99 | + " \n", |
| 100 | + " for row in grid: # iterate over the rows\n", |
| 101 | + " \n", |
| 102 | + " low, high = 0, n # set searching space: low and high\n", |
| 103 | + " \n", |
| 104 | + " while low <= high: # while the condition is met\n", |
| 105 | + " \n", |
| 106 | + " mid = low + (high-low)//2 # set the third search space: middle \n", |
| 107 | + " \n", |
| 108 | + " if row[mid] > -1: # if the selected value is bigger than -1\n", |
| 109 | + " low = mid + 1 # move the searching space to the right \n", |
| 110 | + " \n", |
| 111 | + " else: # if the selected value is smaller than or equal to -1: \n", |
| 112 | + " high = mid - 1 # move the searching space to the left\n", |
| 113 | + "\n", |
| 114 | + " if low <= n: # outside the while loop, we compare the leftmost searching point to n\n", |
| 115 | + " count += (n+1)-low # everything on the right side of low is negative\n", |
| 116 | + " \n", |
| 117 | + " return count\n", |
| 118 | + " \n", |
| 119 | + "# test case \n", |
| 120 | + "grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]\n", |
| 121 | + "countNegatives(grid)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "metadata": {}, |
| 127 | + "source": [ |
| 128 | + "---" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "metadata": {}, |
| 134 | + "source": [ |
| 135 | + "# Question 2: Replace Elements with Greatest Element on Right Side, by Amazon\n", |
| 136 | + "- Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.\n", |
| 137 | + "- After doing so, return the array.\n", |
| 138 | + "- https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 17, |
| 144 | + "metadata": {}, |
| 145 | + "outputs": [ |
| 146 | + { |
| 147 | + "data": { |
| 148 | + "text/plain": [ |
| 149 | + "[18, 6, 6, 6, 1, -1]" |
| 150 | + ] |
| 151 | + }, |
| 152 | + "execution_count": 17, |
| 153 | + "metadata": {}, |
| 154 | + "output_type": "execute_result" |
| 155 | + } |
| 156 | + ], |
| 157 | + "source": [ |
| 158 | + "# solution 1: brutal force\n", |
| 159 | + "def replaceElements(arr):\n", |
| 160 | + " \n", |
| 161 | + " new = []\n", |
| 162 | + " \n", |
| 163 | + " for i in range(len(arr)-1):\n", |
| 164 | + " new.append(max(arr[i+1:]))\n", |
| 165 | + " \n", |
| 166 | + " new +=[-1]\n", |
| 167 | + " \n", |
| 168 | + " return new\n", |
| 169 | + "\n", |
| 170 | + "#test case \n", |
| 171 | + "arr = [17,18,5,4,6,1]\n", |
| 172 | + "replaceElements(arr)" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": 11, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "data": { |
| 182 | + "text/plain": [ |
| 183 | + "[18, 6, 6, 6, 1, -1]" |
| 184 | + ] |
| 185 | + }, |
| 186 | + "execution_count": 11, |
| 187 | + "metadata": {}, |
| 188 | + "output_type": "execute_result" |
| 189 | + } |
| 190 | + ], |
| 191 | + "source": [ |
| 192 | + "# solution 2: \n", |
| 193 | + "def replaceElements(arr):\n", |
| 194 | + " max_value = -1 \n", |
| 195 | + " for i in range(len(arr)-1,-1,-1):\n", |
| 196 | + " arr[i],max_value = max_value,max(max_value,arr[i])\n", |
| 197 | + " return arr\n", |
| 198 | + "\n", |
| 199 | + "#test case \n", |
| 200 | + "arr = [17,18,5,4,6,1]\n", |
| 201 | + "replaceElements(arr)" |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "markdown", |
| 206 | + "metadata": {}, |
| 207 | + "source": [ |
| 208 | + "----" |
| 209 | + ] |
| 210 | + }, |
| 211 | + { |
| 212 | + "cell_type": "markdown", |
| 213 | + "metadata": {}, |
| 214 | + "source": [ |
| 215 | + "# Question 3: Three Consecutive Odds, by Dajiang \n", |
| 216 | + "- Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.\n", |
| 217 | + "- https://leetcode.com/problems/three-consecutive-odds/" |
| 218 | + ] |
| 219 | + }, |
| 220 | + { |
| 221 | + "cell_type": "code", |
| 222 | + "execution_count": 23, |
| 223 | + "metadata": {}, |
| 224 | + "outputs": [ |
| 225 | + { |
| 226 | + "data": { |
| 227 | + "text/plain": [ |
| 228 | + "False" |
| 229 | + ] |
| 230 | + }, |
| 231 | + "execution_count": 23, |
| 232 | + "metadata": {}, |
| 233 | + "output_type": "execute_result" |
| 234 | + } |
| 235 | + ], |
| 236 | + "source": [ |
| 237 | + "# solution 1: brutal force\n", |
| 238 | + "def three_consecutive_odds(arr):\n", |
| 239 | + " for i in range(len(arr)-2):\n", |
| 240 | + " if arr[i]%2 != 0 and arr[i+1]%2 != 0 and arr[i+2]%2 != 0:\n", |
| 241 | + " return True\n", |
| 242 | + " else: # extra attention: the position/indentation of else!\n", |
| 243 | + " return False\n", |
| 244 | + " \n", |
| 245 | + "# test case \n", |
| 246 | + "arr = [2,6,4,1]\n", |
| 247 | + "three_consecutive_odds(arr)" |
| 248 | + ] |
| 249 | + }, |
| 250 | + { |
| 251 | + "cell_type": "code", |
| 252 | + "execution_count": 24, |
| 253 | + "metadata": {}, |
| 254 | + "outputs": [ |
| 255 | + { |
| 256 | + "data": { |
| 257 | + "text/plain": [ |
| 258 | + "False" |
| 259 | + ] |
| 260 | + }, |
| 261 | + "execution_count": 24, |
| 262 | + "metadata": {}, |
| 263 | + "output_type": "execute_result" |
| 264 | + } |
| 265 | + ], |
| 266 | + "source": [ |
| 267 | + "# solution 2 \n", |
| 268 | + "def three_consecutive_odds(arr):\n", |
| 269 | + " count=0\n", |
| 270 | + " for i in range(len(arr)):\n", |
| 271 | + " if arr[i]%2 != 0:\n", |
| 272 | + " count+=1\n", |
| 273 | + " \n", |
| 274 | + " else:\n", |
| 275 | + " count =0 \n", |
| 276 | + " \n", |
| 277 | + " if count==3:\n", |
| 278 | + " return True\n", |
| 279 | + " \n", |
| 280 | + " else: # extra attention: the position/indentation of else!\n", |
| 281 | + " return False\n", |
| 282 | + " \n", |
| 283 | + "# test case \n", |
| 284 | + "arr = [2,6,4,1]\n", |
| 285 | + "three_consecutive_odds(arr)" |
| 286 | + ] |
| 287 | + }, |
| 288 | + { |
| 289 | + "cell_type": "markdown", |
| 290 | + "metadata": {}, |
| 291 | + "source": [ |
| 292 | + "----" |
| 293 | + ] |
| 294 | + } |
| 295 | + ], |
| 296 | + "metadata": { |
| 297 | + "kernelspec": { |
| 298 | + "display_name": "Python 3", |
| 299 | + "language": "python", |
| 300 | + "name": "python3" |
| 301 | + }, |
| 302 | + "language_info": { |
| 303 | + "codemirror_mode": { |
| 304 | + "name": "ipython", |
| 305 | + "version": 3 |
| 306 | + }, |
| 307 | + "file_extension": ".py", |
| 308 | + "mimetype": "text/x-python", |
| 309 | + "name": "python", |
| 310 | + "nbconvert_exporter": "python", |
| 311 | + "pygments_lexer": "ipython3", |
| 312 | + "version": "3.7.4" |
| 313 | + } |
| 314 | + }, |
| 315 | + "nbformat": 4, |
| 316 | + "nbformat_minor": 2 |
| 317 | +} |
0 commit comments