forked from DaleStudy/leetcode-study
-
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.
[bky373] Add Week 11 Solutions (DaleStudy#191)
* Add Week 11 Solutions * Add time and space complexity
- Loading branch information
Showing
5 changed files
with
129 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// time: O(n * m), where n is the amount and m is the number of coins | ||
// space: O(n) | ||
class Solution { | ||
|
||
public int coinChange(int[] coins, int amount) { | ||
int max = amount + 1; | ||
int[] dp = new int[amount + 1]; | ||
Arrays.fill(dp, max); | ||
dp[0] = 0; | ||
for (int i = 1; i <= amount; i++) { | ||
for (int j = 0; j < coins.length; j++) { | ||
if (coins[j] <= i) { | ||
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); | ||
} | ||
} | ||
} | ||
if (dp[amount] > amount) { | ||
return -1; | ||
} | ||
return dp[amount]; | ||
} | ||
} |
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,22 @@ | ||
// time: O(N) | ||
// space: O(N) | ||
class Solution { | ||
|
||
public int numDecodings(String s) { | ||
int[] dp = new int[s.length() + 1]; | ||
dp[0] = 1; | ||
dp[1] = s.charAt(0) == '0' ? 0 : 1; | ||
|
||
for (int i = 2; i < dp.length; i++) { | ||
if (s.charAt(i - 1) != '0') { | ||
dp[i] = dp[i - 1]; | ||
} | ||
|
||
int twoDigits = Integer.valueOf(s.substring(i - 2, i)); | ||
if (twoDigits >= 10 && twoDigits <= 26) { | ||
dp[i] += dp[i - 2]; | ||
} | ||
} | ||
return dp[s.length()]; | ||
} | ||
} |
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,29 @@ | ||
// time: O(N) | ||
// space: O(1) | ||
class Solution { | ||
|
||
public int maxProduct(int[] nums) { | ||
double max = Integer.MIN_VALUE; | ||
double product = 1; | ||
|
||
for (int num : nums) { | ||
product *= num; | ||
max = Math.max(product, max); | ||
if (product == 0) { | ||
product = 1; | ||
} | ||
|
||
} | ||
|
||
product = 1; | ||
for (int i = nums.length - 1; i >= 0; i--) { | ||
product *= nums[i]; | ||
max = Math.max(product, max); | ||
if (product == 0) { | ||
product = 1; | ||
} | ||
} | ||
|
||
return (int) max; | ||
} | ||
} |
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,25 @@ | ||
// time: O(N^3) (the worst case) | ||
// space: O(1) | ||
class Solution { | ||
|
||
public int countSubstrings(String s) { | ||
int count = 0; | ||
for (int i = 0; i < s.length(); i++) { | ||
for (int j = i; j < s.length(); j++) { | ||
count += isPalindrome(s, i, j); | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
int isPalindrome(String s, int start, int end) { | ||
while (start < end) { | ||
if (s.charAt(start) != s.charAt(end)) { | ||
return 0; | ||
} | ||
start++; | ||
end--; | ||
} | ||
return 1; | ||
} | ||
} |
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,31 @@ | ||
// time: O(N^2) | ||
// space: O(N) | ||
class Solution { | ||
|
||
public boolean wordBreak(String s, List<String> wordDict) { | ||
Set<String> words = new HashSet<>(wordDict); | ||
Queue<Integer> que = new LinkedList<>(); | ||
boolean[] visited = new boolean[s.length() + 1]; | ||
que.add(0); | ||
|
||
while (!que.isEmpty()) { | ||
int start = que.remove(); | ||
if (start == s.length()) { | ||
return true; | ||
} | ||
|
||
for (int i = start + 1; i <= s.length(); i++) { | ||
if (visited[i]) { | ||
continue; | ||
} | ||
|
||
if (words.contains(s.substring(start, i))) { | ||
que.add(i); | ||
visited[i] = true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |