Skip to content

Added problems #47

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 8 commits into from
Sep 5, 2022
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
tips
.DS_Store

node_modules/
node_modules/

JAVASCRIPT.md
63 changes: 63 additions & 0 deletions LeetcodeProblems/Algorithms/3SumClosest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
3Sum Closest
https://leetcode.com/problems/3sum-closest/

Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.

Return the sum of the three integers.

You may assume that each input would have exactly one solution.

Example 1:

Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Example 2:

Input: nums = [0,0,0], target = 1
Output: 0


Constraints:

3 <= nums.length <= 1000
-1000 <= nums[i] <= 1000
-104 <= target <= 104
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var threeSumClosest = function(nums, target) {
let mid = 1;
let right = nums.length - 1;
let currentSum = nums[0] + nums[mid] + nums[right];
let closest = currentSum;

nums.sort(function(a,b) {return a - b})

for(var left = 0 ; left < nums.length - 1; left++) {
mid = left + 1;
right = nums.length - 1;

while(mid < right) {
currentSum = nums[left] + nums[mid] + nums[right];

if(Math.abs(target - currentSum) < Math.abs(target - closest)) {
closest = currentSum;
}

if(currentSum > target) {
right--;
} else {
mid++;
}
}
}

return closest;
};

module.exports.threeSumClosest = threeSumClosest;
49 changes: 49 additions & 0 deletions LeetcodeProblems/Algorithms/Container_With_Most_Water.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Container With Most Water
https://leetcode.com/problems/container-with-most-water/

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example 2:

Input: height = [1,1]
Output: 1
*/

/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
let left = 0;
let right = height.length - 1;
let maxArea = calculateArea(left, right, height);

while(left < right) {
if(height[left] < height[right]) {
left++
} else {
right--;
}
maxArea = Math.max(maxArea, calculateArea(left, right, height))
}
return maxArea;
};

var calculateArea = function(x, y, height) {
let minHeight = height[x] > height[y] ? height[y] : height[x];
let width = y -x;
return (width * minHeight);
}

module.exports.maxArea = maxArea;
83 changes: 83 additions & 0 deletions LeetcodeProblems/Algorithms/Find_Anagrams.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Find All Anagrams in a String
https://leetcode.com/problems/find-all-anagrams-in-a-string/

Given two strings s and p, return an array of all the start indices of p's anagrams in s.
You may return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
ypically using all the original letters exactly once.

Example 1:

Input: s = "cbaebabacd", p = "abc"
Output: [0,6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:

Input: s = "abab", p = "ab"
Output: [0,1,2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".
*/

/**
* @param {string} s
* @param {string} p
* @return {number[]}
*/
var findAnagrams = function(s, p) {
if(s.length < p.length) { return [] }

let start = 0;
let end = p.length - 1;
let hashBuild = {};
let countLeft = p.length;
let anagrams = []

for(let e = 0; e < p.length; e++) {
hashBuild[p[e]] = hashBuild[p[e]] !== undefined ? hashBuild[p[e]] + 1 : 1;
}

for(let i = start; i < end; i++) {
if(hashBuild[s[i]] !== undefined) {
hashBuild[s[i]] = hashBuild[s[i]] - 1;
if(hashBuild[s[i]] >= 0) {
countLeft--;
}
}
}

while(end < s.length) {
// check left
if(hashBuild[s[end]] !== undefined) {
hashBuild[s[end]] = hashBuild[s[end]] - 1;
if(hashBuild[s[end]] >= 0) {
countLeft--;
}
if(countLeft == 0) {
anagrams.push(start);
}
}

// check right
if(hashBuild[s[start]] !== undefined) {
hashBuild[s[start]] = hashBuild[s[start]] + 1;
if(hashBuild[s[start]] >= 1) {
countLeft++;
}
}

// slide window
end++;
start++;
}

return anagrams;
};

module.exports.findAnagrams = findAnagrams;
65 changes: 65 additions & 0 deletions LeetcodeProblems/Algorithms/Longest_Substring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters

Given a string s, find the length of the longest substring without repeating characters.

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.


Constraints:

0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.
*/

/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
if(s.length == 0) { return 0 }

var repeatedChars = new Set();
var maxLength = 1;
var currentMaxLength = 1;
var start = 0;
var end = 0;
repeatedChars.add(s.charAt(start));

while(end + 1 < s.length && start < s.length) {
if(repeatedChars.has(s.charAt(end + 1))) {
if(repeatedChars.has(s.charAt(start))) {
currentMaxLength--;
repeatedChars.delete(s.charAt(start))
}
start++;
} else {
repeatedChars.add(s.charAt(end + 1));
currentMaxLength++;
if(currentMaxLength > maxLength) {
maxLength = currentMaxLength;
}
end++;
}
}

return maxLength;
};

module.exports.lengthOfLongestSubstring = lengthOfLongestSubstring;
44 changes: 44 additions & 0 deletions LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Max Consecutive Ones III
https://leetcode.com/problems/max-consecutive-ones-iii

Given a binary array nums and an integer k, return the maximum number of consecutive 1's
in the array if you can flip at most k 0's.

Example 1:

Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output: 6
Explanation: [1,1,1,0,0,1,1,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Example 2:

Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output: 10
Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
*/

var longestOnes = function(nums, k) {
let start = 0;
let end = 0;
let maxWindow = 0;
while(start < nums.length && end < nums.length) {
if(k > 0 || nums[end] == 1) {
if(nums[end] == 0) { k--; }
maxWindow = Math.max(maxWindow, end - start + 1);
end++;
} else { // k = 0 and nums[end] == 0
while(k == 0 && start < nums.length) {
if(nums[start] == 0) {
k++;
}
start++;
}
}
}

return maxWindow;
};

module.exports.longestOnes = longestOnes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Minimum Add to Make Parentheses Valid
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/

A parentheses string is valid if and only if:

It is the empty string,
It can be written as AB (A concatenated with B), where A and B are valid strings, or
It can be written as (A), where A is a valid string.
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.

For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".
Return the minimum number of moves required to make s valid.

Example 1:

Input: s = "())"
Output: 1
Example 2:

Input: s = "((("
Output: 3

Constraints:

1 <= s.length <= 1000
s[i] is either '(' or ')'.
*/


var minAddToMakeValid = function(s) {
var opening = 0;
var extraParClosing = 0;

for(let i = 0; i < s.length; i++) {
if(s.charAt(i) == "(") {
opening++;
} else if(s.charAt(i) == ")") {
if(opening == 0) {
extraParClosing++;
} else {
opening--;;
}
}
}
return extraParClosing + opening;
};

// Solution 2 using a queue
var minAddToMakeValidUsingQueue = function(s) {
var queue = [];
var extraParClosing = 0;

for(let i = 0; i < s.length; i++) {
if(s.charAt(i) == "(") {
queue.push(s.charAt(i))
} else if(s.charAt(i) == ")") {
if(queue.length > 0) {
queue.pop();
} else {
extraParClosing++;
}
}
}

return extraParClosing + queue.length;
};

module.exports.minAddToMakeValid = minAddToMakeValid;
Loading