Skip to content

Commit e8b37ba

Browse files
committedDec 26, 2023
initial commit, excercises 238, 334 and 1071 completed
0 parents  commit e8b37ba

File tree

13 files changed

+276
-0
lines changed

13 files changed

+276
-0
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode
2+
node_modules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 345. Reverse Vowels of a String
2+
3+
Given a string `s`, reverse only all the vowels in the string and return it.
4+
5+
The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once.
6+
7+
## Example 1:
8+
9+
- Input: `s = "hello"`
10+
- Output: `"holle"`
11+
12+
## Example 2:
13+
14+
- Input: `s = "leetcode"`
15+
- Output: `"leotcede"`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const assert = require('assert')
2+
const { benchmark } = require('..//utils/index')
3+
/**
4+
* @param {string} s
5+
* @return {string}
6+
*/
7+
// This function will traverse the string from the left until a vowel appears
8+
// Then swap with the vowel at the right
9+
var reverseVowels = function(s) {
10+
let i = 0
11+
let j = s.length - 1
12+
const reverse = Array(s.length).fill()
13+
while (i <= j){
14+
if (!isVowel(s[i])) {
15+
reverse[i] = s[i]
16+
i++
17+
continue
18+
}
19+
while (j >= i) {
20+
if (!isVowel(s[j])) {
21+
reverse[j] = s[j]
22+
j--
23+
continue
24+
}
25+
26+
reverse[i] = s[j]
27+
reverse[j] = s[i]
28+
j--
29+
// break this while loop to continue from the left
30+
break
31+
}
32+
i++
33+
}
34+
35+
return reverse.join('')
36+
}
37+
38+
var reverseVowels2 = function(s) {
39+
const vowels = Array.from(s).filter(isVowel)
40+
const rev = []
41+
for (let i = 0; i < s.length; i++) {
42+
if (!isVowel(s[i])) {
43+
rev[i] = s[i]
44+
continue
45+
}
46+
rev[i] = vowels[vowels.length - 1]
47+
vowels.pop()
48+
}
49+
return rev.join('')
50+
}
51+
52+
function isVowel(a) {
53+
return ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'].includes(a)
54+
}
55+
56+
// Test `reverseVowels`
57+
assert.strictEqual(reverseVowels('hello'), 'holle')
58+
assert.strictEqual(reverseVowels('leetcode'), 'leotcede')
59+
assert.strictEqual(reverseVowels('a.'), 'a.')
60+
assert.strictEqual(reverseVowels('aA'), 'Aa')
61+
benchmark(reverseVowels, 'leetcode')
62+
63+
// Test `reverseVowels2`
64+
assert.strictEqual(reverseVowels2('hello'), 'holle')
65+
assert.strictEqual(reverseVowels2('leetcode'), 'leotcede')
66+
assert.strictEqual(reverseVowels2('a.'), 'a.')
67+
assert.strictEqual(reverseVowels2('aA'), 'Aa')
68+
benchmark(reverseVowels2, 'leetcode')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "1071-greatest-common-divisor-of-strings",
3+
"version": "1.0.0",
4+
"description": "Given a string `s`, reverse only all the vowels in the string and return it.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC"
12+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 238. Product of Array Except Self
2+
3+
## Problem Description
4+
Given an integer array `nums`, return an array `answer` such that `answer[i]` is equal to the product of all the elements of `nums` except `nums[i]`.
5+
6+
The product of any prefix or suffix of `nums` is guaranteed to fit in a 32-bit integer.
7+
8+
You must write an algorithm that runs in O(n) time and without using the division operation.
9+
10+
## Examples
11+
12+
### Example 1:
13+
Input: `nums` = [1,2,3,4]
14+
Output: [24,12,8,6]
15+
16+
### Example 2:
17+
Input: `nums` = [-1,1,0,-3,3]
18+
Output: [0,0,9,0,0]
19+
20+
## Constraints
21+
- 2 <= `nums.length` <= 105
22+
- -30 <= `nums[i]` <= 30
23+
The product of any prefix or suffix of `nums` is guaranteed to fit in a 32-bit integer.
24+
25+
## Follow up
26+
Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function(nums) {
6+
const n = nums.length;
7+
const result = new Array(n).fill(1);
8+
9+
let leftProduct = 1;
10+
let rightProduct = 1;
11+
12+
for (let i = 0; i < n; i++) {
13+
result[i] *= leftProduct;
14+
leftProduct *= nums[i];
15+
}
16+
17+
for (let i = n - 1; i >= 0; i--) {
18+
result[i] *= rightProduct;
19+
rightProduct *= nums[i];
20+
}
21+
22+
return result;
23+
};
24+
25+
productExceptSelf([1,2,3,4])
26+
// Output: [24,12,8,6]
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "238-product-array-except-self",
3+
"version": "1.0.0",
4+
"description": "## Problem Description Given an integer array `nums`, return an array `answer` such that `answer[i]` is equal to the product of all the elements of `nums` except `nums[i]`.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC"
13+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 334. Increasing Triplet Subsequence
2+
Given an integer array `nums`, return `true` if there exists a triple of indices `(i, j, k)` such
3+
that `i < j < k` and `nums[i] < nums[j] < nums[k]`. If no such indices exist, return `false`.
4+
5+
## Examples
6+
7+
### Example 1:
8+
Input: `nums` = [1,2,3,4,5]
9+
Output: `true`
10+
Explanation: Any triplet where `i < j < k` is valid.
11+
12+
### Example 2:
13+
Input: `nums` = [5,4,3,2,1]
14+
Output: `false`
15+
Explanation: No triplet exists.
16+
17+
### Example 3:
18+
Input: `nums` = [2,1,5,0,4,6]
19+
Output: `true`
20+
Explanation: The triplet `(3, 4, 5)` is valid because `nums[3] == 0 < nums[4] == 4 < nums[5] == 6`.
21+
22+
## Constraints
23+
- 1 <= `nums.length` <= 5 * 10^5
24+
- -2^31 <= `nums[i]` <= 2^31 - 1
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
*
3+
* @param {*} nums
4+
* @returns
5+
*/
6+
var increasingTriplet = function(nums) {
7+
let i = null // The first smaller number
8+
let j = null // The second smaller number
9+
10+
for (const num of nums) {
11+
// Look for the first smaller `num`
12+
if (i === null || num <= i) {
13+
// If we found it assign it to `i`
14+
i = num
15+
continue
16+
}
17+
18+
// At this point is waranteed that i exists and the current `num`
19+
// is greater than `i`
20+
if (j === null || num <= j) {
21+
j = num
22+
continue
23+
}
24+
25+
// At thhis point is waranteed that `i` and `j` exist and
26+
// num > j > i, so we had found a triplet
27+
return true
28+
}
29+
30+
return false
31+
}
32+
33+
module.exports = increasingTriplet
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "334-increasing-triplet-sequence",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const assert = require('assert')
2+
const increasingTriplet = require('./index')
3+
4+
assert.strict.equal(increasingTriplet([1,2,3,4,5]), true)
5+
assert.strict.equal(increasingTriplet([5,4,3,2,1]), false)
6+
assert.strict.equal(increasingTriplet([2,1,5,0,4,6]), true)
7+
assert.strict.equal(increasingTriplet([20,100,10,12,5,13]), true)

‎utils/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
*
3+
* @param {*} f
4+
* @param {...any} params
5+
* @returns
6+
*/
7+
function benchmark(f, ...params) {
8+
const initialCpuUsage = totalCpuUseInSeconds()
9+
f(...params)
10+
const finalCpuUsage = totalCpuUseInSeconds()
11+
const stats = {
12+
function: f.name,
13+
params: Array.from(...params).join(''),
14+
cpuUsageInSeconds: finalCpuUsage - initialCpuUsage
15+
}
16+
console.table(stats)
17+
return stats
18+
}
19+
20+
function totalCpuUseInSeconds() {
21+
return (process.cpuUsage().user + process.cpuUsage().system)/1000000
22+
}
23+
24+
module.exports = { benchmark }

‎utils/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "utils",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC"
12+
}

0 commit comments

Comments
 (0)
Please sign in to comment.