Skip to content

Improve main #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 7, 2018
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tips
21 changes: 17 additions & 4 deletions LeetcodeProblems/Add_Two_Numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Add Two Numbers
https://leetcode.com/problems/add-two-numbers/

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order
and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Expand All @@ -20,16 +21,19 @@ Explanation: 342 + 465 = 807.
* this.next = null;
* }
*/

var ListNode = require('../utilsClasses/ListNode').ListNode;

/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/

var addTwoNumbers = function(l1, l2) {
if(l1 === null) {
if(l1 === null)
return (l2 === null) ? new ListNode(0) : l2;
} else if(l2 === null)
else if(l2 === null)
return l1;

var elem = l1.val + l2.val;
Expand Down Expand Up @@ -62,3 +66,12 @@ var addTwoNumbers = function(l1, l2) {
}
return head;
};


var main = function() {
const list1 = ListNode.linkenList([1,2,3,4]);
const list2 = ListNode.linkenList([1,2,3,4]);
console.log(addTwoNumbers(list1, list2));
}

main();
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Binary Gap
https://leetcode.com/problems/binary-gap/description/

Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N.
Expand Down
63 changes: 8 additions & 55 deletions LeetcodeProblems/CoinChange.js → LeetcodeProblems/Coin_Change.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,20 @@
/*
Coin Change
https://leetcode.com/problems/coin-change/

https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/description/

862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.

If there is no non-empty subarray with sum at least K, return -1.


You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:

Input: A = [1], K = 1
Output: 1
Input: coins = [1, 2, 5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:

Input: A = [1,2], K = 4
Input: coins = [2], amount = 3
Output: -1
Example 3:

Input: A = [2,-1,2], K = 3
Output: 3


Note:

1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
*/


/*
Tree for solution


[pos, countOfNumbers, K]
[pos + 1, countOfNumbers, K] [pos + 1, countOfNumbers + 1, K - nums[pos]] [pos, countOfNumbers + 1, K - nums[pos]]
...

[0, 0, 11] // [pos, countOfNumbers, K]
/ | \
[1, 0, 11] [1, 1, 10] [0, 1, 10]
/ | \ / | \ / | \
[2, 0, 11] [2, 1, 9] [1, 1, 9] [2, 0, 10] [2, 2, 8] [1, 2, 8] [1, 1, 10] [1, 2, 9] [0, 2, 9]
...
You may assume that you have an infinite number of each kind of coin.
*/

// Solution 3
Expand Down Expand Up @@ -132,24 +102,7 @@ var min = function(a, b, c) {
return (b < c) ? b : c;
}


function main() {
// console.log("-------------");
// console.log("Approach 1")
// console.log(coinChange1([], 3));
// console.log(coinChange1([2], 3));
// console.log(coinChange1([1, 2, 5], 11));
// console.log(coinChange1([3,7,405,436], 8839));
// // console.log(coinChange1([370,417,408,156,143,434,168,83,177,280,117], 9953)); takes forever

// console.log("-------------");
// console.log("Approach 2")
// console.log(coinChange2([], 3));
// console.log(coinChange2([2], 3));
// console.log(coinChange2([1, 2, 5], 11));
// console.log(coinChange2([3,7,405,436], 8839));
// console.log(coinChange2([370,417,408,156,143,434,168,83,177,280,117], 9953));

console.log("-------------");
console.log("Approach 3")
console.log(coinChange3([], 3));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Design Circular Deque
https://leetcode.com/problems/design-circular-deque/description/

Design your implementation of the circular double-ended queue (deque).
Expand Down Expand Up @@ -145,5 +146,5 @@ var main = function(){
console.log(obj.insertFront(4));
console.log(obj.getFront());
}
main();

module.exports.main = main;
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/*
Escape The Ghosts
https://leetcode.com/problems/escape-the-ghosts/description/

789. Escape The Ghosts

You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (target[0], target[1]). There are several ghosts on the map, the i-th ghost starts at (ghosts[i][0], ghosts[i][1]).

Each turn, you and all ghosts simultaneously *may* move in one of 4 cardinal directions: north, east, west, or south, going from the previous point to a new point 1 unit of distance away.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Flood Fill
https://leetcode.com/problems/flood-fill/description/

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Generate Parentheses
https://leetcode.com/problems/generate-parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Expand Down Expand Up @@ -31,7 +32,6 @@ var generateParenthesesApproach1 = function(n) {
@param {string} leftParenthesis Amount for parenthesis left to be added.
@param {[string]} sol array that contains the solution found so far.
*/

var genParAux = function(str, position, leftParentheses, sol) {
if(position === str.length) {
var ret = str + ")".repeat(leftParentheses);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Group Anagrams
https://leetcode.com/problems/group-anagrams/description/

Given an array of strings, group anagrams together.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
Linked List Cycle
https://leetcode.com/problems/linked-list-cycle-ii/description/

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Expand All @@ -9,6 +10,7 @@ Follow up:
Can you solve it without using extra space?
*/

var ListNode = require('../utilsClasses/ListNode').ListNode;

// Optimal solution
/**
Expand Down Expand Up @@ -59,15 +61,9 @@ var main = function() {
const head = buildCycle();
console.log(detectCycle(head));
}
main();

function ListNode(val) {
this.val = val;
this.next = null;
}

function buildCycle() {
var node1 = new ListNode(1);
var node1 = ListNode.linkenList([1,2,3,4,5]);
var node2 = new ListNode(2);
var node3 = new ListNode(3);
var node4 = new ListNode(4);
Expand All @@ -78,13 +74,7 @@ function buildCycle() {
node3.next = node4;
node4.next = node5;
node5.next = node2;

/* 1 -> 2 -> 3 -> 4 -> 5
\ /
- - - - -
*/
return node1;
}

main();
module.exports.main = main;
2 changes: 0 additions & 2 deletions LeetcodeProblems/Longest_Consecutive_Sequence.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Longest Consecutive Sequence

https://leetcode.com/problems/longest-consecutive-sequence/

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Expand Down Expand Up @@ -53,7 +52,6 @@ var longestConsecutive = function(nums) {
return cons;
};


var main = function() {
console.log(longestConsecutive([100, 1, 200, 3, 2, 400, 201]));
console.log(longestConsecutive([1,2,3,4, 100, 1, 200, 3, 2, 400, 201]));
Expand Down
73 changes: 35 additions & 38 deletions LeetcodeProblems/Longest_Palindromic_Substring.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/*
https://leetcode.com/problems/longest-palindromic-substring/description/

Longest Palindromic Substring
https://leetcode.com/problems/longest-palindromic-substring/description/

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Expand All @@ -16,7 +15,6 @@ Input: "cbbd"
Output: "bb"
*/


/**
* @param {string} s
* @return {string}
Expand All @@ -25,46 +23,45 @@ var longestPalindrome = function(str) {
if(str.length == 0)
return "";

var maxPal = 1;
var maxPal = 1;
var posPalStart = 0;
var currentPalStart = 0;
var currentPalStart = 0;

for(var i = 1; i < str.length; i++) {
if(str.charAt(i - 1) == str.charAt(i)) {
currentPalStart = i - 1;
var currentPal = 2;
var iter = 1;
while(i - iter - 1 >= 0 && i + iter < str.length &&
str.charAt(i - iter - 1) == str.charAt(i + iter)) {
currentPalStart = i - iter - 1;
iter++;
currentPal += 2;
}
}
if(currentPal > maxPal) {
maxPal = currentPal;
posPalStart = currentPalStart;
}
if(str.charAt(i - 1) == str.charAt(i)) {
currentPalStart = i - 1;
var currentPal = 2;
var iter = 1;
while(i - iter - 1 >= 0 && i + iter < str.length &&
str.charAt(i - iter - 1) == str.charAt(i + iter)) {
currentPalStart = i - iter - 1;
iter++;
currentPal += 2;
}
}
if(currentPal > maxPal) {
maxPal = currentPal;
posPalStart = currentPalStart;
}
}

for(var i = 1; i < str.length - 1; i++) {
if(str.charAt(i - 1) == str.charAt(i + 1)) {
currentPal = 1;
var iter = 1;
while(i - iter >= 0 && i + iter < str.length && str.charAt(i - iter) == str.charAt(i + iter)) {
currentPalStart = i - iter;
iter++;
currentPal += 2;
}
}

for(var i = 1; i < str.length - 1; i++) {
if(str.charAt(i - 1) == str.charAt(i + 1)) {
currentPal = 1;
var iter = 1;
while(i - iter >= 0 && i + iter < str.length &&
str.charAt(i - iter) == str.charAt(i + iter)) {
currentPalStart = i - iter;
iter++;
currentPal += 2;
}
}
if(currentPal > maxPal) {
maxPal = currentPal;
posPalStart = currentPalStart;
}
if(currentPal > maxPal) {
maxPal = currentPal;
posPalStart = currentPalStart;
}

return str.slice(posPalStart, posPalStart + maxPal);
}

return str.slice(posPalStart, posPalStart + maxPal);
}

var main = function() {
Expand Down
Loading