Skip to content

Instantly share code, notes, and snippets.

View hassam-saeed's full-sized avatar
😇
Senior Software Engineer

Hassam Saeed hassam-saeed

😇
Senior Software Engineer
View GitHub Profile
function spiralOrder(matrix) {
if (!matrix.length || !matrix[0].length) return [];
const result = [];
const rows = matrix.length, cols = matrix[0].length;
let top = 0, bottom = rows - 1, left = 0, right = cols - 1;
while (top <= bottom && left <= right) {
// Traverse right
for (let i = left; i <= right; i++) {
@hassam-saeed
hassam-saeed / indices.js
Created November 16, 2023 19:44
A function to return indices of the elements that sums equal to the target number
function foo(nums, target) {
const indices = {};
for (let i = 0; i < nums.length; i++) {
const rem = target - nums[i];
if (indices[rem] !== undefined) return [indices[rem], i];
indices[nums[i]] = i;
}