技術文章

Search in Rotated Sorted Array

[LeetCode] 0033. Search in Rotated Sorted Array

There is an integer array nums sorted in ascending order (with distinct values).

Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. You must write an algorithm with O(log n) runtime complexity.

[LeetCode] 0033. Search in Rotated Sorted Array Read More »

Leetcode, 技術文章
find minimum in rotated sorted array

[LeetCode] 0153. find minimum in rotated sorted array

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

[4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.

Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n-2]].

Given the sorted rotated array nums of unique elements, return the minimum element of this array.

You must write an algorithm that runs in O(log n) time.

[LeetCode] 0153. find minimum in rotated sorted array Read More »

Leetcode, 技術文章
Product of Array Except Self

[LeetCode] 0238. 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.

[LeetCode] 0238. Product of Array Except Self Read More »

Leetcode, 技術文章

[LeetCode] 0123. Best Time to Buy and Sell Stock III

[LeetCode] 0123. Best Time to Buy and Sell Stock III.
Hard
You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete at most two transactions.

Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

[LeetCode] 0123. Best Time to Buy and Sell Stock III Read More »

Leetcode, 技術文章

[LeetCode] 0122. Best Time to Buy and Sell Stock II

[LeetCode] 0122. 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.

[LeetCode] 0122. Best Time to Buy and Sell Stock II Read More »

Leetcode, 技術文章

[LeetCode] 0121. Best Time to Buy and Sell Stock

[LeetCode] 0121. Best Time to Buy and Sell Stock
Easy
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.

[LeetCode] 0121. Best Time to Buy and Sell Stock Read More »

Leetcode, 技術文章
zh_TW繁體中文