Skip to content

Commit 47eae3d

Browse files
feat : leetcode problem no 238
1 parent 596f650 commit 47eae3d

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* BRUTE AND OPTIMAL APPROACH */
2+
function productExceptSelf(nums) {
3+
let finalResult = [];
4+
let leftResult = [];
5+
let rightResult = [];
6+
let left = 1;
7+
let i = 0;
8+
let j = nums.length;
9+
let right = 1;
10+
let n = nums.length;
11+
while (i < n && j > 0) {
12+
let a = i == 0 ? left : nums[i - 1] * left;
13+
leftResult.push(a);
14+
left = a;
15+
i++;
16+
17+
let b = j == nums.length ? right : nums[j] * right;
18+
rightResult[j - 1] = b;
19+
right = b;
20+
j--;
21+
}
22+
23+
i = 0;
24+
while (i < leftResult.length && i < rightResult.length) {
25+
finalResult[i] = leftResult[i] * rightResult[i];
26+
i++;
27+
}
28+
29+
console.log(leftResult);
30+
console.log(rightResult);
31+
console.log(finalResult);
32+
return finalResult;
33+
}
34+
35+
function productExceptSelf_second(nums) {
36+
let total = 1;
37+
let i = 0;
38+
let finalResult = [];
39+
while (i < nums.length) {
40+
total = nums[i] * total;
41+
i++;
42+
}
43+
i = 0;
44+
while (i < nums.length) {
45+
finalResult[i] = total / nums[i];
46+
i++;
47+
}
48+
console.log(total);
49+
}
50+
productExceptSelf([1, 2, 3, 4]);
51+
productExceptSelf_second([1, 2, 3, 4]);

tsconfig.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"isolatedModules": true,
1414
"noEmit": true,
1515
"jsx": "react-jsx",
16+
"allowJs": true,
1617

1718
/* Linting */
1819
"strict": true,
@@ -25,7 +26,14 @@
2526
"~/*": ["src/*"]
2627
}
2728
},
28-
"include": ["src/*", "**/*.ts", "**/*.tsx", "src/ds/linkedList.js"],
29+
"include": [
30+
"src/*",
31+
"**/*.ts",
32+
"**/*.tsx",
33+
"src/ds/linkedList.js",
34+
"src/leetcode/countSubstring.js",
35+
"src/leetcode/countSubstringjts"
36+
],
2937
"references": [{ "path": "./tsconfig.node.json" }],
3038
/* Exclude files and directories from compilation */
3139
"exclude": ["node_modules"]

0 commit comments

Comments
 (0)