- Notifications
You must be signed in to change notification settings - Fork1.3k
js solution 994#189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
fishercoder1534 merged 6 commits intofishercoder1534:masterfromsambabib:sambabib-js-solutionsJan 5, 2025
Uh oh!
There was an error while loading.Please reload this page.
Merged
js solution 994#189
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletion.gitignore
This file contains hidden or 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
43 changes: 43 additions & 0 deletionsjavascript/_17.js
This file contains hidden or 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,43 @@ | ||
function letterCombinations(digits) { | ||
// If the input is an empty string, return an empty array. | ||
if (digits.length === 0) { | ||
return []; | ||
} | ||
// Mapping of digits to letters as per the telephone keypad using a javascript dictionary. | ||
const digitToChar = { | ||
'2': ['a', 'b', 'c'], | ||
'3': ['d', 'e', 'f'], | ||
'4': ['g', 'h', 'i'], | ||
'5': ['j', 'k', 'l'], | ||
'6': ['m', 'n', 'o'], | ||
'7': ['p', 'q', 'r', 's'], | ||
'8': ['t', 'u', 'v'], | ||
'9': ['w', 'x', 'y', 'z'] | ||
}; | ||
// Resultant array to store all possible combinations | ||
const result = []; | ||
// Backtracking function to generate combinations | ||
function backtrack(index, currentCombination) { | ||
// if the current combination has the same length as the input digits. | ||
if (index === digits.length) { | ||
result.push(currentCombination); | ||
return; | ||
} | ||
// Get the letters that the current digit maps to. | ||
let letters = digitToChar[digits[index]]; | ||
// Loop through the letters and call backtrack recursively for the next digit. | ||
for (let letter of letters) { | ||
backtrack(index + 1, currentCombination + letter); | ||
} | ||
} | ||
// Start backtracking from the first digit (index 0) with an empty string as the initial combination. | ||
backtrack(0, ''); | ||
return result; | ||
}; |
23 changes: 23 additions & 0 deletionsjavascript/_3.js
This file contains hidden or 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 @@ | ||
function lengthOfLongestSubstring(s) { | ||
// Using the "sliding window" data structure. | ||
// Create a javascript set to store unique characters. | ||
let charSet = new Set(); | ||
let left = 0; // Left pointer of the sliding window. | ||
let maxLength = 0; | ||
// This moves the right pointer of the sliding window. | ||
for (let right = 0; right < s.length; right++) { | ||
// If the character at the right pointer is already in the set, move the left pointer. | ||
while (charSet.has(s[right])) { | ||
charSet.delete(s[left]); | ||
left++; | ||
} | ||
// Add the current character at the right pointer to the set. | ||
charSet.add(s[right]); | ||
// Update the maximum length of substring without repeating characters. | ||
maxLength = Math.max(maxLength, right - left + 1); | ||
} | ||
return maxLength; | ||
} |
57 changes: 57 additions & 0 deletionsjavascript/_994.js
This file contains hidden or 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,57 @@ | ||
function orangesRotting(grid) { | ||
const rows = grid.length; | ||
const cols = grid[0].length; | ||
let queue = []; | ||
let freshOranges = 0; | ||
// Initialize the queue with all rotten oranges and count fresh oranges | ||
for (let r = 0; r < rows; r++) { | ||
for (let c = 0; c < cols; c++) { | ||
if (grid[r][c] === 2) { | ||
queue.push([r, c]); | ||
} else if (grid[r][c] === 1) { | ||
freshOranges++; | ||
} | ||
} | ||
} | ||
// If there are no fresh oranges, return 0 | ||
if (freshOranges === 0) return 0; | ||
let minutes = 0; | ||
const directions = [ | ||
[0, 1], // right | ||
[1, 0], // down | ||
[0, -1], // left | ||
[-1, 0] // up | ||
]; | ||
// Step 2: Perform BFS | ||
while (queue.length > 0) { | ||
let size = queue.length; | ||
let newRotten = false; | ||
for (let i = 0; i < size; i++) { | ||
let [x, y] = queue.shift(); | ||
for (let [dx, dy] of directions) { | ||
let nx = x + dx; | ||
let ny = y + dy; | ||
// Check if the neighboring cell is a fresh orange | ||
if (nx >= 0 && ny >= 0 && nx < rows && ny < cols && grid[nx][ny] === 1) { | ||
grid[nx][ny] = 2; // Make it rotten | ||
freshOranges--; // Decrease count of fresh oranges | ||
queue.push([nx, ny]); // Add it to the queue | ||
newRotten = true; | ||
} | ||
} | ||
} | ||
// If rotten oranges exist, increment minutes | ||
if (newRotten) minutes++; | ||
} | ||
// Check if there are any fresh oranges left | ||
return freshOranges === 0 ? minutes : -1; | ||
} |
6 changes: 3 additions & 3 deletionspaginated_contents/algorithms/1st_thousand/README.md
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.