Skip to content

Commit

Permalink
Merge branch 'neetcode-gh:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
miladra authored Jul 6, 2022
2 parents c6b6660 + 65b8987 commit 37f8d89
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 5 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Leetcode solutions for [NeetCode](https://www.youtube.com/c/neetcode)
# Leetcode solutions for [NeetCode.io](https://neetcode.io)

### Updates

I will periodically update the neetcode.io site with new solutions for this repo!

### Contributing

Expand Down
26 changes: 26 additions & 0 deletions java/1155-Number-of-Dice-Rolls-With-Target-Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
int mod = 1000000007;
public int numRollsToTarget(int n, int k, int target) {
if (target>n*k || target<n)
return 0;
if (n==1)
return target<n?0:1;
int[][] dp = new int[n+1][target+1];
return helper(n, k, target, dp);
}

public int helper(int n, int k , int target, int[][] dp) {
if (target>n*k || target<n)
return 0;
if (target==0 && n==0)
return 1;
if (dp[n][target]!=0)
return dp[n][target];
int sum = 0;
for (int i =1; i<=k; i++) {
sum = (sum+helper(n-1, k, target-i, dp))%mod;
}
return dp[n][target]=sum;
}

}
24 changes: 24 additions & 0 deletions java/1160-Find-Words-That-Can-Be-Formed-by-Characters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//Intuitive approach

class Solution {
public int countCharacters(String[] words, String chars) {
int[] arrChars = new int[26];
for (int i = 0; i<chars.length(); i++) {
arrChars[chars.charAt(i)-'a']++;
}
int ans = 0;
for (String s: words) {
boolean flag = true;
int[] arrS = new int[26];
for (int i= 0; i<s.length(); i++) {
arrS[s.charAt(i)-'a']++;
}
for (int i =0; i<26; i++) {
if (arrS[i]>arrChars[i])
flag = false;
}
if (flag) ans += s.length();
}
return ans;
}
}
8 changes: 4 additions & 4 deletions java/49-Group-Anagrams.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> res = new ArrayList<>();
if(strs.length==0) return res;
HashMap<String, List<String>> map = new HashMap();
HashMap<String, List<String>> map = new HashMap<>();
for(String s: strs){
char[] hash = new char[26];
for(char c: s.toCharArray()){
hash[c-'a']++;
}
String str=new String(hash);
map.computeIfAbsent(str, k -> new ArrayList<>());
map.get(str).add(s);
String key = new String(hash);
map.computeIfAbsent(key, k -> new ArrayList<>());
map.get(key).add(s);
}
res.addAll(map.values());
return res;
Expand Down
23 changes: 23 additions & 0 deletions java/63-Unique-Paths-II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//Same as unique paths 1 just one extra condition to return 0 if grid[i][j] == 1

class Solution {
public int uniquePathsWithObstacles(int[][] grid) {
return dfs(grid, 0, 0, grid.length, grid[0].length, new int[grid.length][grid[0].length]);
}

public int dfs(int[][] grid, int i, int j, int m, int n, int[][] dp) {
if (i<0 || j<0 || i>=m || j>=n || grid[i][j]==1) {
return 0;
}
if (i==m-1 && j==n-1) {
dp[i][j] = 1;
return dp[i][j];
}
if (dp[i][j]!=0) return dp[i][j];
int right = dfs(grid, i, j+1, m, n, dp);
int left = dfs(grid, i+1, j, m, n, dp);
dp[i][j] = right+left;
return dp[i][j];
}

}
44 changes: 44 additions & 0 deletions javascript/332-Reconstruct-Itinerary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @param {string[][]} tickets
* @return {string[]}
*/
var findItinerary = function (tickets) {
const flight_paths = new Map();
const flight_path_order = ["JFK"];

tickets = tickets.sort();

for (const [source, dest] of tickets) {
let edges = [];
if (flight_paths.has(source)) {
edges = flight_paths.get(source);
}
edges.push(dest);
flight_paths.set(source, edges);
}

const depth_first_search = (city) => {
if (flight_path_order.length === tickets.length + 1) return true;

const cities_to_go_to = flight_paths.get(city) || [];
if (!cities_to_go_to.length) return false;

const cities_copied = Array.from(cities_to_go_to);

for (const other_city of cities_copied) {
flight_path_order.push(other_city);
cities_to_go_to.shift();

if (depth_first_search(other_city)) {
return flight_path_order;
} else {
flight_path_order.pop();
cities_to_go_to.push(other_city);
}
}

return false;
};

return depth_first_search("JFK");
};

0 comments on commit 37f8d89

Please sign in to comment.