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

Commita9f4d57

Browse files
committed
add 55, 134 (greedy problems)
1 parent8f9843c commita9f4d57

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

‎Medium/134-gasStation.js‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
*@see inline comments
3+
*@param {number[]} gas
4+
*@param {number[]} cost
5+
*@return {number}
6+
*/
7+
varcanCompleteCircuit=function(gas,cost){
8+
vartotal=0;
9+
varsumRemaining=0;
10+
varstart=0;
11+
12+
for(vari=0;i<gas.length;i++){
13+
varremaining=gas[i]-cost[i];
14+
// track total remaining gas until i
15+
if(sumRemaining>=0){
16+
sumRemaining+=remaining;
17+
}else{
18+
// if total remaing is smaller than 0, set start from i,
19+
start=i;
20+
// now the total remaining is the remaining at current i
21+
sumRemaining=remaining;
22+
}
23+
total+=remaining;
24+
}
25+
26+
// at the end, if total >= 0, it means there must be a circle to finish, and the
27+
// start is the start
28+
if(total>=0)returnstart;
29+
elsereturn-1;
30+
};

‎Medium/55-jumpGame.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Key: one scenario return false, is when you meet 0 and the max move can't reach to the end.
3+
* becaue no moves can be made when the element is 0.
4+
*
5+
*@param {number[]} nums
6+
*@return {boolean}
7+
*/
8+
varcanJump=function(nums){
9+
varmax=0;
10+
for(vari=0;i<nums.length;i++){
11+
if(i>max)returnfalse;
12+
max=Math.max(max,i+nums[i]);
13+
}
14+
15+
returntrue;
16+
};

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@
8787
*[46. Permutations](https://leetcode.com/problems/permutations/) -[Solution](./Medium/46-permutations.js)
8888
*[47. Permutations II](https://leetcode.com/problems/permutations-ii/) -[Solution](./Medium/47-permutationsII.js)
8989
*[48. Rotate Image](https://leetcode.com/problems/rotate-image/) -[Solution](./Medium/48-rotateImage.js)
90-
*[49. Group Anagrams](https://leetcode.com/problems/anagrams/) -[Solution](./Medium/49-groupAnagrams.js)
90+
*[49. Group Anagrams](https://leetcode.com/problems/anagrams/) -[Solution](./Medium/49-groupAnagrams.js)
9191
*[50. Pow(x, n)](https://leetcode.com/problems/powx-n/) -[Solution](./Medium/50-powerxn.js)
9292
*[53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) -[Solution](./Medium/53-maximumSubarray.js)
9393
*[54. Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) -[Solution](./Medium/54-spiralMatrix.js)
94+
*[55. Jump Game](https://leetcode.com/problems/jump-game/) -[Solution](./Medium/55-jumpGame.js)
9495
*[59. Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) -[Solution](./Medium/59-spiralMatrixII.js)
9596
*[62.Unique Paths](https://leetcode.com/problems/unique-paths/) -[Solution](./Medium/62-uniquePaths.js)
9697
*[64.Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) -[Solution](./Medium/64-minimumPathSum.js)
@@ -108,6 +109,7 @@
108109
*[121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) -[Solution](./Medium/121-bestTimeToBuySellStock.js)
109110
*[122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) -[Solution](./Medium/122-bestTimeToBuySellStockII.js)
110111
*[129. Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) -[Solution](./Medium/129-sumRootToLeafNumbers.js)
112+
*[134. Gas Station](https://leetcode.com/problems/gas-station/) -[Solution](./Medium/134-gasStation.js)
111113
*[136. Single Number](https://leetcode.com/problems/single-number/) -[Solution](./Medium/136-singleNumber.js)
112114
*[141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) -[Solution](./Medium/141-linkedListCycle.js)
113115
*[144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) -[Solution](./Medium/144-binaryTreePreorder.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp