leetcode-238-product-of-array-except-self
題目Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
題目連結
Example 1
12Input: nums = [1,2,3,4]Output: [24,12,8,6]
Example 2
12Input: nums = [-1,1,0,-3,3]Output: [0,0,9,0,0]
Constraints:
2 <= val <= 105
-30 < ...
leetcode-380-insert-delete-getrandom-o1
題目Implement the RandomizedSet class:
RandomizedSet() Initializes the RandomizedSet object.
bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
int getRandom() Returns a random element from the current set of elements (it’s guaranteed that at least one element exists when this method is called). Each ...
leetcode-274-h-index
題目Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher’s h-index.
According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
題目連結
Example 1
1234Input: citations = [3,0,6,1,5]Output: 3Explanation: [3,0,6,1,5] means the researcher has 5 papers in total an ...
leetcode-45-jump-game-ii
題目You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i] and
i + j < n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
題目連結
Example 1
123Input: nums = [2,3,1,1,4]Output: 2Expl ...
leetcode-55-jump-game
題目You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.
Return true if you can reach the last index, or false otherwise.
題目連結
Example 1
123Input: nums = [2,3,1,1,4]Output: trueExplanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2
123Input: nums = [3,2,1,0,4]Output: falseExplanation: You will always arrive at index 3 no matter what. Its max ...
從工程師的角度看待廣告 Part2 - 商品目錄篇
前言在 事件篇 提到發送事件是為了記錄使用者的行為,例如「瀏覽商品」、「加入購物車」等行為。但是,實際上我們看到的廣告,是需要提供「商品目錄」給廣告商,商品目錄就是商品的清單的意思,這樣廣告商才知道我們要投遞哪些商品。
商品目錄商品目錄就是一個商品清單,目錄中每個商品都有各自不同的資訊,如 id, title, link 等。這些欄位資訊在各廣告商的文件有明確的規範。以 Meta 為例,一個商品提供資訊如下,這些欄位是 Meta 規範的必要欄位。每個商品都要提供必要的欄位資訊,商品才能成功上傳到後台的目錄。
id
title
description
availability
condition
price
link
image_link
brand
1234
條紋地毯
黑色和白色的基本色調
in stock
new
24.99 USD
http://www…
http://www…
Jasper’s Market
item_group_id除了必填欄位,還有一些選填欄位可以提供給廣告商,其中比較重要的是 item_group_id,item_group_id 跟商品 i ...
leetcode-122-best-time-to-buy-and-sell-stock-ii
題目You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
題目連結
Example 1
12345Input: prices = [7,1,5,3,6,4]Output: 7Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.Then buy ...
leetcode-121-best-time-to-buy-and-sell-stock
題目You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0
題目連結
Example 1
1234Input: prices = [7,1,5,3,6,4]Output: 5Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.Note that buyin ...
leetcode-189-rotate-array
題目Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Follow-up: Could you do it in-place with O(1) extra space?
題目連結
Example 1
123456Input: nums = [1,2,3,4,5,6,7], k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,4,5,6]rotate 2 steps to the right: [6,7,1,2,3,4,5]rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2
12345Input: nums = [-1,-100,3,99], k = 2Output: [3,99,-1,-100]Explanation: rotate 1 steps to the right: [ ...
leetcode-169-majority-element
題目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.
Follow-up: Could you solve the problem in linear time and in O(1) space?
題目連結
Example 1
12Input: nums = [3,2,3]Output: 3
Example 2
12Input: nums = [2,2,1,1,1,2,2]Output: 2
解釋題目給你一個 array,叫 nums,array 的長度是 n。找出「多數元素」,也就是 出現次數 > n/2 的元素。題目假設 nums 中必定存在多數元素。另外的延伸題是,是否能用 O(1) space 的方式解 ...