-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmayalgo.js
More file actions
49 lines (41 loc) · 1.29 KB
/
mayalgo.js
File metadata and controls
49 lines (41 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
// Return the running sum of nums.
const runningSum = (arr) => {
let newArr = [arr[0]]
for(let i = 1; i < arr.length; i++){
newArr.push(newArr[i - 1] + arr[i]);
}
return newArr;
}
// console.log(runningSum([3,1,2,10,1]));
const ipDefang = (str) => {
let ipArr = str.split('');
for(let i =0; i < ipArr.length; i++){
if(ipArr[i] === '.') {
ipArr[i] = '[.]'
}
}
return ipArr.join('');
}
// console.log(ipDefang("1.1.1.1"))
//Kids with the greatest number of candies
// Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.
// For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.
const candyKids = (candies, extraCandies) => {
let highestNum = 0;
let output = [];
for(let el of candies){
if(el > highestNum){
highestNum = el;
}
}
for(let el of candies){
if(el + extraCandies >= highestNum){
output.push(true)
} else{
output.push(false);
}
}
return output;
}
console.log(candyKids([2,3,5,1,3], 3))