Skip to content

Commit 625070a

Browse files
Merge pull request #47 from ignacio-chiazzo/main
Added problems
2 parents 062fc88 + 5ab2faf commit 625070a

19 files changed

+616
-3
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
tips
22
.DS_Store
33

4-
node_modules/
4+
node_modules/
5+
6+
JAVASCRIPT.md
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
3Sum Closest
3+
https://leetcode.com/problems/3sum-closest/
4+
5+
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.
6+
7+
Return the sum of the three integers.
8+
9+
You may assume that each input would have exactly one solution.
10+
11+
Example 1:
12+
13+
Input: nums = [-1,2,1,-4], target = 1
14+
Output: 2
15+
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
16+
Example 2:
17+
18+
Input: nums = [0,0,0], target = 1
19+
Output: 0
20+
21+
22+
Constraints:
23+
24+
3 <= nums.length <= 1000
25+
-1000 <= nums[i] <= 1000
26+
-104 <= target <= 104
27+
*/
28+
/**
29+
* @param {number[]} nums
30+
* @param {number} target
31+
* @return {number}
32+
*/
33+
var threeSumClosest = function(nums, target) {
34+
let mid = 1;
35+
let right = nums.length - 1;
36+
let currentSum = nums[0] + nums[mid] + nums[right];
37+
let closest = currentSum;
38+
39+
nums.sort(function(a,b) {return a - b})
40+
41+
for(var left = 0 ; left < nums.length - 1; left++) {
42+
mid = left + 1;
43+
right = nums.length - 1;
44+
45+
while(mid < right) {
46+
currentSum = nums[left] + nums[mid] + nums[right];
47+
48+
if(Math.abs(target - currentSum) < Math.abs(target - closest)) {
49+
closest = currentSum;
50+
}
51+
52+
if(currentSum > target) {
53+
right--;
54+
} else {
55+
mid++;
56+
}
57+
}
58+
}
59+
60+
return closest;
61+
};
62+
63+
module.exports.threeSumClosest = threeSumClosest;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Container With Most Water
3+
https://leetcode.com/problems/container-with-most-water/
4+
5+
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]).
6+
7+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
8+
9+
Return the maximum amount of water a container can store.
10+
11+
Notice that you may not slant the container.
12+
13+
Example 1:
14+
Input: height = [1,8,6,2,5,4,8,3,7]
15+
Output: 49
16+
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.
17+
Example 2:
18+
19+
Input: height = [1,1]
20+
Output: 1
21+
*/
22+
23+
/**
24+
* @param {number[]} height
25+
* @return {number}
26+
*/
27+
var maxArea = function(height) {
28+
let left = 0;
29+
let right = height.length - 1;
30+
let maxArea = calculateArea(left, right, height);
31+
32+
while(left < right) {
33+
if(height[left] < height[right]) {
34+
left++
35+
} else {
36+
right--;
37+
}
38+
maxArea = Math.max(maxArea, calculateArea(left, right, height))
39+
}
40+
return maxArea;
41+
};
42+
43+
var calculateArea = function(x, y, height) {
44+
let minHeight = height[x] > height[y] ? height[y] : height[x];
45+
let width = y -x;
46+
return (width * minHeight);
47+
}
48+
49+
module.exports.maxArea = maxArea;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Find All Anagrams in a String
3+
https://leetcode.com/problems/find-all-anagrams-in-a-string/
4+
5+
Given two strings s and p, return an array of all the start indices of p's anagrams in s.
6+
You may return the answer in any order.
7+
8+
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
9+
ypically using all the original letters exactly once.
10+
11+
Example 1:
12+
13+
Input: s = "cbaebabacd", p = "abc"
14+
Output: [0,6]
15+
Explanation:
16+
The substring with start index = 0 is "cba", which is an anagram of "abc".
17+
The substring with start index = 6 is "bac", which is an anagram of "abc".
18+
Example 2:
19+
20+
Input: s = "abab", p = "ab"
21+
Output: [0,1,2]
22+
Explanation:
23+
The substring with start index = 0 is "ab", which is an anagram of "ab".
24+
The substring with start index = 1 is "ba", which is an anagram of "ab".
25+
The substring with start index = 2 is "ab", which is an anagram of "ab".
26+
*/
27+
28+
/**
29+
* @param {string} s
30+
* @param {string} p
31+
* @return {number[]}
32+
*/
33+
var findAnagrams = function(s, p) {
34+
if(s.length < p.length) { return [] }
35+
36+
let start = 0;
37+
let end = p.length - 1;
38+
let hashBuild = {};
39+
let countLeft = p.length;
40+
let anagrams = []
41+
42+
for(let e = 0; e < p.length; e++) {
43+
hashBuild[p[e]] = hashBuild[p[e]] !== undefined ? hashBuild[p[e]] + 1 : 1;
44+
}
45+
46+
for(let i = start; i < end; i++) {
47+
if(hashBuild[s[i]] !== undefined) {
48+
hashBuild[s[i]] = hashBuild[s[i]] - 1;
49+
if(hashBuild[s[i]] >= 0) {
50+
countLeft--;
51+
}
52+
}
53+
}
54+
55+
while(end < s.length) {
56+
// check left
57+
if(hashBuild[s[end]] !== undefined) {
58+
hashBuild[s[end]] = hashBuild[s[end]] - 1;
59+
if(hashBuild[s[end]] >= 0) {
60+
countLeft--;
61+
}
62+
if(countLeft == 0) {
63+
anagrams.push(start);
64+
}
65+
}
66+
67+
// check right
68+
if(hashBuild[s[start]] !== undefined) {
69+
hashBuild[s[start]] = hashBuild[s[start]] + 1;
70+
if(hashBuild[s[start]] >= 1) {
71+
countLeft++;
72+
}
73+
}
74+
75+
// slide window
76+
end++;
77+
start++;
78+
}
79+
80+
return anagrams;
81+
};
82+
83+
module.exports.findAnagrams = findAnagrams;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Longest Substring Without Repeating Characters
3+
https://leetcode.com/problems/longest-substring-without-repeating-characters
4+
5+
Given a string s, find the length of the longest substring without repeating characters.
6+
7+
Example 1:
8+
9+
Input: s = "abcabcbb"
10+
Output: 3
11+
Explanation: The answer is "abc", with the length of 3.
12+
Example 2:
13+
14+
Input: s = "bbbbb"
15+
Output: 1
16+
Explanation: The answer is "b", with the length of 1.
17+
Example 3:
18+
19+
Input: s = "pwwkew"
20+
Output: 3
21+
Explanation: The answer is "wke", with the length of 3.
22+
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
23+
24+
25+
Constraints:
26+
27+
0 <= s.length <= 5 * 104
28+
s consists of English letters, digits, symbols and spaces.
29+
*/
30+
31+
/**
32+
* @param {string} s
33+
* @return {number}
34+
*/
35+
var lengthOfLongestSubstring = function(s) {
36+
if(s.length == 0) { return 0 }
37+
38+
var repeatedChars = new Set();
39+
var maxLength = 1;
40+
var currentMaxLength = 1;
41+
var start = 0;
42+
var end = 0;
43+
repeatedChars.add(s.charAt(start));
44+
45+
while(end + 1 < s.length && start < s.length) {
46+
if(repeatedChars.has(s.charAt(end + 1))) {
47+
if(repeatedChars.has(s.charAt(start))) {
48+
currentMaxLength--;
49+
repeatedChars.delete(s.charAt(start))
50+
}
51+
start++;
52+
} else {
53+
repeatedChars.add(s.charAt(end + 1));
54+
currentMaxLength++;
55+
if(currentMaxLength > maxLength) {
56+
maxLength = currentMaxLength;
57+
}
58+
end++;
59+
}
60+
}
61+
62+
return maxLength;
63+
};
64+
65+
module.exports.lengthOfLongestSubstring = lengthOfLongestSubstring;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Max Consecutive Ones III
3+
https://leetcode.com/problems/max-consecutive-ones-iii
4+
5+
Given a binary array nums and an integer k, return the maximum number of consecutive 1's
6+
in the array if you can flip at most k 0's.
7+
8+
Example 1:
9+
10+
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
11+
Output: 6
12+
Explanation: [1,1,1,0,0,1,1,1,1,1,1]
13+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
14+
Example 2:
15+
16+
Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
17+
Output: 10
18+
Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
19+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
20+
*/
21+
22+
var longestOnes = function(nums, k) {
23+
let start = 0;
24+
let end = 0;
25+
let maxWindow = 0;
26+
while(start < nums.length && end < nums.length) {
27+
if(k > 0 || nums[end] == 1) {
28+
if(nums[end] == 0) { k--; }
29+
maxWindow = Math.max(maxWindow, end - start + 1);
30+
end++;
31+
} else { // k = 0 and nums[end] == 0
32+
while(k == 0 && start < nums.length) {
33+
if(nums[start] == 0) {
34+
k++;
35+
}
36+
start++;
37+
}
38+
}
39+
}
40+
41+
return maxWindow;
42+
};
43+
44+
module.exports.longestOnes = longestOnes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Minimum Add to Make Parentheses Valid
3+
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/
4+
5+
A parentheses string is valid if and only if:
6+
7+
It is the empty string,
8+
It can be written as AB (A concatenated with B), where A and B are valid strings, or
9+
It can be written as (A), where A is a valid string.
10+
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
11+
12+
For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".
13+
Return the minimum number of moves required to make s valid.
14+
15+
Example 1:
16+
17+
Input: s = "())"
18+
Output: 1
19+
Example 2:
20+
21+
Input: s = "((("
22+
Output: 3
23+
24+
Constraints:
25+
26+
1 <= s.length <= 1000
27+
s[i] is either '(' or ')'.
28+
*/
29+
30+
31+
var minAddToMakeValid = function(s) {
32+
var opening = 0;
33+
var extraParClosing = 0;
34+
35+
for(let i = 0; i < s.length; i++) {
36+
if(s.charAt(i) == "(") {
37+
opening++;
38+
} else if(s.charAt(i) == ")") {
39+
if(opening == 0) {
40+
extraParClosing++;
41+
} else {
42+
opening--;;
43+
}
44+
}
45+
}
46+
return extraParClosing + opening;
47+
};
48+
49+
// Solution 2 using a queue
50+
var minAddToMakeValidUsingQueue = function(s) {
51+
var queue = [];
52+
var extraParClosing = 0;
53+
54+
for(let i = 0; i < s.length; i++) {
55+
if(s.charAt(i) == "(") {
56+
queue.push(s.charAt(i))
57+
} else if(s.charAt(i) == ")") {
58+
if(queue.length > 0) {
59+
queue.pop();
60+
} else {
61+
extraParClosing++;
62+
}
63+
}
64+
}
65+
66+
return extraParClosing + queue.length;
67+
};
68+
69+
module.exports.minAddToMakeValid = minAddToMakeValid;

0 commit comments

Comments
 (0)