|
| 1 | +var fib = function(n) { |
| 2 | + if (n <= 1){ |
| 3 | + return n; |
| 4 | + } |
| 5 | + return fib(n-1)+fib(n-2); |
| 6 | +}; |
| 7 | + |
| 8 | +var isPalindrome = function(s) { |
| 9 | + const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g,''); |
| 10 | + return cleaned === cleaned.split('').reverse().join(''); |
| 11 | +}; |
| 12 | + |
| 13 | +var kidsWithCandies = function(candies, extraCandies) { |
| 14 | + let max = Math.max(candies); |
| 15 | + let result = [] |
| 16 | + for (let candy in candies){ |
| 17 | + result.push(candy+extraCandies >= max) |
| 18 | + } |
| 19 | + return result; |
| 20 | +}; |
| 21 | + |
| 22 | +var reverseList = function(head) { |
| 23 | + let prev = null |
| 24 | + let curr = head |
| 25 | + while (curr != null){ |
| 26 | + let next = curr.next; |
| 27 | + curr.next = prev; |
| 28 | + next = prev |
| 29 | + curr = next; |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +var kidsWithCandies = function(candies, extraCandies) { |
| 34 | + let max = Math.max(...candies); |
| 35 | + let result = [] |
| 36 | + for (let candy of candies){ |
| 37 | + result.push(candy+extraCandies >= max) |
| 38 | + } |
| 39 | + return result; |
| 40 | +}; |
| 41 | + |
| 42 | +var reverseList = function(head) { |
| 43 | + let prev = null |
| 44 | + let curr = head |
| 45 | + while (curr !== null){ |
| 46 | + let next = curr.next; |
| 47 | + curr.next = prev; |
| 48 | + prev = curr; |
| 49 | + curr = next; |
| 50 | + } |
| 51 | + return prev; |
| 52 | +}; |
| 53 | + |
| 54 | +var canPlaceFlowers = function(flowerbed, n) { |
| 55 | + for (let i = 0; i < flowerbed.length; i++) { |
| 56 | + // Check if the current plot is empty and the plots to the left and right are also empty or out of bounds |
| 57 | + if (flowerbed[i] === 0 && |
| 58 | + (i === 0 || flowerbed[i - 1] === 0) && |
| 59 | + (i === flowerbed.length - 1 || flowerbed[i + 1] === 0)) { |
| 60 | + |
| 61 | + // Plant a flower |
| 62 | + flowerbed[i] = 1; |
| 63 | + n--; // Reduce the count of flowers we need to plant |
| 64 | + |
| 65 | + // If we have planted enough flowers, return true |
| 66 | + if (n === 0) return true; |
| 67 | + } |
| 68 | + } |
| 69 | + return n <= 0; // If there are still flowers left to plant, return false |
| 70 | +}; |
0 commit comments