diff --git a/209-minimum-size-subarray-sum.js b/209-minimum-size-subarray-sum.js new file mode 100644 index 000000000..0d4b41934 --- /dev/null +++ b/209-minimum-size-subarray-sum.js @@ -0,0 +1,25 @@ +// problem link https://leetcode.com/problems/minimum-size-subarray-sum/submissions/612844825/ +// time complexity is O(n). + +var minSubArrayLen = function(target, nums) { + + let total = 0; + let min_length = Number.MAX_SAFE_INTEGER; + let left_pointer = 0; + for(let i = 0; i < nums.length; i++) { + + total += nums[i]; + while(total >= target) { + min_length = Math.min(min_length, i + 1 - left_pointer); + total -= nums[left_pointer]; + left_pointer = left_pointer+1; + } + } + + if(min_length == Number.MAX_SAFE_INTEGER) { + return 0; + } else { + return min_length; + } +}; + diff --git a/javascript/36-valid-sudoku.js b/javascript/36-valid-sudoku.js new file mode 100644 index 000000000..57c0be474 --- /dev/null +++ b/javascript/36-valid-sudoku.js @@ -0,0 +1,30 @@ + +// the time complexity is constant as we only going to have a 9*9 matrix. +// problem link: https://leetcode.com/problems/valid-sudoku + +var isValidSudoku = function(board) { + + + let rows = []; + let columns = []; + let boxes = []; + for(let i = 0; i < board.length; i++) { + rows.push(new Set()); + columns.push(new Set()); + boxes.push(new Set()); + } + + for(let i = 0; i < board.length; i++) { + for(let j = 0; j < board[0].length; j++) { + const boxIndex = 3 * Math.floor(i/3) + Math.floor(j/3); + let curruntCell = board[i][j]; + if(rows[i].has(curruntCell) || columns[j].has(curruntCell) || boxes[boxIndex].has(curruntCell)) return false; + if(curruntCell == '.') continue; + rows[i].add(curruntCell); + columns[j].add(curruntCell); + boxes[boxIndex].add(curruntCell); + } + } + + return true; +}; diff --git a/javascript/560-subarray-sum-equals-k.js b/javascript/560-subarray-sum-equals-k.js new file mode 100644 index 000000000..ff9d4e71d --- /dev/null +++ b/javascript/560-subarray-sum-equals-k.js @@ -0,0 +1,23 @@ +// Problem link https://leetcode.com/problems/subarray-sum-equals-k +// Time Complexity: O(n) + + +var subarraySum = function(nums, k) { + + + +const prefixMap = {}; +let totalSubArray = 0; +let ongoingSum = 0; + +prefixMap[0] = 1; +for(let i = 0; i < nums.length; i++) { + ongoingSum += nums[i]; + if(prefixMap[ongoingSum - k]){ + totalSubArray += prefixMap[ongoingSum - k]; + } + prefixMap[ongoingSum] = (prefixMap[ongoingSum] ? prefixMap[ongoingSum] + 1: 1); +} + +return totalSubArray; +};