forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'neetcode-gh:main' into main
- Loading branch information
Showing
6 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
java/1160-Find-Words-That-Can-Be-Formed-by-Characters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; |