diff --git a/javascript/LeetCode/Array/118.js b/javascript/LeetCode/Array/118.js new file mode 100644 index 0000000..58d114a --- /dev/null +++ b/javascript/LeetCode/Array/118.js @@ -0,0 +1,25 @@ +/** + * 118. Pascal's Triangle + * + * Pascal's Triangle = 每個數字都是其上方兩個數字的總和 + * + * @param {number} numRows + * @return {number[][]} + */ +var generate = function(numRows) { + let result = [[1]]; + // console.log(result) + for (let i = 0; i < numRows - 1; i++) { + const rows = [0, ...result[result.length - 1], 0]; + const row = []; + + for (let j = 0; j < rows.length - 1; j++) { + row.push(rows[j] + rows[j + 1]); + } + result.push(row); + } + return result; +}; +let numRows = 5 +// Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]] +console.log(generate(numRows)); \ No newline at end of file diff --git a/javascript/LeetCode/Array/1498.js b/javascript/LeetCode/Array/1498.js new file mode 100644 index 0000000..b33500a --- /dev/null +++ b/javascript/LeetCode/Array/1498.js @@ -0,0 +1,76 @@ +/** + * 1498. Number of Subsequences That Satisfy the Given Sum Condition + * + * You are given an array of integers nums and an integer target. + * Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. + * Since the answer may be too large, return it modulo 109 + 7. + * + * + * Example 1: + * Input: nums = [3,5,6,7], target = 9 + * Output: 4 + * Explanation: There are 4 subsequences that satisfy the condition. + * [3] -> Min value + max value <= target (3 + 3 <= 9) + * [3,5] -> (3 + 5 <= 9) + * [3,5,6] -> (3 + 6 <= 9) + * [3,6] -> (3 + 6 <= 9) + * + * Example 2: + * Input: nums = [3,3,6,8], target = 10 + * Output: 6 + * Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). + * [3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] + * + * Example 3: + * Input: nums = [2,3,3,4,6,7], target = 12 + * Output: 61 + * Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]). + * Number of valid subsequences (63 - 2 = 61). + * + * Constraints: + * 1 <= nums.length <= 105 + * 1 <= nums[i] <= 106 + * 1 <= target <= 106 + * + * Hints: + * 1.Sort the array nums. + * 2.Use two pointers approach: Given an index i (choose it as the minimum in a subsequence) find the maximum j where j ≥ i and nums[i] +nums[j] ≤ target. + * 3.Count the number of subsequences. + * + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var numSubseq = function(nums, target) { + const mod = 1e9 + 7; + nums.sort((a, b) => a - b); + + const power = new Array(nums.length).fill(1); + for (let i = 1; i < nums.length; i++) { + power[i] = (power[i - 1] * 2) % mod; + } + + let left = 0; // min + let right = nums.length - 1; // max + let result = 0; + + while (left <= right) { + if (nums[left] + nums[right] <= target) { + result = (result + power[right - left]) % mod; + left++; + } else { + right--; + } + } + + return result; +}; +let nums = [3,5,6,7], target = 9; +/** + * There are 4 subsequences that satisfy the condition. +[3] -> Min value + max value <= target (3 + 3 <= 9) +[3,5] -> (3 + 5 <= 9) +[3,5,6] -> (3 + 6 <= 9) +[3,6] -> (3 + 6 <= 9) + */ +console.log(numSubseq(nums,target)); \ No newline at end of file diff --git a/javascript/LeetCode/Array/2037.js b/javascript/LeetCode/Array/2037.js new file mode 100644 index 0000000..ed28eec --- /dev/null +++ b/javascript/LeetCode/Array/2037.js @@ -0,0 +1,68 @@ +/** + * 2037. Minimum Number of Moves to Seat Everyone + * + * There are n availabe seats and n students standing in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. + * You are also given the array students of length n, where students[j] is the position of the jth student. + * + * You may perform the following move any number of times: + * Increase or decrease the position of the ith student by 1 (i.e., moving the ith student from position x to x + 1 or x - 1) + * Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat. + * + * Note that there may be multiple seats or students in the same position at the beginning. + * + * + * Example 1: + * Input: seats = [3,1,5], students = [2,7,4] + * Output: 4 + * Explanation: The students are moved as follows: + * - The first student is moved from position 2 to position 1 using 1 move. + * - The second student is moved from position 7 to position 5 using 2 moves. + * - The third student is moved from position 4 to position 3 using 1 move. + * In total, 1 + 2 + 1 = 4 moves were used. + * + * Example 2: + * Input: seats = [4,1,5,9], students = [1,3,2,6] + * Output: 7 + * Explanation: The students are moved as follows: + * - The first student is not moved. + * - The second student is moved from position 3 to position 4 using 1 move. + * - The third student is moved from position 2 to position 5 using 3 moves. + * - The fourth student is moved from position 6 to position 9 using 3 moves. + * In total, 0 + 1 + 3 + 3 = 7 moves were used. + * + * Example 3: + * Input: seats = [2,2,6,6], students = [1,3,2,6] + * Output: 4 + * Explanation: Note that there are two seats at position 2 and two seats at position 6. + * The students are moved as follows: + * - The first student is moved from position 1 to position 2 using 1 move. + * - The second student is moved from position 3 to position 6 using 3 moves. + * - The third student is not moved. + * - The fourth student is not moved. + * In total, 1 + 3 + 0 + 0 = 4 moves were used. + * + * + * Constraints: + * n == seats.length == students.length + * 1 <= n <= 100 + * 1 <= seats[i], students[j] <= 100 + * + * @param {number[]} seats + * @param {number[]} students + * @return {number} + */ +var minMovesToSeat = function(seats, students) { + // sort two params. + seats.sort((a,b) => a - b); + students.sort((a,b) => a - b); + console.log(students); + let count = 0; + for(let i = 0;i < seats.length;i++) { + count += Math.abs(seats[i] - students[i]); + } + return count; +}; +let seats = [2,2,6,6], students = [1,3,2,6]; +// 4 +// console.log(assert.strictEqual(minMovesToSeat(seats,students),4)); +console.log(minMovesToSeat(seats,students)); \ No newline at end of file diff --git a/javascript/LeetCode/Array/2099.js b/javascript/LeetCode/Array/2099.js new file mode 100644 index 0000000..39a2846 --- /dev/null +++ b/javascript/LeetCode/Array/2099.js @@ -0,0 +1,67 @@ +/** + * 2099. Find Subsequence of Length K With the Largest Sum + * + * You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum. + * Return any such subsequence as an integer array of length k. + * A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. + * + * Hints: + * 1.From a greedy perspective, what k elements should you pick? + * 2.Could you sort the array while maintaining the index? + * + * Example 1: + * Input: nums = [2,1,3,3], k = 2 + * Output: [3,3] + * Explanation: + * The subsequence has the largest sum of 3 + 3 = 6. + * + * Example 2: + * Input: nums = [-1,-2,3,4], k = 3 + * Output: [-1,3,4] + * Explanation: + * The subsequence has the largest sum of -1 + 3 + 4 = 6. + * + * Example 3: + * Input: nums = [3,4,3,3], k = 2 + * Output: [3,4] + * Explanation: + * The subsequence has the largest sum of 3 + 4 = 7. + * Another possible subsequence is [4, 3]. + * + * + * Constraints: + * 1 <= nums.length <= 1000 + * -105 <= nums[i] <= 105 + * 1 <= k <= nums.length + * + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var maxSubsequence = function(nums, k) { + // 參數為一個數值陣列nums和數值k,回傳長度為k的陣列元素,該陣列元素加總是最大值且必須符合原有陣列元素順序 + // 須依照原有的index 排序 + if(nums.length === k){ + return nums; + } + let arr = []; + let map = new Map(); + let count = 0; + for(let i = 0;i < nums.length;i++) { + map.set(i,nums[i]); + } + // {oringinal index => element} + // 保留原有的index,sort by value desc. + const sortByValue = new Map([...map.entries()].sort((a, b) => b[1] - a[1])); + + for(const [key,value] of sortByValue) { + if(count < k){ + arr[key] = value; + } + count++; + } + return arr.filter((a) => a !== undefined); +}; +let nums = [-1,-2,3,4], k = 3; +// [-1,3,4] +console.log(maxSubsequence(nums,k)); \ No newline at end of file diff --git a/javascript/LeetCode/Array/2325.js b/javascript/LeetCode/Array/2325.js new file mode 100644 index 0000000..cdd4552 --- /dev/null +++ b/javascript/LeetCode/Array/2325.js @@ -0,0 +1,35 @@ +/** + * 2325. Decode the Message + * + * 給兩個字串參數,key & message,使用message的字母去一一比對key要表達的意思。 + * key長度是26個字母,從第一個字母到最後一個字母可以把它轉換成a-z,就能解出是什麼 + * + * @param {string} key + * @param {string} message + * @return {string} + */ +var decodeMessage = function(key, message) { + // 26個英文字母 + // map + let removeWhiteSpace = key.replaceAll(" ",""); + let alp = new Map(); + let result = ""; + let currentChar = 'a'.charCodeAt(0); + for(let a = 0;a < removeWhiteSpace.length;a++) { + // if (alp.has(removeWhiteSpace[a])){ + // continue; + // } + alp.set(removeWhiteSpace[a],String.fromCharCode(currentChar++)); + } + for(const v of message) { + if(alp.get(v)){ + result += alp.get(v); + }else{ + result += " "; + } + } + return result; +}; +let key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"; +// "this is a secret" +console.log(decodeMessage(key,message)); \ No newline at end of file diff --git a/javascript/LeetCode/Array/2465.js b/javascript/LeetCode/Array/2465.js new file mode 100644 index 0000000..fc0a29a --- /dev/null +++ b/javascript/LeetCode/Array/2465.js @@ -0,0 +1,59 @@ +/** + * 2465. Number of Distinct Averages + * + * You are given a 0-indexed integer array nums of even length. + * As long as nums is not empty, you must repetitively: + * Find the minimum number in nums and remove it. + * Find the maximum number in nums and remove it. + * Calculate the average of the two removed numbers. + * The average of two numbers a and b is (a + b) / 2. + * For example, the average of 2 and 3 is (2 + 3) / 2 = 2.5. + * Return the number of distinct averages calculated using the above process. + * + * Note that when there is a tie for a minimum or maximum number, any can be removed. + * + * Hints: + * 1.Try sorting the array. + * 2.Store the averages being calculated, and find the distinct ones. + * + * Example 1: + * Input: nums = [4,1,4,0,3,5] + * Output: 2 + * Explanation: + * 1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3]. + * 2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3]. + * 3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5. + * Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2. + * + * Example 2: + * Input: nums = [1,100] + * Output: 1 + * Explanation: + * There is only one average to be calculated after removing 1 and 100, so we return 1. + * + * Constraints: + * 2 <= nums.length <= 100 + * nums.length is even. + * 0 <= nums[i] <= 100 + * + * @param {number[]} nums + * @return {number} + */ +var distinctAverages = function(nums) { + // 從陣列中移除最小和最大的元素,並將他們兩個加總/2 + // 紀錄每次的加總後/2的結果,計算有幾個唯一值 + nums.sort((a,b) => a - b); + let setUniuqe = new Set(); + let left = 0; + let right = nums.length - 1; + while(left <= right){ + let avg = (nums[left] + nums[right]) / 2.0; + setUniuqe.add(avg); + left++; + right--; + } + return setUniuqe.size; +}; +let nums = [4,1,4,0,3,5]; +// 2 +console.log(distinctAverages(nums)); \ No newline at end of file diff --git a/javascript/LeetCode/Array/3065.js b/javascript/LeetCode/Array/3065.js new file mode 100644 index 0000000..3b9e3f6 --- /dev/null +++ b/javascript/LeetCode/Array/3065.js @@ -0,0 +1,22 @@ +/** + * 3065. Minimum Operations to Exceed Threshold Value I + * + * 一次操作步驟可移除一個nums最小的元素,回傳共需要多少次才能把nums內所有元素 >= k + * 要把所有比k小的元素都移除,簡單來講就是要計算nums中有幾個元素比k小。 + * + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var minOperations = function(nums, k) { + let result = 0; + for(let i = 0;i < nums.length;i++) { + if(nums[i] < k){ + result++; + } + } + return result; +}; +let nums = [2,11,10,1,3], k = 10; +// 3 +console.log(minOperations(nums,k)); \ No newline at end of file diff --git a/javascript/LeetCode/Array/3162.js b/javascript/LeetCode/Array/3162.js new file mode 100644 index 0000000..94f2840 --- /dev/null +++ b/javascript/LeetCode/Array/3162.js @@ -0,0 +1,40 @@ +/** + * 3162. Find the Number of Good Pairs I + * + * good if nums1[i] is divisible by nums2[j] * k (0 <= i <= n - 1, 0 <= j <= m - 1). + * @param {number[]} nums1 + * @param {number[]} nums2 + * @param {number} k + * @return {number} + */ +var numberOfPairs = function(nums1, nums2, k) { + // solution 1. + // let count = 0; + // for(let i = 0;i < nums1.length;i++) { + // for(let j = 0;j < nums2.length;j++) { + // if(nums1[i] % (nums2[j] * k) == 0){ + // count++; + // } + // } + // } + // return count; + + // solution 2. + let count = 0; + let map = new Map(); + for(let i of nums2) { + map.set(i,(map.get(i) || 0 ) + 1); + } + for(let i of nums1) { + // kwy mean j. + for(const [key,value] of map) { + if(i % (key * k)=== 0){ + count+= value; + } + } + } + return count; +}; +let nums1 = [1,3,4], nums2 = [1,3,4], k = 1; +// 5 +console.log(numberOfPairs(nums1,nums2,k)) \ No newline at end of file diff --git a/javascript/LeetCode/Array/3477.js b/javascript/LeetCode/Array/3477.js new file mode 100644 index 0000000..8304fdd --- /dev/null +++ b/javascript/LeetCode/Array/3477.js @@ -0,0 +1,46 @@ +/** + * 3477. Fruits Into Baskets II + * + * 參數為兩個數值陣列,fruits,baskets。 + * fruits表示每種水果的數量;baskets表示籃子最多可裝幾個 + * + * 從左到右須遵守: + * 每種水果要放在>=它的位置上 + * 每個籃子最多只能裝一種水果 + * 若水果無法裝進任何籃子,unplaced + * + * 回傳有幾個unplaced + * @param {number[]} fruits + * @param {number[]} baskets + * @return {number} + */ +var numOfUnplacedFruits = function(fruits, baskets) { + // 要找出有幾樣水果無法放進籃子內 + // 能放進去的規則:籃子的容量必須要 >= 水果 + let unplaced = 0; + for(let i = 0;i < fruits.length;i++) { + let placed = false; + for(let j = 0;j < baskets.length;j++) { + if(fruits[i] <= baskets[j]){ + baskets[j]= -1; // 標記籃子有使用 + placed = true; + break; + } + } + if(!placed){ + unplaced++; + } + } + return unplaced; +}; +// let fruits = [4,2,5], baskets = [3,5,4]; +/** + * 1 + * fruits[0] = 4 is placed in baskets[1] = 5. + * fruits[1] = 2 is placed in baskets[0] = 3. + * fruits[2] = 5 cannot be placed in baskets[2] = 4. + * Since one fruit type remains unplaced, we return 1. + */ +let fruits = [1,4], baskets = [8,1]; +// 1 +console.log(numOfUnplacedFruits(fruits,baskets)); \ No newline at end of file diff --git a/javascript/LeetCode/math/2160.js b/javascript/LeetCode/math/2160.js new file mode 100644 index 0000000..44ff89b --- /dev/null +++ b/javascript/LeetCode/math/2160.js @@ -0,0 +1,61 @@ +/** + * 2160. Minimum Sum of Four Digit Number After Splitting Digits + * + * 將nums 拆成一組[new1,new2],這兩個皆是由nums各位數隨意組合而成。回傳new1 + new 2 加總而成的最小結果。 + * @param {number} num + * @return {number} + */ +var minimumSum = function(num) { + // 最小的2個數字相加 = 十位數:;其次較大的2個數字相加 = 個位數 + // index 奇位數相加 = new2 ;index 偶位數相加 = new1 + + // solution 1. + // let split = num.toString().split("").sort((a,b) => a - b); + // let new1 = ""; + // let new2 = ""; + // for(let i = 0; i < split.length;i++) { + // // 判斷index是偶數還是奇數 + // if(i % 2 !== 0){ + // new2 += split[i]; + // }else{ + // new1 += split[i]; + // } + // } + // return parseInt(new1) + parseInt(new2); + + // solution 2. (覺得第一個 & 第三個比較好) + // let split = num.toString().split("").sort((a,b) => a - b); + // let chunk = []; + // let new1 = "",new2 = ""; + // for (let i = 0; i < split.length; i++) { + // if(i % 2 !== 0){ + // chunk.push(split[i]); + // }else{ + // chunk.push(split[i]); + // } + // } + // for(let i = 0;i < chunk.length;i++){ + // // index是偶數還是奇數 + // if(i % 2 === 0){ + // new1 += chunk[i]; + // }else{ + // new2 += chunk[i]; + // } + // } + // return parseInt(new1) + parseInt(new2); + + // solution 3. + let chunk = num.toString().split("").sort((a,b) => a - b); + // 偶數位index值相加 + let new1 = chunk[0] + chunk[2]; + // 奇數位index值相加 + let new2 = chunk[1] + chunk[3]; + return parseInt(new1) + parseInt(new2); +}; +// let num = 2932; +// 52 +// Some possible pairs [new1, new2] are [29, 23], [223, 9], etc. +// The minimum sum can be obtained by the pair [29, 23]: 29 + 23 = 52. +let num = 2687; +// 95 (27 + 68) +console.log(minimumSum(num)); \ No newline at end of file diff --git a/javascript/geeksforgeeks/medium.js b/javascript/geeksforgeeks/medium.js index a66c18b..bec3a7c 100644 --- a/javascript/geeksforgeeks/medium.js +++ b/javascript/geeksforgeeks/medium.js @@ -663,4 +663,56 @@ var kFreq = function(text,k) { } // const text = 'Welcome to the world of Geeks Geeks for Geeks is great'; // const k = 3; -// console.log(kFreq(text,k)); \ No newline at end of file +// console.log(kFreq(text,k)); + +/** + * K closest elements + * + * Given a sorted array arr[] of unique elements and a value x, find the k closest elements to x in arr[]. + * Note that if the element is present in array, then it should not be in output, only the other closest elements are required. + * + * Examples: + * Input: k = 4, x = 35, arr[] = [12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56] + * Output: 39 30 42 45 + * + * Input: k = 2, x = 4, arr[] = {1, 3, 4, 10, 12} + * Output: 3 1 + * + * Hints: + * An element a is closer to x than b if: + * |a - x| < |b - x|, or + * |a - x| == |b - x| and a > b (i.e., prefer the larger element if tied) + * + * 找出最接近x值的k個元素,若x也在陣列中,不要讓它出現 + * + * @param {number} k + * @param {number} x + * @param {number[]} arr + * @returns {number[]} + */ +var kClosetEle = function(k,x,arr){ + // (arr[i] - x) < (arr[i + 1] - x ) or (arr[i] - x) === (arr[i+1] - x) && arr[i] > arr[i + 1] + let res = []; + arr.sort((a,b) =>{ + let diffA = Math.abs(a - x); + let diffB = Math.abs(b - x); + // prefer larger element + if (diffA === diffB){ + return b - a; + } + return diffA - diffB; + }) + for(let i = 0;i < arr.length;i++) { + if((Math.abs(arr[i] - x)) < Math.abs((arr[i + 1] - x)) && arr[i] !== x){ + res.push(arr[i]); + } + if(res.length === k){ + break; + } + } + return res; +} + +// let k = 4, x = 35, arr = [12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56]; +// [39,30,42,45] +// console.log(kClosetEle(k,x,arr)); \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index 05b943a..944814e 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -844,40 +844,7 @@ let words = ["ab","ty","yt","lc","cl","ab"]; // console.log(longestPalindrome(words)); -/** - * K closest elements - * - * Given a sorted array arr[] of unique elements and a value x, find the k closest elements to x in arr[]. - * - * Note that if the element is present in array, then it should not be in output, only the other closest elements are required. - * - * Examples: - * Input: k = 4, x = 35, arr[] = [12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56] - * Output: 39 30 42 45 - * - * Input: k = 2, x = 4, arr[] = {1, 3, 4, 10, 12} - * Output: 3 1 - * - * 找出最接近x值的k個元素,若x也在陣列中,不要讓它出現 - * @param {number} k - * @param {number} x - * @param {number[]} arr - * @returns {number[]} - */ -var kClosetEle = function(k,x,arr){ - let res = []; - let diff = 0; - for(let i = 0;i < arr.length;i++) { - if(Math.abs(arr[i] - x) ){ - - } - } -} - -// let k = 4, x = 35, arr = [12, 16, 22, 30, 35, 39, 42, 45, 48, 50, 53, 55, 56]; -// [39,30,42,45] -// console.log(kClosetEle(k,x,arr)); /** * 2929. Distribute Candies Among Children II @@ -963,3 +930,102 @@ var answerString = function(word, numFriends) { }; +/** + * 594. Longest Harmonious Subsequence + * + * harmonious array = 最大值與最小值差1 + * 回傳能夠成harmonious array的子序列長度 + * @param {number[]} nums + * @return {number} + */ +var findLHS = function(nums) { + +}; +// let nums = [1,3,2,2,5,2,3,7]; +// 5 +// console.log(findLHS(nums)); + +/** + * 2 Sum - Pair Sum Closest to Target using Binary Search + * + * Given an array arr[] of n integers and an integer target, the task is to find a pair in arr[] such that it’s sum is closest to target. + * Note: Return the pair in sorted order and if there are multiple such pairs return the pair with maximum absolute difference. + * If no such pair exists return an empty array. + * + * 從陣列中找出一組加總後最接近target的陣列元素 + * 以asc排序回傳,若有好幾組,則回傳差最大的 + * + * Examples: + * Input: arr[] = [10, 30, 20, 5], target = 25 + * Output: [5, 20] + * Explanation: Out of all the pairs, [5, 20] has sum = 25 which is closest to 25. + * + * + * Input: arr[] = [5, 2, 7, 1, 4], target = 10 + * Output: [2, 7] + * Explanation: As (4, 7) and (2, 7) both are closest to 10, but absolute difference of (2, 7) is 5 and (4, 7) is 3. Hence,[2, 7] has maximum absolute difference and closest to target. + * + * Input: arr[] = [10], target = 10 + * Output: [] + * Explanation: As the input array has only 1 element, return an empty array. + * + * + * @param {number[]} arr + * @param {number} target + * @returns {number[]} + */ +function sumClosest(arr, target) { + // binary search + // arr.sort((a,b) => a - b); + // let left = 0; + // let right = arr.length - 1; + + if(arr.length <= 1){ + return []; + } + + +} +// let arr = [5, 2, 7, 1, 4]; +// let target = 10; +// [2,7] +// console.log(sumClosest(arr, target)); + +/** + * Closest pair from two sorted arrays + * + * Given two arrays arr1[0...m-1] and arr2[0..n-1], and a number x, + * the task is to find the pair arr1[i] + arr2[j] such that absolute value of (arr1[i] + arr2[j] - x) is minimum. + * + * Example: + * Input: + * arr1[] = {1, 4, 5, 7}; + * arr2[] = {10, 20, 30, 40}; + * x = 32 + * Output: 1 and 30 + * + * Input: + * arr1[] = {1, 4, 5, 7}; + * arr2[] = {10, 20, 30, 40}; + * x = 50 + * Output: 7 and 40 + * + * 兩個數值陣列參數和數值x,找出arr1[i] + arr2[j] 是arr1[i] + arr2[j] - x的最小值 + * + * @param {number[]} arr1 + * @param {number[]} arr2 + * @param {number} x + * @return {number[]} + */ +var closetPair = function(arr1,arr2,x) { + let result = []; + for(let i = 0;i < arr1.length;i++) { + + } + +} +// let arr1 = [1,4,5,7],arr2 = [10,20,30,40],x = 32; +// [1,30]; +// console.log(closetPair(arr1,arr2,x)); + + diff --git a/javascript/nodejs/app.js b/javascript/nodejs/app.js index 724795b..02003ad 100644 --- a/javascript/nodejs/app.js +++ b/javascript/nodejs/app.js @@ -5,7 +5,6 @@ const port = 3000; const server = createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); - url res.end('Hello World'); }); server.listen(port, hostname, () => {