Skip to content

Commit 49438a3

Browse files
Move tests to its own files Round 1
1 parent cd71920 commit 49438a3

8 files changed

+22
-748
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,9 @@
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-
*/
301
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-
}
2+
const maxProfit = require('../LeetcodeProblems/Best_Time_To_Buy_And_Sell_Stock_II').maxProfit;
623

634
function test() {
645
assert.equal(maxProfit([7,1,5,3,6,4]), 7);
656
assert.equal(maxProfit([7,1,5,3320,6,4]), 3319);
667
}
678

68-
module.exports.main = main;
9+
module.exports.test = test;
+2-66
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,9 @@
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-
*/
391
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-
}
2+
const binaryGap = require('../LeetcodeProblems/Binary_Gap').binaryGap;
673

684
function test() {
695
assert.equal(binaryGap(22), 2); // 10110
706
assert.equal(binaryGap(8), 0); // 1000
717
}
728

73-
module.exports.main = main;
9+
module.exports.test = test;
+3-97
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,5 @@
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;
1+
var test = function() {
2+
// TODO
693
}
704

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-
}
5+
module.exports.test = test;
+2-107
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,5 @@
1-
/*
2-
Coin Change
3-
https://leetcode.com/problems/coin-change/
4-
5-
You are given coins of different denominations and a total amount of money amount.
6-
Write a function to compute the fewest number of coins that you need to make up that amount.
7-
If that amount of money cannot be made up by any combination of the coins, return -1.
8-
9-
Example 1:
10-
11-
Input: coins = [1, 2, 5], amount = 11
12-
Output: 3
13-
Explanation: 11 = 5 + 5 + 1
14-
Example 2:
15-
16-
Input: coins = [2], amount = 3
17-
Output: -1
18-
Note:
19-
You may assume that you have an infinite number of each kind of coin.
20-
*/
211
const assert = require('assert');
22-
23-
// Solution 3
24-
var coinChange = function(coins, amount) {
25-
var memo = [];
26-
for(var i = 0; i <= amount; i++)
27-
memo[i] = Number.POSITIVE_INFINITY;
28-
29-
memo[0] = 0;
30-
for(var i = 0; i < coins.length; i++) {
31-
const coin = coins[i];
32-
for(var j = coin; j < memo.length; j++)
33-
memo[j] = min2(memo[j], memo[j - coin] + 1);
34-
}
35-
36-
return (memo[amount] == Number.POSITIVE_INFINITY) ? -1 : memo[amount];
37-
};
38-
39-
var min2 = function(a, b) {
40-
return (a < b) ? a : b;
41-
}
42-
43-
// Solution 2
44-
var buildMemoKey = function(position, amount) {
45-
return position + "-" + amount;
46-
}
47-
48-
var coinChange2 = function(coins, amount) {
49-
var memo = {};
50-
var solution = coinChangeAux2(coins, amount, 0, memo, Number.POSITIVE_INFINITY);
51-
return solution == Number.POSITIVE_INFINITY ? -1 : solution;
52-
};
53-
54-
var coinChangeAux2 = function(coins, amount, pos, memo) {
55-
var key = buildMemoKey(pos, amount);
56-
if(memo[key])
57-
return memo[key];
58-
59-
if(amount < 0) {
60-
return Number.POSITIVE_INFINITY;
61-
} else if(amount == 0) {
62-
return 0;
63-
} else if(pos >= coins.length) {
64-
return Number.POSITIVE_INFINITY;
65-
}
66-
67-
var left = coinChangeAux2(coins, amount, pos + 1, memo);
68-
var middle = 1 + coinChangeAux2(coins, amount - coins[pos], pos + 1, memo);
69-
var right = 1 + coinChangeAux2(coins, amount - coins[pos], pos, memo);
70-
71-
var solution = min(left, middle, right);
72-
memo[key] = solution;
73-
return solution;
74-
}
75-
76-
// Solution 1 naive
77-
var coinChange1 = function(coins, amount) {
78-
var solution = coinChangeAux1(coins, amount, 0);
79-
return solution == Number.POSITIVE_INFINITY ? -1 : solution;
80-
};
81-
82-
var coinChangeAux1 = function(coins, amount, pos) {
83-
if(amount < 0) {
84-
return Number.POSITIVE_INFINITY;
85-
} else if(amount == 0) {
86-
return 0;
87-
} else if(pos >= coins.length) {
88-
return Number.POSITIVE_INFINITY;
89-
}
90-
91-
var left = coinChangeAux1(coins, amount, pos + 1);
92-
var middle = 1 + coinChangeAux1(coins, amount - coins[pos], pos + 1);
93-
var right = 1 + coinChangeAux1(coins, amount - coins[pos], pos);
94-
95-
var partialSol = min(left, middle, right);
96-
return partialSol;
97-
}
98-
99-
var min = function(a, b, c) {
100-
if(a < b)
101-
return (a < c) ? a : c;
102-
return (b < c) ? b : c;
103-
}
104-
105-
function main() {
106-
test();
107-
}
2+
const coinChange = require('../LeetcodeProblems/Coin_Change').coinChange;
1083

1094
function test() {
1105
assert.equal(coinChange([], 3), -1);
@@ -114,4 +9,4 @@ function test() {
1149
assert.equal(coinChange([370, 417, 408, 156, 143, 434, 168, 83, 177, 280, 117], 9953), 24);
11510
}
11611

117-
module.exports.main = main;
12+
module.exports.test = test;

0 commit comments

Comments
 (0)