Skip to content

Commit 38270f5

Browse files
committed
練習codewars
1 parent 8d1f1a4 commit 38270f5

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

javascript/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,3 +1159,27 @@ var specialTriplets = function(nums) {
11591159
*/
11601160
// console.log(specialTriplets(nums));
11611161

1162+
/**
1163+
* 3kyu - How many are smaller than me II?
1164+
*
1165+
* @param {number[]} arr
1166+
* @returns {number[]}
1167+
*/
1168+
function smaller(arr) {
1169+
// 回傳arr[i]的右邊有幾個是小於自己的
1170+
let ans = [];
1171+
for(let i = 0;i < arr.length;++i) {
1172+
let count = 0;
1173+
for(let j = i + 1;j < arr.length;j++) {
1174+
if(arr[i] === arr[j]){
1175+
continue;
1176+
}
1177+
if(arr[i] > arr[j]){
1178+
count++;
1179+
}
1180+
}
1181+
ans[i] = count;
1182+
}
1183+
return ans;
1184+
}
1185+
console.log(assert.equal(smaller([5, 4, 7, 9, 2, 4, 1, 4, 5, 6]), [5, 2, 6, 6, 1, 1, 0, 0, 0, 0]))

0 commit comments

Comments
 (0)