Skip to content
Open
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
10 changes: 10 additions & 0 deletions jan-week-3/arrayPairSum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Example 1:
Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
Note:
n is a positive integer, which is in the range of [1, 10000].
All the integers in the array will be in the range of [-10000, 10000].
12 changes: 12 additions & 0 deletions jan-week-3/arrayPairSum/arrayPairSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const arrayPairSum = (nums) => {
let sum = 0;
const sorted = nums.sort((a, b) => a - b);

for (let i = 0; i < sorted.length; i += 2) {
sum += sorted[i];
}

return sum;
};

module.exports = arrayPairSum;
9 changes: 9 additions & 0 deletions jan-week-3/arrayPairSum/arrayPairSum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const arrayPairSum = require('./arrayPairSum');

describe('arrayPairSum', () => {

test('should return sum of pairs', () => {
expect(arrayPairSum([1, 4, 3, 2])).toEqual(4);
});

});
24 changes: 24 additions & 0 deletions jan-week-3/dayOfTheWeek/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Given a date, return the corresponding day of the week for that date.

### The input is given as three integers representing the day, month and year respectively.

### Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

### Example 1:

Input: day = 31, month = 8, year = 2019
Output: "Saturday"

### Example 2:

Input: day = 18, month = 7, year = 1999
Output: "Sunday"

### Example 3:

Input: day = 15, month = 8, year = 1993
Output: "Sunday"

### Constraints:

The given dates are valid dates between the years 1971 and 2100.
6 changes: 6 additions & 0 deletions jan-week-3/dayOfTheWeek/dayOfTheWeek.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const dayOfTheWeek = (day, month, year) => {
const week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return week[new Date(year + '-' + month + '-' + day).getDay()];
};

module.exports = dayOfTheWeek;
9 changes: 9 additions & 0 deletions jan-week-3/dayOfTheWeek/dayOfTheWeek.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const dayOfTheWeek = require('./dayOfTheWeek');

describe('dayOfTheWeek', () => {

test('should return day of the week', () => {
expect(dayOfTheWeek(31, 8, 2019)).toBe("Saturday");
});

});
33 changes: 33 additions & 0 deletions jan-week-3/distributeCandies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
We distribute some number of candies, to a row of n = num_people people in the following way:

We then give 1 candy to the first person, 2 candies to the second person, and so on until we give n candies to the last person.

Then, we go back to the start of the row, giving n + 1 candies to the first person, n + 2 candies to the second person, and so on until we give 2 * n candies to the last person.

This process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies. The last person will receive all of our remaining candies (not necessarily one more than the previous gift).

Return an array (of length num_people and sum candies) that represents the final distribution of candies.

Example 1:

Input: candies = 7, num_people = 4
Output: [1,2,3,1]
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0,0].
On the third turn, ans[2] += 3, and the array is [1,2,3,0].
On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1].
Example 2:

Input: candies = 10, num_people = 3
Output: [5,2,3]
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0].
On the third turn, ans[2] += 3, and the array is [1,2,3].
On the fourth turn, ans[0] += 4, and the final array is [5,2,3].

Constraints:

1 <= candies <= 10^9
1 <= num_people <= 1000
22 changes: 22 additions & 0 deletions jan-week-3/distributeCandies/distributeCandies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const distributeCandies = (candies, num_people) => {
let count = 0;
let arr = Array(num_people).fill(0);
while (candies > 0) {
for (let i = 0; i < num_people; i++) {
count += 1;
if (candies - count > 0) {
candies -= count;
arr[i] += count;
} else {
arr[i] += candies;
candies = 0;
}
if (candies <= 0) {
break;
}
}
}
return arr;
};

module.exports = distributeCandies;
9 changes: 9 additions & 0 deletions jan-week-3/distributeCandies/distributeCandies.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const distributeCandies = require('./distributeCandies');

describe('distributeCandies', () => {

test('should return the distributed candies', () => {
expect(distributeCandies(7, 4)).toEqual([1, 2, 3, 1]);
});

});
13 changes: 13 additions & 0 deletions jan-week-3/findDisappearedNumbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]
30 changes: 30 additions & 0 deletions jan-week-3/findDisappearedNumbers/findDisappearedNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const findDisappearedNumbers = (nums) => {
// let map = {};
// let result = [];

// for (let number of nums) {
// map[number] = map[number] ? map[number] += 1 : 1;
// }

// for (let i = 1; i <= nums.length; i++) {
// if (!map[i]) {
// result.push(i);
// }
// }

// return result;
let res = []
nums.forEach((val, ind, arr) => {
let tmp = Math.abs(arr[ind]) - 1;
if (arr[tmp] > 0)
arr[tmp] *= -1;
})
console.log(nums);
nums.forEach((val, ind) => {
if (val > 0)
res.push(ind + 1)
})
return res
};

module.exports = findDisappearedNumbers;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const findDisappearedNumbers = require('./findDisappearedNumbers');

describe('findDisappearedNumbers', () => {

test('should return disappeared numbers', () => {
expect(findDisappearedNumbers([4, 3, 2, 7, 8, 2, 3, 1])).toEqual([5, 6]);
});

});
21 changes: 21 additions & 0 deletions jan-week-3/findOcurrences/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second.

### For each such occurrence, add "third" to the answer, and return the answer.

### Example 1:

Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
Output: ["girl","student"]

### Example 2:

Input: text = "we will we will rock you", first = "we", second = "will"
Output: ["we","rock"]


### Note:

1 <= text.length <= 1000
text consists of space separated words, where each word consists of lowercase English letters.
1 <= first.length, second.length <= 10
first and second consist of lowercase English letters.
14 changes: 14 additions & 0 deletions jan-week-3/findOcurrences/findOcurrences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const findOccurrences = (text, first, second) => {
const words = text.split(' ');
let third = [];

for (let i = 0; i < words.length; i++) {
if (words[i - 1] === second && words[i - 2] === first) {
third.push(words[i]);
}
}

return third;
};

module.exports = findOccurrences;
9 changes: 9 additions & 0 deletions jan-week-3/findOcurrences/findOcurrences.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const findOccurrences = require('./findOcurrences');

describe('findOccurrences', () => {

test('should return occurrences', () => {
expect(findOccurrences("alice is a good girl she is a good student", "a", "good")).toEqual(["girl", "student"]);
});

});
15 changes: 15 additions & 0 deletions jan-week-3/heightChecker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Students are asked to stand in non-decreasing order of heights for an annual photo.

Return the minimum number of students not standing in the right positions. (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.)

Example 1:

Input: [1,1,4,2,1,3]
Output: 3
Explanation:
Students with heights 4, 3 and the last 1 are not standing in the right positions.

Note:

1 <= heights.length <= 100
1 <= heights[i] <= 100
13 changes: 13 additions & 0 deletions jan-week-3/heightChecker/heightChecker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const heightChecker = (heights) => {
let sorted = heights.slice().sort((a, b) => a - b);
let count = 0;

for (let i = 0; i < sorted.length; i++) {
if (heights[i] !== sorted[i]) {
count++;
}
}
return count;
};

module.exports = heightChecker;
9 changes: 9 additions & 0 deletions jan-week-3/heightChecker/heightChecker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const heightChecker = require('./heightChecker');

describe('heightChecker', () => {

test('should return the height in ascending order', () => {
expect(heightChecker([1, 1, 4, 2, 1, 3])).toEqual(3);
});

});
11 changes: 11 additions & 0 deletions jan-week-3/keyboardRow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.

### Example:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

### Note:

You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
9 changes: 9 additions & 0 deletions jan-week-3/keyboardRow/keyboardRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const top = /^[qwertyuiop]+$/i;
const mid = /^[asdfghjkl]+$/i;
const bottom = /^[zxcvbnm]+$/i;
const layouts = [top, mid, bottom];

const findWords = words =>
words.filter(word => layouts.some(layout => layout.test(word)));

module.exports = findWords;
10 changes: 10 additions & 0 deletions jan-week-3/keyboardRow/keyboardRow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

const findWords = require('./keyboardRow');

describe('findWords', () => {

test('should return the words', () => {
expect(findWords(["Hello", "Alaska", "Dad", "Peace"])).toEqual(["Alaska", "Dad"]);
});

});
29 changes: 29 additions & 0 deletions jan-week-3/minimumAbsDifference/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.

### Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows

a, b are from arr
a < b
b - a equals to the minimum absolute difference of any two elements in arr


### Example 1:

Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.

### Example 2:

Input: arr = [1,3,6,10,15]
Output: [[1,3]]

### Example 3:

Input: arr = [3,8,-10,23,19,-4,-14,27]
Output: [[-14,-10],[19,23],[23,27]]

### Constraints:

2 <= arr.length <= 10^5
-10^6 <= arr[i] <= 10^6
22 changes: 22 additions & 0 deletions jan-week-3/minimumAbsDifference/minimumAbsDifference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const minimumAbsDifference = (arr) => {
arr.sort((a, b) => a - b);
let differences = [];
let minDiff = Infinity;
let i = 1;

while (i < arr.length) {
let currentDiff = Math.abs(arr[i - 1] - arr[i]);

if (currentDiff < minDiff) {
differences = [[arr[i - 1], arr[i]]];
minDiff = currentDiff;
} else if (currentDiff === minDiff) {
differences.push([arr[i - 1], arr[i]]);
}
i++;
}

return differences;
};

module.exports = minimumAbsDifference;
12 changes: 12 additions & 0 deletions jan-week-3/minimumAbsDifference/minimumAbsDifference.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const minimumAbsDifference = require('./minimumAbsDifference');

describe('minimumAbsDifference', () => {

test('should return pairs with min difference', () => {
expect(minimumAbsDifference([4, 2, 1, 3])).toEqual([[1, 2], [2, 3], [3, 4]]);
expect(minimumAbsDifference([1, 3, 6, 10, 15])).toEqual([[1, 3]]);
expect(minimumAbsDifference([3, 8, -10, 23, 19, -4, -14, 27]))
.toEqual([[-14, -10], [19, 23], [23, 27]]);
});

});
Loading