Skip to content

Commit 0820521

Browse files
committed
new problem: 1346
1 parent 841135f commit 0820521

File tree

10 files changed

+262
-0
lines changed

10 files changed

+262
-0
lines changed

LeetCode/1346/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const checkIfExist = function (arr) {
2+
if (arr.length === 0) return;
3+
let map = new Map();
4+
5+
for (let i = 0; i < arr.length; i++) {
6+
map.set(`${arr[i]}`, { value: arr[i], index: i });
7+
}
8+
9+
for (let i = 0; i < arr.length; i++) {
10+
const hasDoubleI = map.has(`${arr[i] * 2}`);
11+
const hasDoubleII = map.has(`${arr[i] / 2}`);
12+
13+
if (hasDoubleI || hasDoubleII) {
14+
const idxDoubleI = hasDoubleI ? map.get(`${arr[i] * 2}`).index : -1;
15+
const idxDoubleII = hasDoubleII ? map.get(`${arr[i] / 2}`).index : -1;
16+
if (i !== idxDoubleI || i !== idxDoubleII) {
17+
return true;
18+
}
19+
}
20+
}
21+
22+
return false;
23+
};
24+
25+
console.log(checkIfExist([10, 2, 5, 3, 4]));
26+
27+
// second solution with some()
28+
29+
const checkIfExist2 = (arr) => {
30+
if (arr.length === 0) return;
31+
32+
return (
33+
((x) => arr.some((n) => n === x * 2 || n === x / 2))(arr.pop()) ||
34+
checkIfExist(arr)
35+
);
36+
};
37+
38+
console.log(checkIfExist2([10, 2, 5, 3, 4]));

LeetCode/Combination-Sum/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Given a set of candidate numbers without duplicates and a target number. Write a function
3+
* returns all unique combinations in candidates summing to the target.
4+
*
5+
* Notes: The same repeated numbers maybe chosen from unlimited times.
6+
*
7+
* Exp: Input candidates = [2, 3, 5], target = 8;
8+
* Output: [
9+
* [2, 2, 2, 2],
10+
* [2, 3, 3],
11+
* [3, 5]
12+
* ]
13+
*/
14+
15+
const combinationSum = (candidates, target) => {
16+
let combinations = [];
17+
let idx = 0;
18+
19+
// Sorting the candidates array
20+
candidates.sort((a, b) => a - b);
21+
22+
// Check the special case, target = 0;
23+
if (target === 0) return [];
24+
};
25+
26+
combinationSum([1, 2, 2], 0);

LeetCode/Delete and Earn/deleteAndEarn.js

Whitespace-only changes.

LeetCode/Delete and Earn/deleteAndEarn.spec.js

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Given two strings, write a function returns the longest common substring
3+
*
4+
* example: theFunctionName("ABAB", "BABA") would return "ABA" or "BAB"
5+
*/
6+
7+
// The first thing we will try is using a bruteforce method
8+
/**
9+
* "ABAB"
10+
* ^
11+
* ^
12+
* "BABA"
13+
* ^
14+
* ^
15+
*/
16+
17+
// Helper function
18+
19+
const isSame = (x, y) => {
20+
if (x === y) {
21+
return true;
22+
}
23+
return false;
24+
};
25+
26+
module.exports.longestCommonSubstring = (a, b) => {
27+
// convert s1 and s2 to array by split function
28+
s1 = a.split("");
29+
s2 = b.split("");
30+
31+
// Initialize the result
32+
let result = [];
33+
34+
// Initial checking
35+
if (s1.length === 0 || s2.length === 0) return "";
36+
37+
// Initialize the 2-dimesional array
38+
let cache = new Array(s1.length).map(x => new Array(s2.length));
39+
40+
for (let i = 0; i < s1.length; i++) {
41+
for (let j = 0; j < s2.length; j++) {
42+
if (isSame(s1[i], s2[j])) {
43+
if (i === 0 || j === 0) {
44+
cache[i][j] = 1;
45+
} else {
46+
cache[i][j] = cache[i - 1][j - 1] + 1;
47+
}
48+
if (cache[i][j] > result.length) {
49+
result = this.longestCommonSubstring(i - length);
50+
}
51+
}
52+
}
53+
}
54+
};

LeetCode/Longest Common Substring/longestCommonSubstring.spec.js

Whitespace-only changes.

LeetCode/a-b-c-d-pairs/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Problem: Given an integer N = 1000. Print all positive integer solutions to the equation a^3 + b^3 = c^3 + d^3 where a, b, c and d are integer between 1 and N
3+
*/
4+
5+

LeetCode/addTwoNumbers/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function ListNode(val) {
2+
this.val = val;
3+
this.next = null;
4+
}
5+
6+
ListNode.prototype.addNode = function(node) {
7+
if (this.next === null) {
8+
this.next = node;
9+
}
10+
};
11+
12+
const addTwoNumbers = function(l1, l2) {
13+
// Your code here
14+
let result = new ListNode(-1);
15+
let cur = result;
16+
let carry = 0;
17+
18+
while (l1 || l2) {
19+
let n1 = l1 ? l1.val : 0;
20+
let n2 = l2 ? l2.val : 0;
21+
let sum = n1 + n2 + carry;
22+
23+
carry = Math.floor(sum / 10);
24+
cur.next = new ListNode(sum % 10);
25+
cur = cur.next;
26+
if (l1) {
27+
l1 = l1.next;
28+
}
29+
if (l2) {
30+
l2 = l2.next;
31+
}
32+
}
33+
if (carry) {
34+
cur.next = new ListNode(1);
35+
}
36+
return result.next;
37+
};
38+
39+
let l1 = new ListNode(3),
40+
val1 = new ListNode(4),
41+
val2 = new ListNode(2);
42+
let l2 = new ListNode(4),
43+
val3 = new ListNode(6),
44+
val4 = new ListNode(5);
45+
l1.addNode(val1);
46+
val1.addNode(val2);
47+
l2.addNode(val3);
48+
val3.addNode(val4);
49+
50+
let l3 = new ListNode(9),
51+
vall3 = new ListNode(9),
52+
vall32 = new ListNode(9);
53+
54+
let l4 = new ListNode(8);
55+
56+
console.log(addTwoNumbers(l1, l2));

LeetCode/min-stack/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Min Stack (LeetCode 155)
3+
*
4+
* Disign a stack supporting push, pop, top, and retrieving the minimum element in a constant time.
5+
*
6+
* push(x) -- Push element x into stack
7+
* pop() -- Removes the element on the top of stack
8+
* top() -- Get the top element
9+
* getMinValue() -- Retrieve the minimum element in the stack
10+
*
11+
*/
12+
13+
// Consider "Broken Solution" for getMinValue()
14+
/**
15+
* Use a member variable to keep track of the minimum value
16+
* It works if there is no pop() operation
17+
* We have to use additional memory!
18+
*
19+
* So we can end up with a solution like below
20+
*/
21+
22+
// First solution
23+
24+
class MinStack_v1 {}

LeetCode/twoSum/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* First solution: Brute fore
3+
*/
4+
const twoSumI = function (nums, target) {
5+
if (nums.length <= 1) {
6+
return [];
7+
}
8+
for (let i = 0; i < nums.length - 1; i++) {
9+
for (let j = i + 1; j < nums.length; j++) {
10+
if (nums[i] + nums[j] === target) {
11+
return [i, j];
12+
}
13+
}
14+
}
15+
return [];
16+
};
17+
18+
/**
19+
* Second solution: Using with binary search
20+
*/
21+
const binarySearch = function (arr, value, start, end) {
22+
if (start > end) {
23+
return null;
24+
}
25+
let mid = Math.floor((start + end) / 2);
26+
if (arr[mid].value === value) return arr[mid].index;
27+
if (arr[mid].value > value) {
28+
return binarySearch(arr, value, start, mid - 1);
29+
}
30+
if (arr[mid].value < value) {
31+
return binarySearch(arr, value, mid + 1, end);
32+
}
33+
};
34+
const twoSumII = function (nums, target) {
35+
// create object contains elements in nums array and its indexes
36+
// exp: [{index: 0, value: 1}, {index: 1, value: 2}, ...]
37+
let objNums = [];
38+
for (let i = 0; i < nums.length; i++) {
39+
objNums.push({
40+
index: i,
41+
value: nums[i],
42+
});
43+
}
44+
// sort the objNums based e.value
45+
objNums.sort((a, b) => a.value - b.value > 0);
46+
// binary search the complement in objNums
47+
for (let i = 0; i < objNums.length; i++) {
48+
let complement = target - objNums[i].value;
49+
let complementIdx = binarySearch(objNums, complement, 0, objNums.length);
50+
if (complementIdx) {
51+
return [i, complementIdx];
52+
}
53+
}
54+
};
55+
56+
const twoSumIII = (arr, target) => {};
57+
58+
console.log(twoSumIII([1, 5, 2, 4, 3], 6));
59+
console.log(twoSumII([1, 5, 2, 4, 3], 6));

0 commit comments

Comments
 (0)