Skip to content

Commit 37f8d89

Browse files
authored
Merge branch 'neetcode-gh:main' into main
2 parents c6b6660 + 65b8987 commit 37f8d89

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+
class Solution {
2+
int mod = 1000000007;
3+
public int numRollsToTarget(int n, int k, int target) {
4+
if (target>n*k || target<n)
5+
return 0;
6+
if (n==1)
7+
return target<n?0:1;
8+
int[][] dp = new int[n+1][target+1];
9+
return helper(n, k, target, dp);
10+
}
11+
12+
public int helper(int n, int k , int target, int[][] dp) {
13+
if (target>n*k || target<n)
14+
return 0;
15+
if (target==0 && n==0)
16+
return 1;
17+
if (dp[n][target]!=0)
18+
return dp[n][target];
19+
int sum = 0;
20+
for (int i =1; i<=k; i++) {
21+
sum = (sum+helper(n-1, k, target-i, dp))%mod;
22+
}
23+
return dp[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+
class Solution {
4+
public int countCharacters(String[] words, String chars) {
5+
int[] arrChars = new int[26];
6+
for (int i = 0; i<chars.length(); i++) {
7+
arrChars[chars.charAt(i)-'a']++;
8+
}
9+
int ans = 0;
10+
for (String s: words) {
11+
boolean flag = true;
12+
int[] arrS = new int[26];
13+
for (int i= 0; i<s.length(); i++) {
14+
arrS[s.charAt(i)-'a']++;
15+
}
16+
for (int i =0; i<26; i++) {
17+
if (arrS[i]>arrChars[i])
18+
flag = false;
19+
}
20+
if (flag) ans += s.length();
21+
}
22+
return ans;
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
public List<List<String>> groupAnagrams(String[] strs) {
33
List<List<String>> res = new ArrayList<>();
44
if(strs.length==0) return res;
5-
HashMap<String, List<String>> map = new HashMap();
5+
HashMap<String, List<String>> map = new HashMap<>();
66
for(String s: strs){
77
char[] hash = new char[26];
88
for(char c: s.toCharArray()){
99
hash[c-'a']++;
1010
}
11-
String str=new String(hash);
12-
map.computeIfAbsent(str, k -> new ArrayList<>());
13-
map.get(str).add(s);
11+
String key = new String(hash);
12+
map.computeIfAbsent(key, k -> new ArrayList<>());
13+
map.get(key).add(s);
1414
}
1515
res.addAll(map.values());
1616
return res;

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+
class Solution {
4+
public int uniquePathsWithObstacles(int[][] grid) {
5+
return dfs(grid, 0, 0, grid.length, grid[0].length, new int[grid.length][grid[0].length]);
6+
}
7+
8+
public int dfs(int[][] grid, int i, int j, int m, int n, int[][] dp) {
9+
if (i<0 || j<0 || i>=m || j>=n || grid[i][j]==1) {
10+
return 0;
11+
}
12+
if (i==m-1 && j==n-1) {
13+
dp[i][j] = 1;
14+
return dp[i][j];
15+
}
16+
if (dp[i][j]!=0) return dp[i][j];
17+
int right = dfs(grid, i, j+1, m, n, dp);
18+
int left = dfs(grid, i+1, j, m, n, dp);
19+
dp[i][j] = right+left;
20+
return dp[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+
var findItinerary = function (tickets) {
6+
const flight_paths = new Map();
7+
const flight_path_order = ["JFK"];
8+
9+
tickets = tickets.sort();
10+
11+
for (const [source, dest] of tickets) {
12+
let edges = [];
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+
const depth_first_search = (city) => {
21+
if (flight_path_order.length === tickets.length + 1) return true;
22+
23+
const cities_to_go_to = flight_paths.get(city) || [];
24+
if (!cities_to_go_to.length) return false;
25+
26+
const cities_copied = Array.from(cities_to_go_to);
27+
28+
for (const other_city of cities_copied) {
29+
flight_path_order.push(other_city);
30+
cities_to_go_to.shift();
31+
32+
if (depth_first_search(other_city)) {
33+
return flight_path_order;
34+
} else {
35+
flight_path_order.pop();
36+
cities_to_go_to.push(other_city);
37+
}
38+
}
39+
40+
return false;
41+
};
42+
43+
return depth_first_search("JFK");
44+
};

0 commit comments

Comments
 (0)