From 967b11645ef89385642dac81247bd03b76edd3c7 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Sun, 6 Nov 2022 05:15:25 +0530 Subject: [PATCH 1/3] Solved valid-sudoku in JS. Solved Valid sudoku in JS --- javascript/36-valid-sudoku.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 javascript/36-valid-sudoku.js 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; +}; From cbebd684afbae49068a070f2fdda88bd834e2411 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Sun, 6 Nov 2022 05:23:23 +0530 Subject: [PATCH 2/3] Solved subarray sum equals k in JS. --- javascript/560-subarray-sum-equals-k.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 javascript/560-subarray-sum-equals-k.js 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; +}; From 60e51a843754e02351eec9813e4f685c68d9655d Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Mon, 7 Nov 2022 14:58:16 +0530 Subject: [PATCH 3/3] Create 209-minimum-size-subarray-sum.js Solved minimum-size-subarray-sum in JS. --- 209-minimum-size-subarray-sum.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 209-minimum-size-subarray-sum.js 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; + } +}; +
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: