forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2771 from NBunnDunn/main
Create 2017-grid-game.js & Create 0028-find-index-of-first-occurence-in-string.js
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
javascript/0028-find-index-of-first-occurrence-in-string.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Submission Details: | ||
* https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/ | ||
* Time O(n * m), Space O(1) | ||
* Runtime: 48ms (beats 91.92%) || 41.6mb (beats 78.25%) | ||
*/ | ||
|
||
/** | ||
* @param {string} haystack | ||
* @param {string} needle | ||
* @return {number} | ||
*/ | ||
var strStr = function(haystack, needle) { | ||
if (needle.length == 0) return 0; | ||
for (let i = 0; i < haystack.length; i++) { | ||
let k = i, j = 0; | ||
while (haystack[k] == needle[j] && j < needle.length) { | ||
k++, j++; | ||
} | ||
if (j == needle.length) return i; | ||
} | ||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Submission Details: | ||
* https://leetcode.com/problems/grid-game/ | ||
* Time O(n), Space O(1) | ||
* Runtime: 89ms (beats 79.31%) || 53.5mb (beats 89.66%) | ||
*/ | ||
|
||
/** | ||
* @param {number[][]} grid | ||
* @return {number} | ||
*/ | ||
|
||
var gridGame = function(grid) { | ||
let one = grid[0].reduce((a,b)=>a+b) - grid[0][0]; | ||
let two = 0; | ||
let res = one; | ||
for(let i = 1; i < grid[0].length; i++){ | ||
one-=grid[0][i]; | ||
two+=grid[1][i-1]; | ||
res = Math.min(res, Math.max(one,two)); | ||
} | ||
return res; | ||
}; |