diff --git a/javascript/LeetCode/Array/1390.js b/javascript/LeetCode/Array/1390.js new file mode 100644 index 0000000..ee30293 --- /dev/null +++ b/javascript/LeetCode/Array/1390.js @@ -0,0 +1,67 @@ +/** + * 1390. Four Divisors + * + * 參數為數值陣列,找出元素能夠被整除4次的為何?並將能整除該元素的數字加總回傳 + * @param {number[]} nums + * @return {number} + */ +var sumFourDivisors = function(nums) { + // 能整除元素的除了最小的1之外,還有它自己,所以固定整除的有2個 + + // solution 1.此方法可用,但若用在大資料,會tle + // let ans = 0; + // for(let i = 0;i < nums.length;++i) { + // let arr = divisors(nums[i]); + // if(arr.length === 4){ + // ans += arr.reduce((a,b) => a + b,0); + // } + // } + // return ans; + // /** + // * 每個元素能被整除的數字有哪些 + // * @param {number} e + // * @returns {number[]} + // */ + // function divisors(e){ + // let divisor = []; + // for(let i = 1;i <= e;++i) { + // if(e % i === 0){ + // divisor.push(i); + // } + // } + // return divisor; + // } + + // solution 2. + let ans = 0; + for(const a of nums){ + let divisorsCount = 0; + let sum = 0; + for(let i = 1;i * i <= a;++i) { + if(a % i === 0){ + divisorsCount++; + sum += i; + if (i * i !== a) { + divisorsCount++; + sum += a / i; + } + } + } + if(divisorsCount === 4){ + ans += sum; + } + } + return ans; +}; +let nums = [21,4,7]; +/*** + * ans: 32 + * + * 21 has 4 divisors: 1, 3, 7, 21 + * 4 has 3 divisors: 1, 2, 4 + * 7 has 2 divisors: 1, 7 + * The answer is the sum of divisors of 21 only. + */ +// let nums = [21,21]; +// 64 (32 + 32) +console.log(sumFourDivisors(nums)); \ No newline at end of file diff --git a/javascript/LeetCode/Array/3074.js b/javascript/LeetCode/Array/3074.js new file mode 100644 index 0000000..e100a35 --- /dev/null +++ b/javascript/LeetCode/Array/3074.js @@ -0,0 +1,23 @@ +/** + * 3074. Apple Redistribution into Boxes + * + * 最少需要幾個箱子才能將重新分配的apple裝進去 + * + * @param {number[]} apple + * @param {number[]} capacity + * @return {number} + */ +var minimumBoxes = function(apple, capacity) { + // sort box desc + capacity.sort((a,b) => b - a); + let sum = apple.reduce((a,b) => a + b,0); + let ans = 0; + while(sum > 0){ + sum -= capacity[ans++]; + } + return ans; + +}; +let apple = [5,5,5], capacity = [2,4,2,7]; +// 4 +console.log(minimumBoxes(apple,capacity)) \ No newline at end of file diff --git a/javascript/LeetCode/Array/66.js b/javascript/LeetCode/Array/66.js new file mode 100644 index 0000000..c317008 --- /dev/null +++ b/javascript/LeetCode/Array/66.js @@ -0,0 +1,32 @@ +/** + * 66. Plus One + * + * 參數為數值陣列,將該參數+1並以數字陣列回傳 + * 只需要知道最後一個數字是什麼並將它+1 + * @param {number[]} digits + * @return {number[]} + */ +var plusOne = function(digits) { + // 只需要知道最後一個數字是什麼並將它+1 + // 若 +1 位數 >= 2,則拆開 + + for(let i = digits.length - 1;i >= 0;--i) { + if(digits[i] + 1 < 10){ + digits[i]++; + return digits; + } + digits[i] = 0; + } + digits.unshift(1); + return digits; +}; +let digits = [1,2,3]; +//[1,2,4] +// Explanation: The array represents the integer 123. +// Incrementing by one gives 123 + 1 = 124. +// Thus, the result should be [1,2,4]. +// let digits = [9]; +// [1,0] +// let digits = [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]; +// [6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,4] +console.log(plusOne(digits)); \ No newline at end of file diff --git a/javascript/LeetCode/String/3794.js b/javascript/LeetCode/String/3794.js new file mode 100644 index 0000000..522b84f --- /dev/null +++ b/javascript/LeetCode/String/3794.js @@ -0,0 +1,16 @@ +/** + * 3794. Reverse String Prefix + * + * 反轉s中前k個字母並回傳 + * @param {string} s + * @param {number} k + * @return {string} + */ +var reversePrefix = function(s, k) { + return s.substring(0,k).split("").reverse().join("") + s.substring(k); +}; +// let s = "abcd", k = 2; +// "bacd" +let s = "hey", k = 1; +// "hey" +console.log(reversePrefix(s,k)); \ No newline at end of file diff --git a/javascript/codewar/array/17.js b/javascript/codewar/array/17.js new file mode 100644 index 0000000..94ab83e --- /dev/null +++ b/javascript/codewar/array/17.js @@ -0,0 +1,64 @@ +/** + * 3kyu - How many are smaller than me II? + * + * 回傳arr[i]的右邊有幾個是小於自己的 + * + * @param {number[]} arr + * @returns {number[]} + */ +function smaller(arr) { + // 這方法ok,但不適用於large test cases + // let ans = []; + // for(let i = 0;i < arr.length;++i) { + // let count = 0; + // for(let j = 0;j < arr.length;j++) { + // // if(arr[i] === arr[j]){ + // // continue; + // // } + // if(arr[i] > arr[j]){ + // count++; + // } + // } + // ans[i] = count; + // } + // return ans; + + return arr.map((current, i) => { + let count = 0; + // 比較當前元素右邊所有的元素 + for (let j = 0; j < arr.length; j++) { + if (arr[j] < current) { + count++; + } + } + return count; + }); + + // binary search + // const result = new Array(arr.length).fill(0); + // const sortedArray = []; + + // // 從右往左處理每個元素 + // for (let i = arr.length - 1; i >= 0; i--) { + // const current = arr[i]; + + // // binary search + // let left = 0; + // let right = sortedArray.length; + + // while (left < right) { + // const mid = Math.floor((left + right) / 2); + // if (sortedArray[mid] < current) { + // left = mid + 1; + // } else { + // right = mid; + // } + // } + // result[i] = left; + + // sortedArray.splice(left, 0, current); + // } + // return result; +} +console.log(assert.deepEqual(smaller([5, 4, 7, 9, 2, 4, 1, 4, 5, 6]), [5, 2, 6, 6, 1, 1, 0, 0, 0, 0])); +console.log(assert.deepEqual(smaller([5, 4, 3, 2, 1]), [4, 3, 2, 1, 0])) \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index b9f992c..1218a05 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -2,6 +2,7 @@ import { format } from 'node:path'; import {ExecutionTimer} from './time.js'; import assert from 'node:assert/strict'; +import { count } from 'node:console'; /* 22. Generate Parentheses @@ -1159,3 +1160,43 @@ var specialTriplets = function(nums) { */ // console.log(specialTriplets(nums)); + + +/** + * 345. Reverse Vowels of a String + * + * 找出所有母音(不分大小寫),其餘子音維持原位,唯獨反轉母音 + * @param {string} s + * @return {string} + */ +var reverseVowels = function(s) { + let vowels = ["a","e","i","o","u","A","E","I","O","U"]; + let splitS = s.split(""); + // 2 pointer? + let j = splitS.length - 1,i = 0; + while(i < j){ + if(!vowels.includes(splitS[i],i)){ + i++; + continue; + } + if(!vowels.includes(splitS[j],j)){ + j--; + continue; + } + let char = splitS[i]; + splitS[i] = splitS[j]; + splitS[j] = char; + i++; + j--; + } + return splitS.join(""); +}; +let s = "IceCreAm"; +/** + * Output: "AceCreIm" + * Explanation: + * The vowels in s are ['I', 'e', 'e', 'A']. On reversing the vowels, s becomes "AceCreIm". + * + */ +// console.log(reverseVowels(s)); +