Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2771 from NBunnDunn/main
Browse files Browse the repository at this point in the history
Create 2017-grid-game.js & Create 0028-find-index-of-first-occurence-in-string.js
  • Loading branch information
felivalencia3 authored Aug 7, 2023
2 parents 6481c34 + e3e94a6 commit ab33b27
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
23 changes: 23 additions & 0 deletions javascript/0028-find-index-of-first-occurrence-in-string.js
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;
}
23 changes: 23 additions & 0 deletions javascript/2017-grid-game.js
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;
};

0 comments on commit ab33b27

Please sign in to comment.