Skip to content

Commit cd20b50

Browse files
committed
3318 not done
1 parent 7c59c48 commit cd20b50

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

javascript/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,3 +1049,31 @@ let numerator = 1, denominator = 2;
10491049

10501050

10511051

1052+
/**
1053+
* 3318. Find X-Sum of All K-Long Subarrays I
1054+
*
1055+
* 計算每個元素出現次數但只保留出現次數 = x 次的元素,若有超過2個元素,則只保留元素較大的那個
1056+
* 以陣列型態計算連續子陣列元素總和
1057+
*
1058+
* @param {number[]} nums
1059+
* @param {number} k
1060+
* @param {number} x
1061+
* @return {number[]}
1062+
*/
1063+
var findXSum = function(nums, k, x) {
1064+
// 將nums切成長度 = k的,並計算在裡面有幾個是連續子陣列且元素出現次數 = x的
1065+
// 最後將符合的加總,並組成新陣列,形成元素i
1066+
let res = [];
1067+
let map = new Map();
1068+
1069+
for(let i = 0;i < k;++i) {
1070+
map.has(nums[i]) ? map.set(nums[i],map.get(nums[i]) + 1) : map.set(nums[i],1);
1071+
}
1072+
1073+
};
1074+
let nums = [1,1,2,2,3,4,2,3], k = 6, x = 2;
1075+
// [6,10,12]
1076+
// For subarray [1, 1, 2, 2, 3, 4], only elements 1 and 2 will be kept in the resulting array. Hence, answer[0] = 1 + 1 + 2 + 2.
1077+
// For subarray [1, 2, 2, 3, 4, 2], only elements 2 and 4 will be kept in the resulting array. Hence, answer[1] = 2 + 2 + 2 + 4. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.
1078+
// For subarray [2, 2, 3, 4, 2, 3], only elements 2 and 3 are kept in the resulting array. Hence, answer[2] = 2 + 2 + 2 + 3 + 3.
1079+
console.log(findXSum(nums,k,x));

0 commit comments

Comments
 (0)