貢獻者: 奧黛麗-Audrey
video:
模擬面試影片(漢)
模擬面試影片(英)
R: Interviewer
E: Interviewee
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
R: 在第一道題目中,會給你一個整數陣列以及一個目標值。請你在這個整數陣列中,找出兩個整數相加要等於題目給定的目標值,而答案就是要以陣列的形式,回傳你找到這兩個整數的索引值。你可以假設一組輸入就恰好會有一組答案,而且陣列裡的元素不會重複使用。
E: 想請問這個陣列是排序過的嗎?
R: 不是。
E: 我想我沒有問題了。但為了確認我對題目的理解是不是正確的,我想舉個例子。假設題目輸入的陣列是 [2,13,9,1],目標值是3,這樣要回傳的答案就 [0,3] 對嗎?
R: 沒錯。
E: 我目前比較直覺的想法是用雙層的for迴圈來實作。第一層迴圈是找第一個數字,第二層迴圈去找要相加的第二個數字,最後把整個陣列走過一次,就可以得到所有相加的可能組合,在過程中只要得到答案就直接回傳。
R: 程式是沒有問題,但很明顯,雙層迴圈對於時間複雜度而言,並不是很有效的做法,針對這點你有什麼想法嗎?
E: 如果是要降低時間複雜度的話,我想可以用hash table來實作,針對每一個元素建立一個hash table,去存他們的index,之後再用一個for迴圈去算目標值和原本陣列的每一個元素相減得到的差,如果存在於這個hash table中,就代表找到解了;這個方法實作起來就是兩個單層的for迴圈,將時間複雜度從原本的 降低到 。
R: 因為我們公司有做一些影像處理的案子,所以這題是關於影像的應用。我自己很常在拍一些證件或文件的時候,忘記把手機轉正,之後的後製就需要來旋轉拍好的照片。所以接下來的題目主要就是想模擬一下旋轉影像的過程,我剛有把示意圖放在google文件的第二頁,我們會給你一個matrix,希望你能將這個用來模擬影像旋轉180度。
E: 根據我的觀察,若是要做180度的旋轉,其實只要針對column跟row都各做一次反轉就好。像是先在每一個row做反轉的話,就會變成[[3,2,1],[6,5,4],[9,8,7]],之後再對column做反轉就會達到題目要求的[[9,8,7],[6,5,4],[3,2,1]]
R: 如果是要將影像順時針轉90度的話,你會怎麼做呢?
E: 先將矩陣轉置,之後針對每一個row裡面的元素reverse,就能達到順時針旋轉90度。以原圖為例,原本的[[1,2,3],[4,5,6],[7,8,9]]經轉置後會變成[[1,4,7],[2,5,8],[3,6,9]],之後在每個row做reverse,得[[7,4,1],[8,5,2],[9,6,3]]。
R: 恭喜你完成今天的面試,後續有任何消息,我們同仁會再通知你,期待下次再見。
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
R: Any questions?
E: Is the array sorted?
R: No.
E: My initial approach is to use hash table to record the number of times of each element appears.
R: It's a nice solution. However, I hope you can reduce the space complexity. Could you solve the problem in O(1) space?
E: To reduce space complexity, I will sort the array first. Since the majority element always exists, and according to the definition, it appears more than the floor of (n/2) times, the middle of the sorted array must be the majority element. In this solution, the space complextity will be O(1).
R: Good, it fulfills the requirement. However, it seems like a trade-off between space complexity and time complexity. Do you have another solution to keep the time complexity in linear time when you reduce the space complexity?
E: I have come up with the concept of Moore Voting. In this solution, two variables, candidate and count, are required. First, choose the first element as candidate and iterate through the list. For each item, check if it's the same as the current candidate. If it is, increment the count; if not, decrement the count. When the count reaches zero, just update the candidate to the current item. After iterating through the entire list, the candidate will hold the majority element if it exists.
R: Good job! I think we have finished our interview today. Thanks for your time and hope to see you again!