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
/** | |
* @param {number} x | |
* @param {number} y | |
* @param {number} z | |
* @return {number} | |
*/ | |
var findClosest = function(x, y, z) { | |
// Calculate absolute distance from Person 1 to Person 3 | |
const distance1 = Math.abs(x - z); |
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
/** | |
* Counts the number of valid point pairs (i, j) such that: | |
* - x[i] ≤ x[j] | |
* - y[i] ≥ y[j] | |
* - No other point between i and j has the same or higher y[j] | |
* | |
* @param {number[][]} points - Array of [x, y] coordinates | |
* @return {number} - Number of valid pairs | |
*/ | |
var numberOfPairs = function(points) { |
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
/** | |
* @param {number[][]} points | |
* @return {number} | |
*/ | |
var numberOfPairs = function(points) { | |
let count = 0; | |
// Step 1: Sort points by x ascending, then y descending | |
// This ensures that for any point[i], all point[j] with j < i have x <= x[i] | |
points.sort((a, b) => { |
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
/** | |
* @param {character[][]} board | |
* @return {void} Do not return anything, modify board in-place instead. | |
*/ | |
var solveSudoku = function(board) { | |
const SIZE = 9; | |
// Track used digits in rows, columns, and 3x3 boxes | |
const row = Array.from({ length: SIZE }, () => Array(SIZE).fill(false)); | |
const col = Array.from({ length: SIZE }, () => Array(SIZE).fill(false)); |
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
/** | |
* Validates a partially filled 9x9 Sudoku board. | |
* Only filled cells are checked for rule violations. | |
* | |
* @param {character[][]} board - 2D array representing the Sudoku board | |
* @return {boolean} - true if the board is valid, false otherwise | |
*/ | |
var isValidSudoku = function(board) { | |
// Set to track seen digits in rows, columns, and sub-boxes | |
const seen = new Set(); |
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
/** | |
* @param {number} n | |
* @param {number} m | |
* @return {number} | |
*/ | |
var flowerGame = function(n, m) { | |
// Count how many odd numbers are in [1, n] | |
const oddX = Math.floor(n / 2) + (n % 2); // e.g., 1, 3, 5... | |
const evenX = Math.floor(n / 2); // e.g., 2, 4, 6... |
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
/** | |
* @param {number[][]} grid | |
* @return {number[][]} | |
*/ | |
var sortMatrix = function(grid) { | |
const n = grid.length; | |
// Helper to extract a matrix starting at (row, col) | |
function getMatrix(row, col) { | |
const matrix = []; |
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
/** | |
* @param {number[][]} grid | |
* @return {number} | |
*/ | |
var lenOfVDiagonal = function(grid) { | |
const n = grid.length; | |
const m = grid[0].length; | |
// Diagonal directions: ↘, ↙, ↖, ↗ | |
const DIRS = [ |
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
/** | |
* Returns the area of the rectangle with the longest diagonal. | |
* If multiple rectangles share the longest diagonal, returns the one with the largest area. | |
* | |
* @param {number[][]} dimensions - Array of [length, width] pairs for each rectangle. | |
* @return {number} - Area of the rectangle with the longest diagonal. | |
*/ | |
var areaOfMaxDiagonal = function(dimensions) { | |
let maxDiagonalSq = 0; // Stores the longest diagonal squared (avoids using Math.sqrt) | |
let maxArea = 0; // Stores the area of the rectangle with the longest diagonal |
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
/** | |
* @param {number[][]} mat | |
* @return {number[]} | |
*/ | |
var findDiagonalOrder = function(mat) { | |
// Edge case: empty matrix | |
if (!mat || mat.length === 0 || mat[0].length === 0) return []; | |
const m = mat.length; // number of rows | |
const n = mat[0].length; // number of columns |