Skip to content

Commit 677f135

Browse files
Yong SuYong Su
Yong Su
authored and
Yong Su
committed
Add more FB problems.
1 parent d6a6fb0 commit 677f135

8 files changed

+342
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ A collection of JavaScript problems and solutions for studying algorithms.
5151
- [Two Sum](src/array/two-sum.js)
5252
- [Three Sum](src/array/three-sum.js)
5353
- [Median of Two Sorted Arrays](src/array/median-of-two-sorted-arrays.js)
54+
- [Largest Rectangle in Histogram](src/array/largest-rectangle-in-histogram.js)
5455
- [Plus One](src/array/plus-one.js)
5556
- [Trapping Rain Water](src/array/trapping-rain-water)
5657
- [Merge Intervals](src/array/merge-intervals.js)
@@ -94,6 +95,9 @@ A collection of JavaScript problems and solutions for studying algorithms.
9495
- [Longest Mountain in Array](src/array/longest-mountain-in-array.js)
9596
- [Continuous Subarray Sum](src/array/continuous-subarray-sum.js)
9697
- [Contiguous Array](src/array/contiguous-array.js)
98+
- [Merge Sorted Array](src/array/merge-sorted-array.js)
99+
- [Product of Array Except Self](src/array/product-of-array-except-self.js)
100+
- [Missing Number](src/array/missing-number.js)
97101

98102
### Matrix
99103

@@ -153,6 +157,8 @@ A collection of JavaScript problems and solutions for studying algorithms.
153157
- [Longest Substring with At Most Two Distinct Characters](src/string/longest-substring-with-at-most-two-distinct-characters.js)
154158
- [Longest Substring Without Repeating Characters](src/string/longest-substring-without-repeating-characters.js)
155159
- [Group Anagrams](src/string/group-anagrams.js)
160+
- [Palindromic Substrings](src/string/palindromic-substrings.js)
161+
- [Integer to English Words](src/string/integer-to-english-words.js)
156162

157163
### Sorting
158164

@@ -428,6 +434,7 @@ A collection of JavaScript problems and solutions for studying algorithms.
428434
- [Total Hamming Distance](src/math/total-hamming-distance.js)
429435
- [Nth Digit](src/math/nth-digit.js)
430436
- [Roman to Integer](src/math/roman-to-integer.js)
437+
- [Excel Sheet Column Title](src/math/excel-sheet-column-title.js)
431438

432439
### Sampling
433440

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Largest Rectangle in Histogram
3+
*
4+
* Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the
5+
* area of largest rectangle in the histogram.
6+
*
7+
* Example:
8+
*
9+
* Input: [2,1,5,6,2,3]
10+
* Output: 10
11+
*/
12+
13+
import Stack from 'common/stack';
14+
15+
/**
16+
* @param {number[]} heights
17+
* @return {number}
18+
*/
19+
const largestRectangleArea = heights => {
20+
const n = heights.length;
21+
const stack = new Stack();
22+
23+
let max = 0;
24+
25+
for (let i = 0; i <= n; i++) {
26+
// If we finished all the elements OR the current element is less than top
27+
while (!stack.isEmpty() && (i === n || heights[i] < heights[stack.peek()])) {
28+
const height = heights[stack.pop()];
29+
const width = stack.isEmpty() ? i : i - 1 - stack.peek();
30+
31+
max = Math.max(max, width * height);
32+
}
33+
34+
stack.push(i);
35+
}
36+
37+
return max;
38+
};
39+
40+
export { largestRectangleArea };

src/array/merge-sorted-array.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Merge Sorted Array
3+
*
4+
* Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
5+
*
6+
* Note:
7+
*
8+
* The number of elements initialized in nums1 and nums2 are m and n respectively.
9+
* You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from
10+
* nums2.
11+
* Example:
12+
*
13+
* Input:
14+
* nums1 = [1,2,3,0,0,0], m = 3
15+
* nums2 = [2,5,6], n = 3
16+
*
17+
* Output: [1,2,2,3,5,6]
18+
*/
19+
20+
/**
21+
* @param {number[]} nums1
22+
* @param {number} m
23+
* @param {number[]} nums2
24+
* @param {number} n
25+
* @return {void} Do not return anything, modify nums1 in-place instead.
26+
*/
27+
const merge = (nums1, m, nums2, n) => {
28+
let i = m - 1;
29+
let j = n - 1;
30+
let k = m + n - 1;
31+
32+
while (i >= 0 && j >= 0) {
33+
if (nums1[i] > nums2[j]) {
34+
nums1[k--] = nums1[i--];
35+
} else {
36+
nums1[k--] = nums2[j--];
37+
}
38+
}
39+
40+
while (j >= 0) {
41+
nums1[k--] = nums2[j--];
42+
}
43+
};
44+
45+
export { merge };

src/array/missing-number.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Missing Number
3+
*
4+
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
5+
*
6+
* Example 1:
7+
*
8+
* Input: [3,0,1]
9+
* Output: 2
10+
* Example 2:
11+
*
12+
* Input: [9,6,4,2,3,5,7,0,1]
13+
* Output: 8
14+
* Note:
15+
* Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space
16+
* complexity?
17+
*/
18+
19+
/**
20+
* @param {number[]} nums
21+
* @return {number}
22+
*/
23+
const missingNumber = nums => {
24+
let res = nums.length;
25+
26+
for (i = 0; i < nums.length; i++) {
27+
res ^= i ^ nums[i]; // a ^ b ^ b = a
28+
}
29+
30+
return res;
31+
};
32+
33+
export { missingNumber };
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Product of Array Except Self
3+
*
4+
* Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product
5+
* of all the elements of nums except nums[i].
6+
*
7+
* Example:
8+
*
9+
* Input: [1,2,3,4]
10+
* Output: [24,12,8,6]
11+
* Note: Please solve it without division and in O(n).
12+
*
13+
* Follow up:
14+
* Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of
15+
* space complexity analysis.)
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @return {number[]}
21+
*/
22+
const productExceptSelf = nums => {
23+
const n = nums.length;
24+
const res = Array(n).fill(0);
25+
26+
// Scan from left to right
27+
res[0] = 1;
28+
for (let i = 1; i < n; i++) {
29+
res[i] = res[i - 1] * nums[i - 1];
30+
}
31+
32+
// Scan from right to left
33+
let right = 1;
34+
for (let i = n - 1; i >= 0; i--) {
35+
res[i] *= right;
36+
right *= nums[i];
37+
}
38+
39+
return res;
40+
};
41+
42+
export { productExceptSelf };

src/math/excel-sheet-column-title.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Excel Sheet Column Title
3+
*
4+
* Given a positive integer, return its corresponding column title as appear in an Excel sheet.
5+
*
6+
* For example:
7+
*
8+
* 1 -> A
9+
* 2 -> B
10+
* 3 -> C
11+
* ...
12+
* 26 -> Z
13+
* 27 -> AA
14+
* 28 -> AB
15+
* ...
16+
* Example 1:
17+
*
18+
* Input: 1
19+
* Output: "A"
20+
* Example 2:
21+
*
22+
* Input: 28
23+
* Output: "AB"
24+
* Example 3:
25+
*
26+
* Input: 701
27+
* Output: "ZY"
28+
*/
29+
30+
/**
31+
* @param {number} n
32+
* @return {string}
33+
*/
34+
const convertToTitle = n => {
35+
let title = '';
36+
37+
while (n > 0) {
38+
n--;
39+
title = String.fromCharCode(65 + (n % 26)) + title;
40+
n = Math.floor(n / 26);
41+
}
42+
43+
return title;
44+
};
45+
46+
export { convertToTitle };
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Integer to English Words
3+
*
4+
* Convert a non-negative integer to its english words representation.
5+
* Given input is guaranteed to be less than 231 - 1.
6+
*
7+
* Example 1:
8+
*
9+
* Input: 123
10+
* Output: "One Hundred Twenty Three"
11+
* Example 2:
12+
*
13+
* Input: 12345
14+
* Output: "Twelve Thousand Three Hundred Forty Five"
15+
* Example 3:
16+
*
17+
* Input: 1234567
18+
* Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
19+
* Example 4:
20+
*
21+
* Input: 1234567891
22+
* Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
23+
*/
24+
25+
const LESS_THAN_20 = [
26+
'',
27+
'One',
28+
'Two',
29+
'Three',
30+
'Four',
31+
'Five',
32+
'Six',
33+
'Seven',
34+
'Eight',
35+
'Nine',
36+
'Ten',
37+
'Eleven',
38+
'Twelve',
39+
'Thirteen',
40+
'Fourteen',
41+
'Fifteen',
42+
'Sixteen',
43+
'Seventeen',
44+
'Eighteen',
45+
'Nineteen',
46+
];
47+
const TENS = ['', 'Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
48+
const THOUSANDS = ['', 'Thousand', 'Million', 'Billion'];
49+
50+
/**
51+
* @param {number} num
52+
* @return {string}
53+
*/
54+
const numberToWords = num => {
55+
if (num === 0) return 'Zero';
56+
57+
let i = 0;
58+
let words = '';
59+
60+
while (num > 0) {
61+
if (num % 1000 != 0) words = helper(num % 1000) + THOUSANDS[i] + ' ' + words;
62+
num = Math.floor(num / 1000);
63+
i++;
64+
}
65+
66+
return words.trim();
67+
};
68+
69+
const helper = num => {
70+
if (num == 0) return '';
71+
else if (num < 20) return LESS_THAN_20[num] + ' ';
72+
else if (num < 100) return TENS[Math.floor(num / 10)] + ' ' + helper(num % 10);
73+
else return LESS_THAN_20[Math.floor(num / 100)] + ' Hundred ' + helper(num % 100);
74+
};
75+
76+
export { numberToWords };

src/string/palindromic-substrings.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Palindromic Substrings
3+
*
4+
* Given a string, your task is to count how many palindromic substrings in this string.
5+
*
6+
* The substrings with different start indexes or end indexes are counted as different substrings even they consist of
7+
* same characters.
8+
*
9+
* Example 1:
10+
*
11+
* Input: "abc"
12+
* Output: 3
13+
* Explanation: Three palindromic strings: "a", "b", "c".
14+
*
15+
* Example 2:
16+
*
17+
* Input: "aaa"
18+
* Output: 6
19+
* Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
20+
*
21+
* Note:
22+
* The input string length won't exceed 1000.
23+
*/
24+
25+
/**
26+
* @param {string} s
27+
* @return {number}
28+
*/
29+
const countSubstrings = s => {
30+
const extendPalindrome = (s, left, right) => {
31+
while (left >= 0 && right < s.length && s[left] === s[right]) {
32+
count++;
33+
left--;
34+
right++;
35+
}
36+
};
37+
38+
if (!s || s.length === 0) {
39+
return 0;
40+
}
41+
42+
let count = 0;
43+
44+
for (let i = 0; i < s.length; i++) {
45+
// i is the mid point
46+
extendPalindrome(s, i, i); // odd length;
47+
extendPalindrome(s, i, i + 1); // even length
48+
}
49+
50+
return count;
51+
};
52+
53+
export { countSubstrings };

0 commit comments

Comments
 (0)