Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit230b959

Browse files
authored
Merge pull requestneetcode-gh#1062 from Ykhan799/main
Create: 84-Largest-Rectangle-in-Histogram.ts, 4-Median-of-Two-Sorted-Arrays.ts, 25-Reverse-Nodes-in-k-Group.ts, 1899-Merge-Triplets-to-Form-Target-Triplet.ts, 10-Regular-Expression-Matching.ts, 134-Gas-Station.ts, 55-Jump-Game.ts, 45-Jump-Game-II.ts, 199-Binary-Tree-Right-Side-View.ts, 124-Binary-Tree-Maximum-Path-Sum.ts, 98-Validate-Binary-Search-Tree.ts, 678-Valid-Parenthesis-String.ts, 518-Coin-Change-II.ts, 97-Interleaving-String.ts, 115-Distinct-Subsequences.ts, 312-Burst-Balloons.ts, 105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.ts, 215-Kth-Largest-Element-in-an-Array.ts, 621-Task-Scheduler.ts, 994-Rotting-Oranges.ts, 210-Course-Schedule-II.ts, 207-Course-Schedule.ts, 684-Redundant-Connection.ts, 127-Word-Ladder.ts, 787-Cheapest-Flights-Within-K-Stops.ts, 494-Target-Sum.ts, 846-Hand-of-Straights.ts
2 parents793ea14 +560f835 commit230b959

File tree

27 files changed

+811
-0
lines changed

27 files changed

+811
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
functionisMatch(s:string,p:string):boolean{
2+
letlenS=s.length;
3+
letlenP=p.length;
4+
letmap={};
5+
6+
returncheck(0,0);
7+
8+
functioncheck(idxS:number,idxP:number):boolean{
9+
if(map[idxS+':'+idxP]!==undefined){
10+
returnmap[idxS+':'+idxP];
11+
}
12+
13+
if(idxS>lenS){
14+
returnfalse;
15+
}
16+
17+
if(idxS===lenS&&idxP===lenP){
18+
returntrue;
19+
}
20+
21+
if(p[idxP]==='.'||p[idxP]===s[idxS]){
22+
map[idxS+':'+idxP]=p[idxP+1]==='*' ?check(idxS+1,idxP)||check(idxS,idxP+2) :check(idxS+1,idxP+1);
23+
}
24+
25+
else{
26+
map[idxS+':'+idxP]=p[idxP+1]==='*' ?check(idxS,idxP+2) :false;
27+
}
28+
29+
returnmap[idxS+':'+idxP];
30+
}
31+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
functionbuildTree(preorder:number[],inorder:number[]):TreeNode|null{
16+
if(!preorder.length||!inorder.length){
17+
returnnull;
18+
}
19+
20+
letroot=newTreeNode(preorder[0]);
21+
letmid=inorder.indexOf(preorder[0]);
22+
23+
root.left=buildTree(preorder.slice(1,mid+1),inorder.slice(0,mid));
24+
root.right=buildTree(preorder.slice(mid+1),inorder.slice(mid+1));
25+
returnroot;
26+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
functionnumDistinct(s:string,t:string):number{
2+
constsLen=s.length;
3+
consttLen=t.length;
4+
5+
if(sLen<tLen){
6+
return0;
7+
}
8+
9+
constcache=newArray(tLen).fill(0);
10+
for(letr=sLen-1;r>=0;r--){
11+
letprev=1;
12+
for(letc=tLen-1;c>=0;c--){
13+
constcurr=cache[c];
14+
if(s[r]===t[c]){
15+
cache[c]+=prev;
16+
}
17+
prev=curr;
18+
}
19+
}
20+
returncache[0];
21+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
functionmaxPathSum(root:TreeNode|null):number{
16+
constres=[root.val];
17+
18+
// return max path sum without split
19+
functiondfs(root:TreeNode|null):number{
20+
if(!root){
21+
return0;
22+
}
23+
24+
letleftMax=dfs(root.left);
25+
letrightMax=dfs(root.right);
26+
leftMax=Math.max(leftMax,0);
27+
rightMax=Math.max(rightMax,0);
28+
29+
// compute max path sum WITH split
30+
res[0]=Math.max(res[0],root.val+leftMax+rightMax);
31+
returnroot.val+Math.max(leftMax,rightMax);
32+
}
33+
34+
dfs(root);
35+
returnres[0];
36+
};

‎typescript/127-Word-Ladder.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
functionladderLength(beginWord:string,endWord:string,wordList:string[]):number{
2+
if(!wordList.includes(endWord)){
3+
return0;
4+
}
5+
letpatternMap={};
6+
wordList.push(beginWord);
7+
8+
for(letwordofwordList){
9+
for(letx=0;x<word.length;x++){
10+
constpattern=word.slice(0,x)+'*'+word.slice(x+1);
11+
patternMap[pattern]=patternMap[pattern]||[];
12+
patternMap[pattern].push(word);
13+
}
14+
}
15+
16+
letwordCount=1,queue=[beginWord],visited=[beginWord];
17+
while(queue.length){
18+
constlevelSize=queue.length;
19+
for(letx=0;x<levelSize;x++){
20+
constword=queue.shift();
21+
if(word===endWord){
22+
returnwordCount;
23+
}
24+
25+
for(letx=0;x<word.length;x++){
26+
constpattern=word.slice(0,x)+'*'+word.slice(x+1);
27+
for(letneiofpatternMap[pattern]){
28+
if(neiinvisited){
29+
continue;
30+
}
31+
visited[nei]=nei;
32+
queue.push(nei);
33+
}
34+
}
35+
}
36+
wordCount++;
37+
}
38+
return0;
39+
};

‎typescript/134-Gas-Station.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
functioncanCompleteCircuit(gas:number[],cost:number[]):number{
2+
letres=0;
3+
letnetDistance=0;
4+
5+
// Checks if there is enough gas to complete a cycle
6+
if(gas.reduce((a,b)=>a+b)-cost.reduce((a,b)=>a+b)<0){
7+
return-1;
8+
}
9+
10+
// finds positive netDistance to check if cycle can be completed
11+
for(leti=0;i<gas.length;i++){
12+
netDistance+=gas[i]-cost[i];
13+
14+
// resets net Distance and gets gas starting gas station
15+
if(netDistance<0){
16+
netDistance=0;
17+
res=i+1;
18+
}
19+
}
20+
returnres;
21+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
functionmergeTriplets(triplets:number[][],target:number[]):boolean{
2+
letgood=newSet();
3+
4+
for(lettintriplets){
5+
lettriplet=triplets[t];
6+
if(triplet[0]>target[0]||triplet[1]>target[1]||triplet[2]>target[2]){
7+
continue;
8+
}
9+
10+
for(leti=0;i<triplet.length;i++){
11+
if(triplet[i]===target[i]){
12+
good.add(i);
13+
}
14+
}
15+
}
16+
returngood.size===3;
17+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
functionrightSideView(root:TreeNode|null):number[]{
16+
letresult=[];
17+
letqueue=[];
18+
19+
if(root===null){
20+
return[];
21+
}
22+
23+
queue.push(root);
24+
25+
while(queue.length>0){
26+
letlength=queue.length;
27+
for(leti=0;i<length;i++){
28+
letnode=queue.shift();
29+
30+
if(i===length-1){
31+
result.push(node.val);
32+
}
33+
if(node.left!==null){
34+
queue.push(node.left);
35+
}
36+
if(node.right!==null){
37+
queue.push(node.right);
38+
}
39+
}
40+
}
41+
returnresult;
42+
};

‎typescript/207-Course-Schedule.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
functioncanFinish(numCourses:number,prerequisites:number[][]):boolean{
2+
constgraph=createGraph(numCourses,prerequisites);
3+
letseen=newSet();
4+
letseeing=newSet();
5+
6+
functionexplore(course:number):boolean{
7+
if(seen.has(course)){
8+
returntrue;
9+
}
10+
if(seeing.has(course)){
11+
returnfalse;
12+
}
13+
14+
seeing.add(course);
15+
for(letneighborofgraph[course]){
16+
if(!explore(neighbor)){
17+
returnfalse;
18+
}
19+
}
20+
21+
seen.add(course);
22+
seeing.delete(course);
23+
returntrue;
24+
}
25+
26+
for(leti=0;i<numCourses;i++){
27+
if(!explore(i)){
28+
returnfalse;
29+
}
30+
}
31+
returntrue;
32+
};
33+
34+
functioncreateGraph(numCourses:number,edges:number[][]):number[][]{
35+
constgraph=Array.from({length:numCourses},()=>[]);
36+
37+
for(letedgeofedges){
38+
let[a,b]=edge;
39+
40+
if(!(aingraph)){
41+
graph[a]=[];
42+
}
43+
if(!(bingraph)){
44+
graph[b]=[];
45+
}
46+
graph[a].push(b);
47+
}
48+
returngraph;
49+
}

‎typescript/210-Course-Schedule-II.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
functionfindOrder(numCourses:number,prerequisites:number[][]):number[]{
2+
constprereq=[];
3+
for(leti=0;i<numCourses;i++){
4+
prereq[i]=[];
5+
}
6+
for(const[crs,pre]ofprerequisites){
7+
prereq[crs].push(pre);
8+
}
9+
10+
constoutput=[];
11+
constvisit=newSet();
12+
constcycle=newSet();
13+
14+
functiondfs(course:number):boolean{
15+
if(cycle.has(course)){
16+
returnfalse;
17+
}
18+
if(visit.has(course)){
19+
returntrue;
20+
}
21+
22+
cycle.add(course);
23+
for(constpreofprereq[course]){
24+
if(!dfs(pre)){
25+
returnfalse;
26+
}
27+
}
28+
cycle.delete(course);
29+
visit.add(course);
30+
output.push(course);
31+
returntrue;
32+
}
33+
34+
for(letj=0;j<numCourses;j++){
35+
if(!dfs(j)){
36+
return[];
37+
}
38+
}
39+
40+
returnoutput;
41+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
functionfindKthLargest(nums:number[],k:number):number{
2+
k=nums.length-k;
3+
4+
functionquickSelect(l:number,r:number):number{
5+
constpivot=nums[r];
6+
letp=l;
7+
leti=l;
8+
lettemp;
9+
while(i>=l&&i<r){
10+
if(nums[i]<=pivot){
11+
temp=nums[p];
12+
nums[p]=nums[i];
13+
nums[i]=temp;
14+
p++;
15+
}
16+
i++;
17+
}
18+
19+
temp=nums[p];
20+
nums[p]=nums[r];
21+
nums[r]=temp;
22+
23+
if(p>k){
24+
returnquickSelect(l,p-1);
25+
}
26+
if(p<k){
27+
returnquickSelect(p+1,r);
28+
}
29+
returnnums[p];
30+
}
31+
32+
returnquickSelect(0,nums.length-1);
33+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp