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
8 changes: 8 additions & 0 deletions dec_week_4/addDigits/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

### Example:

Input: 38
Output: 2
Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
Since 2 has only one digit, return it.
24 changes: 24 additions & 0 deletions dec_week_4/addDigits/addDigits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// const addDigits = (num) => {
// let result = 0;

// while (num >= 10) {
// result += num % 10;
// num = Math.floor(num / 10);

// if (num < 10) {
// num += result;
// result = 0;
// }
// }

// return num;
// };

const addDigits = (num) => {
if (isNaN(num) || num === 0) return 0;
if (num < 10) return num;

return num % 9 === 0 ? 9 : num % 9;
};

module.exports = addDigits;
Empty file.
24 changes: 24 additions & 0 deletions dec_week_4/flipAndInvertImage/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.

### To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].

### To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].

### Example 1:

Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]

### Example 2:

Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]

### Notes:

1 <= A.length = A[0].length <= 20
0 <= A[i][j] <= 1
11 changes: 11 additions & 0 deletions dec_week_4/flipAndInvertImage/flipAndInvertImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const flipAndInvertImage = (A) => {
let result = [];
for (let array of A) {
result.push(array.reverse().map((ele) => ele === 1 ? 0 : 1));
}
return result;
};

// const flipAndInvertImage = A => A.map(row => row.map(val => val ^ 1).reverse());

module.exports = flipAndInvertImage;
Empty file.
25 changes: 25 additions & 0 deletions dec_week_4/isUgly/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
### Write a program to check whether a given number is an ugly number.

### Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

### Example 1:

Input: 6
Output: true
Explanation: 6 = 2 × 3

### Example 2:

Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2

### Example 3:

Input: 14
Output: false
Explanation: 14 is not ugly since it includes another prime factor 7.
### Note:

1 is typically treated as an ugly number.
Input is within the 32-bit signed integer range: [−231, 231 − 1].
16 changes: 16 additions & 0 deletions dec_week_4/isUgly/isUgly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const isUgly = (num) => {
if (num <= 0) return false;
if (num < 5) return true;
while (num % 2 == 0) {
num = num / 2;
}
while (num % 3 == 0) {
num = num / 3;
}
while (num % 5 == 0) {
num = num / 5;
}
return (num == 1);
};

module.exports = isUgly;
Empty file.
29 changes: 29 additions & 0 deletions dec_week_4/minTimeToVisitAllPoints/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to find the minimum time in seconds to visit all points.

### You can move according to the next rules:

### In one second always you can either move vertically, horizontally by one unit or diagonally (it means to move one unit vertically and one unit horizontally in one second).
You have to visit the points in the same order as they appear in the array.


### Example 1:

Input: points = [[1,1],[3,4],[-1,0]]
Output: 7
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
Time from [1,1] to [3,4] = 3 seconds
Time from [3,4] to [-1,0] = 4 seconds
Total time = 7 seconds

### Example 2:

Input: points = [[3,2],[-2,2]]
Output: 5


Constraints:

points.length == n
1 <= n <= 100
points[i].length == 2
-1000 <= points[i][0], points[i][1] <= 1000
13 changes: 13 additions & 0 deletions dec_week_4/minTimeToVisitAllPoints/minTimeToVisitAllPoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const minTimeToVisitAllPoints = (points) => {
let minTime = 0;

for (let i = 1; i < points.length; ++i) {
const point0 = points[i - 1];
const point1 = points[i];
minTime += Math.max(Math.abs(point1[0] - point0[0]), Math.abs(point1[1] - point0[1]));
}

return minTime;
};

module.exports = minTimeToVisitAllPoints;
Empty file.
18 changes: 18 additions & 0 deletions dec_week_4/robotToOrigin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

### The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

### Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

### Example 1:

Input: "UD"
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.


### Example 2:

Input: "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.
20 changes: 20 additions & 0 deletions dec_week_4/robotToOrigin/robotToOrigin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const judgeCircle = (moves) => {
let horizontal = 0;
let vertical = 0;

for (let i = 0; i < moves.length; i++) {
if (moves[i] === 'U') {
vertical++;
} else if (moves[i] === 'D') {
vertical--;
} else if (moves[i] === 'R') {
horizontal++;
} else if (moves[i] === 'L') {
horizontal--;
}
}

return (vertical === 0 && horizontal === 0) ? true : false;
};

module.exports = judgeCircle;
Empty file.
18 changes: 18 additions & 0 deletions dec_week_4/sortedSquares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

### Example 1:

Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]

### Example 2:

Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]


Note:

1 <= A.length <= 10000
-10000 <= A[i] <= 10000
A is sorted in non-decreasing order.
19 changes: 19 additions & 0 deletions dec_week_4/sortedSquares/sortedSquares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// const sortedSquares = (A) => {
// const result = [];
// let head = 0;
// let tail = A.length - 1;

// while (head <= tail) {
// if (A[head] ** 2 > A[tail] ** 2) result.push(A[head++] ** 2);
// else result.push(A[tail--] ** 2);
// }

// return result.reverse();
// };

const sortedSquares = (A) =>
A
.map((number) => Math.abs(number * number))
.sort((a, b) => a - b);

module.exports = sortedSquares;
Empty file.
16 changes: 16 additions & 0 deletions dec_week_4/validAnagram/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### Given two strings s and t , write a function to determine if t is an anagram of s.

### Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

### Example 2:

Input: s = "rat", t = "car"
Output: false
Note:
You may assume the string contains only lowercase alphabets.

### Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
56 changes: 56 additions & 0 deletions dec_week_4/validAnagram/validAnagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// const getFreq = (s) => s.reduce((acc, curr) => {
// acc[curr] = acc[curr] ? acc[curr] + 1 : 1;
// return acc;
// }, {});

// const getSize = (obj) => {
// var size = 0, key;
// for (key in obj) {
// if (obj.hasOwnProperty(key)) size++;
// }
// return size;
// };

// const checkEquality = (s, t) => {
// for (let key in s) {
// if (!t.hasOwnProperty(key) || s[key] !== t[key]) {
// return false;
// }
// }
// return true;
// }

// const eqObject = (s, t) => {
// let sizeS = getSize(s);
// let sizeT = getSize(t);

// if (sizeS > sizeT) {
// return checkEquality(s, t);
// }

// return checkEquality(t, s);
// };

// const isAnagram = (s, t) => {
// const mapS = getFreq(s.split(''));
// const mapT = getFreq(t.split(''));

// return eqObject(mapS, mapT);
// };

const isAnagram = (s, t) => {
if (s.length !== t.length) {
return false;
}

let arr = [...Array(26)].fill(0);
let codeA = "a".charCodeAt(0);
for (let i = 0; i < s.length; i++) {
arr[s.charCodeAt(i) - codeA]++;
arr[t.charCodeAt(i) - codeA]--;
}

return arr.every(el => el === 0);
};

module.exports = isAnagram;
Empty file.
14 changes: 14 additions & 0 deletions dec_week_4_2/Encode and Decode TinyURL/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let obj = {};
const origin = "http://tinyurl.com/";

const encode = (longUrl) => {
const randomString = Math.random().toString(36).substring(5);
obj[randomString] = longUrl;
return origin + randomString;

};

const decode = (shortUrl) => {
let getKey = shortUrl.split(origin);
return obj[getKey[1]];
};
18 changes: 18 additions & 0 deletions dec_week_4_2/FindElements/FindElements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class FindElements {
constructor(root) {
this.set = new Set();
this.dfs(root, 0);
}

dfs(node, val) {
this.set.add(val);
node.left && this.dfs(node.left, val * 2 + 1);
node.right && this.dfs(node.right, val * 2 + 2);
}

find(target) {
return this.set.has(target);
}
}

module.exports = FindElements;
Loading