8000 feat: Solved 6 Leetcode problems · ralvarezdev/leetcode@571060e · GitHub
[go: up one dir, main page]

Skip to content

Commit 571060e

Browse files
committed
feat: Solved 6 Leetcode problems
* Solved 2 Leetcode problems from the 30 Days of JavaScript study plan * Solved 4 additional Leetcode problems
1 parent 13d0a40 commit 571060e

8 files changed

+238
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {void}
4+
*/
5+
var ArrayWrapper = function(nums) {
6+
this.nums=nums
7+
this.total=nums.length===0?0: nums.reduce((accum, value)=>accum+=value)
8+
};
9+
10+
/**
11+
* @return {number}
12+
*/
13+
ArrayWrapper.prototype.valueOf = function() {
14+
return this.total
15+
}
16+
17+
/**
18+
* @return {string}
19+
*/
20+
ArrayWrapper.prototype.toString = function() {
21+
return "[" +this.nums + "]"
22+
}
23+
24+
/**
25+
* const obj1 = new ArrayWrapper([1,2]);
26+
* const obj2 = new ArrayWrapper([3,4]);
27+
* obj1 + obj2; // 10
28+
* String(obj1); // "[1,2]"
29+
* String(obj2); // "[3,4]"
30+
*/
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Calculator {
2+
#value
3+
4+
/**
5+
* @param {number} value
6+
*/
7+
constructor(value) {
8+
this.#value=value
9+
}
10+
11+
/**
12+
* @param {number} value
13+
* @return {Calculator}
14+
*/
15+
add(value){
16+
this.#value+=value
17+
return this
18+
}
19+
20+
/**
21+
* @param {number} value
22+
* @return {Calculator}
23+
*/
24+
subtract(value){
25+
this.#value-=value
26+
return this
27+
}
28+
29+
/**
30+
* @param {number} value
31+
* @return {Calculator}
32+
*/
33+
multiply(value) {
34+
this.#value*=value
35+
return this
36+
}
37+
38+
/**
39+
* @param {number} value
40+
* @return {Calculator}
41+
*/
42+
divide(value) {
43+
if(value===0)
44+
throw new Error("Division by zero is not allowed")
45+
46+
this.#value/=value
47+
return this
48+
}
49+
50+
/**
51+
* @param {number} value
52+
* @return {Calculator}
53+
*/
54+
power(value) {
55+
this.#value**=value
56+
return this
57+
}
58+
59+
/**
60+
* @return {number}
61+
*/
62+
getResult() {
63+
return this.#value
64+
}
65+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int findCenter(int[][] edges) {
3+
int[] nodes=edges[0];
4+
5+
for(int node: edges[1])
6+
if(node==nodes[0]||node==nodes[1])
7+
return node;
8+
9+
return -1;
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def findCenter(self, edges: List[List[int]]) -> int:
3+
nodes=edges[0]
4+
5+
for node in edges[1]:
6+
if node == nodes[0] or node == nodes[1]:
7+
return node
8+
9+
return -1
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} edges
4+
* @return {number[][]}
5+
*/
6+
var getAncestors = function(n, edges) {
7+
const arr = new Array(n)
8+
const traverse=new Array(n)
9+
10+
for(let i=0;i<n;i++)
11+
{
12+
arr[i]=[]
13+
traverse[i]=false
14+
}
15+
16+
const getPos=(arr, value)=>{
17+
let low=0
18+
let high=arr.length-1
19+
let isUpper
20+
21+
if(high<0)
22+
return 0
23+
24+
while(high>=low)
25+
{
26+
mid = low + Math.floor((high - low) / 2);
27+
28+
if (arr[mid] === value)
29+
return -1;
30+
31+
isUpper = arr[mid] < value
32+
33+
if(isUpper)
34+
low = mid + 1;
35+
36+
else
37+
high = mid - 1;
38+
}
39+
40+
return (isUpper)?mid+1:mid;
41+
}
42+
43+
const sortedInsertion = (arr, value)=>{
44+
const pos = getPos(arr,value)
45+
46+
if(pos>=0)
47+
return arr.toSpliced(pos,0,value)
48+
49+
return arr
50+
}
51+
52+
for(let [fromNode, toNode] of edges)
53+
arr[toNode]=sortedInsertion(arr[toNode], fromNode)
54+
55+
const getAncestor=(arr, toNode)=>{
56+
if(!traverse[toNode]){
57+
for(let fromNode of arr[toNode]) {
58+
if(!traverse[fromNode])
59+
getAncestor(arr, fromNode)
60+
61+
for(let fromFromNode of arr[fromNode])
62+
arr[toNode]=sortedInsertion(arr[toNode], fromFromNode)
63+
}
64+
65+
traverse[toNode]=true
66+
}
67+
}
68+
69+
for(let i=0;i<n;i++)
70+
getAncestor(arr, i)
71+
72+
return arr
73+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var findPrefixScore = function(nums) {
6+
const arr = new Array(nums.length)
7+
let counter=0
8+
let maxNum=0
9+
let accum=0
10+
11+
for(let num of nums){
12+
if(num>maxNum)
13+
maxNum=num
14+
15+
arr[counter++]=accum=maxNum+num+accum
16+
}
17+
18+
return arr
19+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def maximumImportance(self, n: int, roads: List[List[int]]) -> int:
3+
counter = dict()
4+
5+
for road in roads:
6+
for node in road:
7+
if node not in counter:
8+
counter[node]=1
9+
else:
10+
counter[node]+=1
11+
12+
counterList=list(counter.items())
13+
14+
def sortFunc(entry):
15+
return entry[1]
16+
17+
counterList.sort(key=sortFunc, reverse=True)
18+
19+
importance = dict()
20+
maxCounter=n
21+
22+
for node in counterList:
23+
importance[node[0]]=maxCounter
24+
maxCounter-=1
25+
26+
total=0
27+
for road in roads:
28+
for i in range(0, 2):
29+
total+=importance[road[i]]
30+
31+
return total

0 commit comments

Comments
 (0)
0