貢獻者: 鞋墊-Shoe
🧔:interviewer 👶:interviewee
🧔:一個array中,有很多個subarray,要你找出最大總和的subarray並return出該subarray的總和
👶:那想先請問這個array的長度範圍,有沒有可能是空的,以及array的element取值範圍
🧔:array長度界在1 ~ 之間,大小界在 -10^4 ~ 10^4
👶:那我會用python去實作想先請問一下,我是否能假設function的輸入為一個list
🧔:可以這樣假設,沒問題
👶:這邊他希望去找出一個total sum最大的subarray,我們想要快速知道這個subarray的"總和"(影片口誤)是多少,我會想到用preSum的方式先去紀錄0~i的總和,就能藉由下式知道subarray的total sum,就能夠將問題轉成找preSum[j] - preSum[i] (j > i)的最大值,這樣就能在固定preSum[j]的情況下去找一個最小的preSum[i] (curMin),這樣我只需要去loop preSum這個array,並且將每個值當作preSum[j],在扣掉j前面的最小element "curMin",就能找出以j結尾的subarray中之最大total sum,接著再將每個位置結尾的total sum取max就能得到我們的答案
👶:那整體的時間複雜度為O(n),空間複雜度為O(n),以上為我的演算法,請問有甚麼問題嗎 ?
🧔:聽起來是沒甚麼問題,你可以開始實作了
👶:以上為我的演算法實作後的code,請問有甚麼問題嗎 ?
🧔:希望你可以在進一步的優化空間,讓空間複雜度優化到O(1)
👶:基本上我每次loop只會用到preSum[j],所以可將兩個loop合併就不需要使用到preSum的空間
👶:以上為我的演算法實作後的code,請問這樣有符合你的要求嗎 ?
🧔:大致上是ok,那這邊還有一點時間,給一個follow up,如果用divide and conquer去實作你會怎麼處理這個問題,當然這邊時間複雜度就不一定在O(n)以內
👶:divide and conquer就是把大問題化成子問題,也就是說在index範圍i ~ j的arr中,可以將問題轉成找,i ~ k和k+1 ~ j的maxSubarray以及橫跨k的maxSubarray,那怎麼找這個橫跨k的maxSubarray呢 ? 我只需以k開頭向左邊加總找出一個最大值,在以k+1開頭向右邊加總找出一個最大值,兩值相加最會是橫跨k的maxSubarray,最後在與i ~ k和k+1 ~ j的maxSubarray比較找出最大值,就能夠找出i ~ j的maxSubarray
👶:那整體時間複雜度為O(nlogn),空間複雜度為O(logn),以上為我的演算法,請問有甚麼問題嗎 ?
🧔:聽起來是沒甚麼問題,你可以開始實作了
👶:以上為我的演算法實作後的code,請問這樣有符合你的要求嗎 ?
🧔:大致上是ok,今天的面試就到此結束謝謝
針對 interviewer 的檢討:
針對 interviewee 的檢討:
curMin
」聽起來像是「找 Kermit」,反而造成誤會。直接說「更新目前的最小值」float
?preSum
佔用的空間),應當跟 interviewer 確認,並討論這樣改善的效益何在延伸閱讀
影片
🧔:在數學上經常會計算到 x^n,希望你能夠實作出一個程式計算 x^n,並計算後的回傳數值
👶:那想先請問x、n的範圍,n有沒有可能是負的或是小數之類的
🧔:x的範圍界在-100 ~ 100之間,n的範圍界在-2^31 ~ 2^31-1之間,且n都是整數
👶:還有一個問題是我輸入0^0,會希望我的系統回傳甚麼
🧔:如果是0^0回傳1就可以了
👶:就值觀來說,解這個題目會想將n分成兩種情況處理,n>=0計算x^n,n<0計算1 / x^(-n),這樣就只需要在乎n>=0的情況來實作我們的演算法,就值觀來說就是乘x n次,也就是loop n次,這樣就能計算x^n 的數值,但是這個n的範圍大於2^31 -1,也就是已經大於10^9,數值非常大複雜度為O(n),也就是直接loop會耗費很多時間,那這樣子的話…
我想到一個方式就是累乘的方式進行,這樣x的次數會以指數的方式增長,假設需要乘t次,這樣t=2^n,t=logn,時間複雜度大約就會是logn
👶:可是n不一定是2的冪(power of 2),所以必須計算 將近n的數值,所以利用recursion計算剩餘的數值,再用相同的方式找到一個2^k將近次數的數值來計算以此類推直到n=0就回傳1
👶:那整體時間複雜度為O(logn),空間複雜度因為recursion需要cache address,所以為O(logn)
👶:那這邊假設function input為浮點數x和整數n,最後回傳x^n的數值,請問這樣假設ok嗎 ?
🧔:這樣子去定義function是可以的
👶:那目前我的演算法大致上長這樣,請問有甚麼問題嗎 ?
🧔:聽起來是沒甚麼問題,你可以開始實作了
👶:以上為我的演算法實作後的code,請問這樣有符合你的要求嗎 ?
🧔:大致上是ok,今天的面試就到此結束謝謝
Interviewer
整體檢討:在開始寫code前,做了非常詳細的說明,而且有一邊打字,很好地展現了面試者的思路。
影片
🧔:Hello, I am your interviewer. The first question is we given a grid and value of the grid is one become island, zero become water. An island is a group of 1's connected 4-directionally. And you need to find the max area of an island in the grid.
👶:Ok! We have 2D grid. "m" is grid of height and "n" is grid of width. I want to ask the m、n range. It will become empty or not ?
🧔:The m、n range will become 1~50.
👶:I think I will use the depth-first search to solve the this question. If the grid will become like this one (left grid), we will loop all the position of the grid, and use depth-first search to search the island and calculate the island area. If we start at this position ((i, j) = (1, 1)), we will search this position left, right, up and down, and search connected 4-directionally island's area. And the island area will become 5 (left grid). After we search this area, we will change grid value become zeros (right grid). And if next time loop the position again, we will not depth-first search again the island.
👶:I think I will use for loop to loop all the grid. if we find the grid value is one, we will use the depth-first search to search all area of island, and let island's value become zero like this (right grid).
👶:I think I define the function like this, input is a 2D grid. Could I define the function like this ?
🧔:Ok! You can define function like this
👶:This is my algorithm, could you have any concern ?
🧔:I think I do not have any concern. You can start to implement.
👶:Ok!
👶:This is my program. Could you have any concern ?
🧔:I think this is the good method, and thank for your repeat.