-
-
Notifications
You must be signed in to change notification settings - Fork 245
[GangBean] Week 4 #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[GangBean] Week 4 #825
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
931ef0f
feat: solve merge two sorted lists
GangBean fd8f57e
feat: solve missing number
GangBean 611a716
feat: solve word search
GangBean 3336965
feat: solve palindromic substrings
GangBean 2f88c69
feat: solve coin change
GangBean 9c6b684
refactor: remove duplicate code in word search
GangBean e9cabee
refactor: remove coins sorting
GangBean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 @@ | ||
class Solution { | ||
public int coinChange(int[] coins, int amount) { | ||
/** | ||
1. understanding | ||
- given coins that can be used, find the minimum count of coins sum up to input amount value. | ||
- [1,2,5]: 11 | ||
- 2 * 5 + 1 * 1: 3 -> use high value coin as much as possible if the remain can be sumed up by remain coins. | ||
2. strategy | ||
- If you search in greedy way, it will takes over O(min(amount/coin) ^ N), given N is the length of coins. | ||
- Let dp[k] is the number of coins which are sum up to amount k, in a given coin set. | ||
- Then, dp[k] = min(dp[k], dp[k-coin] + 1) | ||
3. complexity | ||
- time: O(CA), where C is the length of coins, A is amount value | ||
- space: O(A), where A is amount value | ||
*/ | ||
Arrays.sort(coins); | ||
|
||
int[] dp = new int[amount + 1]; | ||
for (int i = 1; i <= amount; i++) { | ||
dp[i] = amount + 1; | ||
} | ||
|
||
for (int coin: coins) { // O(C) | ||
for (int k = coin; k <= amount; k++) { // O(A) | ||
dp[k] = Math.min(dp[k], dp[k-coin] + 1); | ||
} | ||
} | ||
|
||
return (dp[amount] >= amount + 1) ? -1 : dp[amount]; | ||
} | ||
} | ||
|
This file contains hidden or 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,73 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
/** | ||
1. understanding | ||
- merge 2 sorted linked list | ||
2. strategy | ||
- assign return ListNode | ||
- for each node, started in head, compare each node's value, and add smaller value node to return node, and move the node's head to head.next | ||
3. complexity | ||
- time: O(N + M), N is the length of list1, M is the length of list2 | ||
- space: O(1), exclude the return variable | ||
*/ | ||
ListNode curr = null; | ||
ListNode ret = null; | ||
|
||
while (list1 != null && list2 != null) { | ||
if (list1.val < list2.val) { | ||
ListNode node = new ListNode(list1.val); | ||
if (ret == null) { | ||
ret = node; | ||
} else { | ||
curr.next = node; | ||
} | ||
list1 = list1.next; | ||
curr = node; | ||
} else { | ||
ListNode node = new ListNode(list2.val); | ||
if (ret == null) { | ||
ret = node; | ||
} else { | ||
curr.next = node; | ||
} | ||
list2 = list2.next; | ||
curr = node; | ||
} | ||
} | ||
|
||
while (list1 != null) { | ||
ListNode node = new ListNode(list1.val); | ||
if (ret == null) { | ||
ret = node; | ||
} else { | ||
curr.next = node; | ||
} | ||
list1 = list1.next; | ||
curr = node; | ||
} | ||
|
||
while (list2 != null) { | ||
ListNode node = new ListNode(list2.val); | ||
if (ret == null) { | ||
ret = node; | ||
} else { | ||
curr.next = node; | ||
} | ||
list2 = list2.next; | ||
curr = node; | ||
} | ||
|
||
return ret; | ||
} | ||
} | ||
|
This file contains hidden or 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,19 @@ | ||
class Solution { | ||
public int missingNumber(int[] nums) { | ||
/** | ||
1. understanding | ||
- array nums, n distinct numbers in range [0, n] | ||
- find missing number | ||
2. strategy | ||
- you can calculate the sum of range [0, n]: n(n+1)/2 ... (1) | ||
- and the sum of nums ... (2) | ||
- and then extract (2) from (1) = (missing value) what we want. | ||
3. complexity | ||
- time: O(N), N is the length of nums | ||
- space: O(1) | ||
*/ | ||
int N = nums.length; | ||
return N*(N+1)/2 - Arrays.stream(nums).sum(); | ||
} | ||
} | ||
|
DaleSeo marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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,51 @@ | ||
class Solution { | ||
int[] dx = {0, 1, 0, -1}; | ||
int[] dy = {1, 0, -1, 0}; | ||
public boolean exist(char[][] board, String word) { | ||
/** | ||
1. understanding | ||
- check if word can be constructed from board, | ||
- start in any block, moving only 4 direction, up, left, below, right | ||
- can't use same block | ||
2. strategy | ||
- backtracking and dfs | ||
- iterate over each block, if first character matches, find words in depth first search algorithm | ||
- each dfs, mark current block is visited, and find 4 or less possible directions, when any character matches with next character in word, then call dfs in that block recursively | ||
3. complexity | ||
- time: O(M * N * L), where L is the length of word | ||
- space: O(M * N) which marks if block of the indices is visited or not | ||
*/ | ||
boolean[][] isVisited = new boolean[board.length][board[0].length]; | ||
boolean ret = false; | ||
for (int y = 0; y < board.length; y++) { | ||
for (int x = 0; x < board[0].length; x++) { | ||
if (board[y][x] == word.charAt(0)) { | ||
isVisited[y][x] = true; | ||
ret = ret || isWordExists(board, isVisited, word, y, x, 0); | ||
isVisited[y][x] = false; | ||
} | ||
DaleSeo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
return ret; | ||
} | ||
|
||
private boolean isWordExists(char[][] board, boolean[][] isVisited, String word, int y, int x, int idx) { | ||
if (idx == word.length() - 1) return true; | ||
// System.out.println(String.format("(%d, %d): %s", y, x, word.charAt(idx))); | ||
boolean isExists = false; | ||
for (int dir = 0; dir < 4; dir++) { | ||
int ny = y + dy[dir]; | ||
int nx = x + dx[dir]; | ||
if (0 <= ny && ny < board.length | ||
&& 0 <= nx && nx < board[0].length | ||
&& !isVisited[ny][nx] | ||
&& word.charAt(idx + 1) == board[ny][nx]) { | ||
isVisited[ny][nx] = true; | ||
isExists = isExists || isWordExists(board, isVisited, word, ny, nx, idx + 1); | ||
isVisited[ny][nx] = false; | ||
} | ||
} | ||
return isExists; | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.