Skip to content

Commit cd71920

Browse files
Generate tests classes
1 parent bb18500 commit cd71920

File tree

56 files changed

+4312
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4312
-0
lines changed

LeetcodeProblemsTests/3Sum_Test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const assert = require('assert');
2+
const threeSum = require('../LeetcodeProblems/3Sum').threeSum;
3+
4+
var test = function () {
5+
assert.deepEqual(threeSum([]), []);
6+
assert.deepEqual(threeSum([0]), []);
7+
assert.deepEqual(threeSum([0, 0]), []);
8+
assert.deepEqual(
9+
threeSum([0, 0, 0]),
10+
[[0, 0, 0]]
11+
);
12+
assert.deepEqual(
13+
threeSum([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),
14+
[[0, 0, 0]]
15+
);
16+
assert.deepEqual(
17+
threeSum([-1, 0, 1, 2, -1, -4]),
18+
[ [ -1, 2, -1 ], [ 0, 1, -1 ] ]
19+
);
20+
}
21+
22+
module.exports.test = test;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const assert = require('assert');
2+
const addTwoNumbers = require('../LeetcodeProblems/Add_Two_Numbers').addTwoNumbers;
3+
const ListNodeTestHelper = require('../utilsClasses/ListNodeTestHelper');
4+
var ListNode = require('../UtilsClasses/ListNode').ListNode;
5+
6+
function test() {
7+
const list1 = ListNode.linkenList([1,2,3,4]);
8+
const list2 = ListNode.linkenList([1,2,3,4]);
9+
ListNodeTestHelper.assertList(
10+
addTwoNumbers(list1, list2),
11+
[2, 4, 6, 8]
12+
);
13+
14+
const list3 = ListNode.linkenList([1]);
15+
const list4 = ListNode.linkenList([1,2]);
16+
ListNodeTestHelper.assertList(
17+
addTwoNumbers(list3, list4),
18+
[2, 2]
19+
);
20+
21+
const list5 = ListNode.linkenList([]);
22+
const list6 = ListNode.linkenList([1,2]);
23+
ListNodeTestHelper.assertList(
24+
addTwoNumbers(list5, list6),
25+
[1, 2]
26+
);
27+
}
28+
29+
module.exports.test = test;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const assert = require('assert');
2+
const cutAwardBadges = require('../LeetcodeProblems/Award_Budget_Cuts').cutAwardBadges;
3+
4+
function test() {
5+
assert.deepEqual(
6+
cutAwardBadges([2, 100, 50, 120, 1000], 190),
7+
[ 47, 47, 47, 47, 2 ]
8+
);
9+
assert.deepEqual(
10+
cutAwardBadges([2, 100, 50, 120, 1000], 5),
11+
[ 1, 1, 1, 1, 1 ]
12+
);
13+
}
14+
15+
module.exports.test = test;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const assert = require('assert');
2+
const backspaceCompare = require('../LeetcodeProblems/Backspace_String_Compare').backspaceCompare;
3+
const backspaceCompare2 = require('../LeetcodeProblems/Backspace_String_Compare').backspaceCompare2;
4+
5+
function test() {
6+
assert.equal(backspaceCompare("ab#c", "ad#c"), true); // true
7+
assert.equal(backspaceCompare("ab##", "c#d#"), true); // true
8+
assert.equal(backspaceCompare("a##c", "#a#c"), true); // true
9+
assert.equal(backspaceCompare("a#c", "b"), false); // false
10+
11+
assert.equal(backspaceCompare2("ab#c", "ad#c"), true); // true
12+
assert.equal(backspaceCompare2("ab##", "c#d#"), true); // true
13+
assert.equal(backspaceCompare2("a##c", "#a#c"), true); // true
14+
assert.equal(backspaceCompare2("a#c", "b"), false); // false
15+
}
16+
test();
17+
18+
module.exports.test = test;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
3+
Best Time to Buy and Sell Stock II
4+
5+
Say you have an array for which the ith element is the price of a given stock on day i.
6+
7+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
8+
9+
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
10+
11+
Example 1:
12+
13+
Input: [7,1,5,3,6,4]
14+
Output: 7
15+
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
16+
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
17+
Example 2:
18+
19+
Input: [1,2,3,4,5]
20+
Output: 4
21+
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
22+
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
23+
engaging multiple transactions at the same time. You must sell before buying again.
24+
Example 3:
25+
26+
Input: [7,6,4,3,1]
27+
Output: 0
28+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
29+
*/
30+
const assert = require('assert');
31+
32+
/**
33+
* @param {number[]} prices
34+
* @return {number}
35+
*/
36+
var maxProfit = function(prices) {
37+
var profit = 0;
38+
var iter = 0;
39+
40+
while(iter < prices.length) {
41+
while(iter < prices.length - 1 && prices[iter] > prices[iter + 1]) {
42+
iter++;
43+
}
44+
const buy = prices[iter];
45+
iter++;
46+
while(iter < prices.length - 1 && prices[iter] < prices[iter + 1]) {
47+
iter++;
48+
}
49+
50+
if(iter < prices.length) {
51+
const sell = prices[iter];
52+
profit = profit + sell - buy;
53+
}
54+
}
55+
56+
return profit;
57+
};
58+
59+
var main = function() {
60+
test();
61+
}
62+
63+
function test() {
64+
assert.equal(maxProfit([7,1,5,3,6,4]), 7);
65+
assert.equal(maxProfit([7,1,5,3320,6,4]), 3319);
66+
}
67+
68+
module.exports.main = main;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Binary Gap
3+
https://leetcode.com/problems/binary-gap/description/
4+
5+
Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N.
6+
7+
If there aren't two consecutive 1's, return 0.
8+
9+
Example 1:
10+
11+
Input: 22
12+
Output: 2
13+
Explanation:
14+
22 in binary is 0b10110.
15+
In the binary representation of 22, there are three ones, and two consecutive pairs of 1's.
16+
The first consecutive pair of 1's have distance 2.
17+
The second consecutive pair of 1's have distance 1.
18+
The answer is the largest of these two distances, which is 2.
19+
Example 2:
20+
21+
Input: 5
22+
Output: 2
23+
Explanation:
24+
5 in binary is 0b101.
25+
Example 3:
26+
27+
Input: 6
28+
Output: 1
29+
Explanation:
30+
6 in binary is 0b110.
31+
Example 4:
32+
33+
Input: 8
34+
Output: 0
35+
Explanation:
36+
8 in binary is 0b1000.
37+
There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.
38+
*/
39+
const assert = require('assert');
40+
41+
/**
42+
* @param {number} N
43+
* @return {number}
44+
*/
45+
var binaryGap = function(N) {
46+
var maxDist = 0;
47+
var currentDist = 0;
48+
while(N > 0) {
49+
const bit = N % 2;
50+
N >>= 1;
51+
if(bit === 1) {
52+
currentDist = 1;
53+
while(N > 0 && N % 2 === 0 ) {
54+
currentDist++;
55+
N >>= 1;
56+
}
57+
if(N !== 0 && currentDist > maxDist)
58+
maxDist = currentDist;
59+
}
60+
}
61+
return maxDist;
62+
};
63+
64+
var main = function() {
65+
test();
66+
}
67+
68+
function test() {
69+
assert.equal(binaryGap(22), 2); // 10110
70+
assert.equal(binaryGap(8), 0); // 1000
71+
}
72+
73+
module.exports.main = main;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Clone Graph
3+
https://leetcode.com/problems/clone-graph/description/
4+
5+
Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains a label (int) and a list (List[UndirectedGraphNode]) of its neighbors. There is an edge between the given node and each of the nodes in its neighbors.
6+
7+
8+
OJ's undirected graph serialization (so you can understand error output):
9+
Nodes are labeled uniquely.
10+
11+
We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
12+
13+
14+
As an example, consider the serialized graph {0,1,2#1,2#2,2}.
15+
16+
The graph has a total of three nodes, and therefore contains three parts as separated by #.
17+
18+
First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
19+
Second node is labeled as 1. Connect node 1 to node 2.
20+
Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
21+
22+
23+
Visually, the graph looks like the following:
24+
25+
1
26+
/ \
27+
/ \
28+
0 --- 2
29+
/ \
30+
\_/
31+
Note: The information about the tree serialization is only meant so that you can understand error output if you get a wrong answer.
32+
You don't need to understand the serialization to solve the problem.
33+
*/
34+
35+
/**
36+
* Definition for undirected graph.
37+
* function UndirectedGraphNode(label) {
38+
* this.label = label;
39+
* this.neighbors = []; // Array of UndirectedGraphNode
40+
* }
41+
*/
42+
43+
44+
// SOLUTION 1 Using DFS
45+
/**
46+
* @param {UndirectedGraphNode} graph
47+
* @return {UndirectedGraphNode}
48+
*/
49+
var cloneGraph = function(graph) {
50+
if(!graph)
51+
return graph;
52+
53+
return dfs(graph, {});
54+
};
55+
56+
var dfs = function(graph, visited) {
57+
if(visited[graph.label])
58+
return visited[graph.label];
59+
60+
var newNode = new UndirectedGraphNode(graph.label);
61+
visited[newNode.label] = newNode;
62+
63+
for(var i = 0; i < graph.neighbors.length; i++) {
64+
const neighbor = dfs(graph.neighbors[i], visited);
65+
newNode.neighbors.push(neighbor);
66+
}
67+
68+
return newNode;
69+
}
70+
71+
// SOLUTION 2 Using DFS
72+
var cloneGraphBFS = function(graph) {
73+
if(graph === null)
74+
return graph;
75+
76+
var visitedMap = {};
77+
var queue = [graph];
78+
var copyReturn = new UndirectedGraphNode(graph.label);
79+
visitedMap[graph.label] = copyReturn;
80+
81+
while(queue.length > 0) {
82+
var node = queue.shift();
83+
var nodeCopied = visitedMap[node.label];
84+
85+
for(var i = 0; i < node.neighbors.length; i++) {
86+
var neighbor = node.neighbors[i];
87+
88+
if(!visitedMap[neighbor.label]) {
89+
var copyNeighbor = new UndirectedGraphNode(neighbor.label);
90+
visitedMap[neighbor.label] = copyNeighbor;
91+
queue.push(neighbor);
92+
}
93+
94+
nodeCopied.neighbors.push(visitedMap[neighbor.label]);
95+
}
96+
}
97+
98+
return copyReturn;
99+
}

0 commit comments

Comments
 (0)