Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit582c02f

Browse files
committed
add 122, 153, ready to pull readme.md
1 parent9ae14c3 commit582c02f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Key: because no limitation, as long as the price is heigher than before, the profit
3+
* can be added to maxProfit.
4+
*
5+
*@param {number[]} prices
6+
*@return {number}
7+
*/
8+
varmaxProfit=function(prices){
9+
varmaxProfit=0;
10+
for(vari=1;i<prices.length;i++){
11+
varprofit=prices[i]-prices[i-1];
12+
if(profit>0)maxProfit+=profit;
13+
}
14+
15+
returnmaxProfit;
16+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Binary search. Be careful about the boundaries.
3+
* If the first element is larger than the last one, then we compute the element in the middle,
4+
* and compare it with the first element. If value of the element in the middle is larger than
5+
* the first element, we know the rotation is at the second half of this array.
6+
* Else, it is in the first half in the array.
7+
*
8+
*@param {number[]} nums
9+
*@return {number}
10+
*/
11+
varfindMin=function(nums){
12+
varlow=0;
13+
varhigh=nums.length-1;
14+
15+
while(low<=high){
16+
if(nums[low]<=nums[high])returnnums[low];
17+
varmid=low+Math.floor((high-low)/2);
18+
if(nums[mid]>=nums[low])low=mid+1;
19+
elsehigh=mid;
20+
}
21+
22+
returnnums[0];
23+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp