Given a static-sized array of integers arr, move all zeroes in the array to the end of the array. You should preserve the relative order of items in the array.
We should implement a solution that is more efficient than a naive brute force.
Examples:
Constraints:
arr
p1
指向 0 的位置,為下個應該要被設置非零數值的位置p2
指向非 0 的位置,為下個 p1
位置應該要設置的數字持續交換 p1
及 p2
即可得到答案
Time complexity: array 中每個 index 最多只會看到一次,複雜度為
Space complexity: 需要記錄回傳的陣列,複雜度為
arr
is const
type)這題其實很簡單而且以前有做過,但當下太緊張且一直嘗試理解 interviewer 所說的話,應該要與 interviewer 溝通並請她給我點時間思考。
Given a package with a weight limit limit and an array arr of item weights, implement a function getIndicesOfItemWeights that finds two items whose sum of weights equals the weight limit limit. Your function should return a pair [i, j] of the indices of the item weights, ordered such that i > j. If such a pair doesn’t exist, return an empty array.
Analyze the time and space complexities of your solution.
Example:
Constraints:
limit
Time complexity: 遍歷整個 array,複雜度為
Space complexity: 利用 hash map 紀錄每個 index 及 value,複雜度為