You are viewing a single comment's thread. Return to all comments →
my js code (javascript) O(n)
let results = []; let temp = []; let index = 1; for (let j = 0; j < n; ) { if (arr[j] >= k) { arr[j] -= k; temp = [...Array(k + 1).keys()]; if (index >= k) { temp = temp.map((value) => value + (index - 1)); } index = index + k; temp.shift(); if (temp.length > 0) { results.push(temp); } } else { temp = [...Array(arr[j] + 1).keys()]; if (index <= 1) { } else { temp = temp.map((value) => value + (index - 1)); } temp.shift(); if (temp.length > 0) { results.push(temp); } index = 1; j++; } } let count = 0; for (let r = 0; r < results.length; r++) { if (results[r].includes(r + 1)) { count++; } } return count;
Seems like cookies are disabled on this browser, please enable them to open this website
Lisa's Workbook
You are viewing a single comment's thread. Return to all comments →
my js code (javascript) O(n)