Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions javascript/LeetCode/Array/118.js
Original file line number Diff line number Diff line change
@@ -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));
76 changes: 76 additions & 0 deletions javascript/LeetCode/Array/1498.js
Original file line number Diff line number Diff line change
@@ -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));
68 changes: 68 additions & 0 deletions javascript/LeetCode/Array/2037.js
Original file line number Diff line number Diff line change
@@ -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));
67 changes: 67 additions & 0 deletions javascript/LeetCode/Array/2099.js
Original file line number Diff line number Diff line change
@@ -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));
35 changes: 35 additions & 0 deletions javascript/LeetCode/Array/2325.js
Original file line number Diff line number Diff line change
@@ -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));
59 changes: 59 additions & 0 deletions javascript/LeetCode/Array/2465.js
Original file line number Diff line number Diff line change
@@ -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));
22 changes: 22 additions & 0 deletions javascript/LeetCode/Array/3065.js
Original file line number Diff line number Diff line change
@@ -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));
Loading