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
| function minimumBribes(q) { | |
| let noOfBribes = 0; | |
| let tooChaotic = false; | |
| for (let i = 0, len = q.length; i < len; i++) { | |
| if (q[i] - (i + 1) > 2) { | |
| tooChaotic = true; | |
| } | |
| else if (q[i] < (i + 1)) { | |
| const diff = (i + 1) - q[i]; |
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
| function repeatedString(s, n) { | |
| if (s === "a") return n; | |
| const len = s.length; | |
| let numOfA = Array.from(s).filter(el => el === "a").length; | |
| let totalLen, restLen, answer = 0; | |
| totalLen = Math.floor(n / len) * len; | |
| restLen = n - totalLen; |
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
| function jumpingOnClouds(c) { | |
| let pos = 0; | |
| let cloud; | |
| let len = c.length - 1; | |
| let answer = 0; | |
| while (pos < len) { | |
| cloud = c.indexOf(1, pos + 1); | |
| if (cloud - pos === 1) pos += 2; |
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
| function countingValleys(n, s) { | |
| let seaLevel = 0; | |
| let answer = 0; | |
| let valleyStart = false; | |
| for (let i = 0, len = s.length; i < len; i++) { | |
| if (s[i] === "U") seaLevel += 1; | |
| else if (s[i] === "D") seaLevel -= 1; | |
| if (seaLevel < 0) valleyStart = true; |
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
| function sockMerchant(n, ar) { | |
| const set = new Set(ar); | |
| let answer = 0; | |
| set.forEach(color => { | |
| answer += Math.floor(ar.filter(el => el === color).length / 2); | |
| }) | |
| return answer; | |
| } |
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
| function solution(number, k) { | |
| const len = number.length - k; | |
| let max = number; | |
| for(let i = 0; i < k; i++) { | |
| for(let j = 0; j <= len; j++) { | |
| if(max[j] < max[j+1]) { | |
| max = max.replace(max[j], ''); | |
| break; | |
| } |
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
| function matchingStrings(strings, queries) { | |
| const result = []; | |
| queries.forEach(querie => { | |
| let count = 0; | |
| strings.forEach(string => { | |
| if (string.match(querie) && string.match(querie)[0].length === string.length) count += 1; | |
| }); | |
| result.push(count); |
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
| function solution(genres, plays) { | |
| const genreDB = makeGenreDB(genres); | |
| let answer; | |
| answer = chooseBestAlbum(genreDB, plays); | |
| return answer; | |
| } | |
| function makeGenreDB(list){ | |
| const genreObj = {}; |
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
| function countInversions(arr) { | |
| let count = 0; | |
| mergeSort(arr, count); | |
| function mergeSort (arr){ | |
| if(arr.length < 2) return arr; | |
| const pivot = Math.floor(arr.length / 2); | |
| const left = arr.slice(0, pivot); | |
| const right = arr.slice(pivot); | |
| return merge(mergeSort(left), mergeSort(right)); |
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
| function activityNotifications(expenditure, d) { | |
| let notification = 0; | |
| for (let i = d, j = 0, len = expenditure.length; i < len; i++ , j++) { | |
| const transactionData = expenditure.slice(j, i).sort((a, b) => a - b); | |
| const median = getMedian(transactionData); | |
| if (median * 2 <= expenditure[i]) notification += 1; | |
| } | |
| return notification; |