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

Commit37f8d89

Browse files
authored
Merge branch 'neetcode-gh:main' into main
2 parentsc6b6660 +65b8987 commit37f8d89

File tree

6 files changed

+126
-5
lines changed

6 files changed

+126
-5
lines changed

‎README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
#Leetcode solutions for[NeetCode](https://www.youtube.com/c/neetcode)
1+
#Leetcode solutions for[NeetCode.io](https://neetcode.io)
2+
3+
###Updates
4+
5+
I will periodically update the neetcode.io site with new solutions for this repo!
26

37
###Contributing
48

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
classSolution {
2+
intmod =1000000007;
3+
publicintnumRollsToTarget(intn,intk,inttarget) {
4+
if (target>n*k ||target<n)
5+
return0;
6+
if (n==1)
7+
returntarget<n?0:1;
8+
int[][]dp =newint[n+1][target+1];
9+
returnhelper(n,k,target,dp);
10+
}
11+
12+
publicinthelper(intn,intk ,inttarget,int[][]dp) {
13+
if (target>n*k ||target<n)
14+
return0;
15+
if (target==0 &&n==0)
16+
return1;
17+
if (dp[n][target]!=0)
18+
returndp[n][target];
19+
intsum =0;
20+
for (inti =1;i<=k;i++) {
21+
sum = (sum+helper(n-1,k,target-i,dp))%mod;
22+
}
23+
returndp[n][target]=sum;
24+
}
25+
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//Intuitive approach
2+
3+
classSolution {
4+
publicintcountCharacters(String[]words,Stringchars) {
5+
int[]arrChars =newint[26];
6+
for (inti =0;i<chars.length();i++) {
7+
arrChars[chars.charAt(i)-'a']++;
8+
}
9+
intans =0;
10+
for (Strings:words) {
11+
booleanflag =true;
12+
int[]arrS =newint[26];
13+
for (inti=0;i<s.length();i++) {
14+
arrS[s.charAt(i)-'a']++;
15+
}
16+
for (inti =0;i<26;i++) {
17+
if (arrS[i]>arrChars[i])
18+
flag =false;
19+
}
20+
if (flag)ans +=s.length();
21+
}
22+
returnans;
23+
}
24+
}

‎java/49-Group-Anagrams.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ class Solution {
22
publicList<List<String>>groupAnagrams(String[]strs) {
33
List<List<String>>res =newArrayList<>();
44
if(strs.length==0)returnres;
5-
HashMap<String,List<String>>map =newHashMap();
5+
HashMap<String,List<String>>map =newHashMap<>();
66
for(Strings:strs){
77
char[]hash =newchar[26];
88
for(charc:s.toCharArray()){
99
hash[c-'a']++;
1010
}
11-
Stringstr=newString(hash);
12-
map.computeIfAbsent(str,k ->newArrayList<>());
13-
map.get(str).add(s);
11+
Stringkey =newString(hash);
12+
map.computeIfAbsent(key,k ->newArrayList<>());
13+
map.get(key).add(s);
1414
}
1515
res.addAll(map.values());
1616
returnres;

‎java/63-Unique-Paths-II.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//Same as unique paths 1 just one extra condition to return 0 if grid[i][j] == 1
2+
3+
classSolution {
4+
publicintuniquePathsWithObstacles(int[][]grid) {
5+
returndfs(grid,0,0,grid.length,grid[0].length,newint[grid.length][grid[0].length]);
6+
}
7+
8+
publicintdfs(int[][]grid,inti,intj,intm,intn,int[][]dp) {
9+
if (i<0 ||j<0 ||i>=m ||j>=n ||grid[i][j]==1) {
10+
return0;
11+
}
12+
if (i==m-1 &&j==n-1) {
13+
dp[i][j] =1;
14+
returndp[i][j];
15+
}
16+
if (dp[i][j]!=0)returndp[i][j];
17+
intright =dfs(grid,i,j+1,m,n,dp);
18+
intleft =dfs(grid,i+1,j,m,n,dp);
19+
dp[i][j] =right+left;
20+
returndp[i][j];
21+
}
22+
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*@param {string[][]} tickets
3+
*@return {string[]}
4+
*/
5+
varfindItinerary=function(tickets){
6+
constflight_paths=newMap();
7+
constflight_path_order=["JFK"];
8+
9+
tickets=tickets.sort();
10+
11+
for(const[source,dest]oftickets){
12+
letedges=[];
13+
if(flight_paths.has(source)){
14+
edges=flight_paths.get(source);
15+
}
16+
edges.push(dest);
17+
flight_paths.set(source,edges);
18+
}
19+
20+
constdepth_first_search=(city)=>{
21+
if(flight_path_order.length===tickets.length+1)returntrue;
22+
23+
constcities_to_go_to=flight_paths.get(city)||[];
24+
if(!cities_to_go_to.length)returnfalse;
25+
26+
constcities_copied=Array.from(cities_to_go_to);
27+
28+
for(constother_cityofcities_copied){
29+
flight_path_order.push(other_city);
30+
cities_to_go_to.shift();
31+
32+
if(depth_first_search(other_city)){
33+
returnflight_path_order;
34+
}else{
35+
flight_path_order.pop();
36+
cities_to_go_to.push(other_city);
37+
}
38+
}
39+
40+
returnfalse;
41+
};
42+
43+
returndepth_first_search("JFK");
44+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp