-
-
Notifications
You must be signed in to change notification settings - Fork 245
[YoungSeok-Choi] Week 1 Solutions #1163
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,19 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
// 시간복잡도 O(n) | ||
public boolean containsDuplicate(int[] nums) { | ||
Map<Integer, Boolean> dupMap = new HashMap<>(); | ||
|
||
for(int i = 0; i < nums.length; i++) { | ||
if(dupMap.containsKey(nums[i])) { | ||
return true; | ||
} | ||
|
||
dupMap.put(nums[i], true); | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,84 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
// 시간복잡도: O(n) | ||
// TODO: DP 방식으로 풀어보기 | ||
class Solution { | ||
public Map<Integer, Integer> robMap = new HashMap<>(); | ||
public int rob(int[] nums) { | ||
return dfs(nums, 0); | ||
} | ||
|
||
public int dfs(int[] nums, int index) { | ||
if(nums.length == 0) { | ||
return 0; | ||
} | ||
|
||
if(index >= nums.length) { | ||
return 0; | ||
} | ||
|
||
// 이미 털었던 집이라면, 해 | ||
if(robMap.containsKey(index)) { | ||
return robMap.get(index); | ||
} | ||
|
||
// 이번 집을 털게되는 경우 | ||
int robThis = nums[index] + dfs(nums, index + 2); | ||
|
||
// 이번 집을 털지않고 건너뛰는 경우,. | ||
int skipThis = dfs(nums, index + 1); | ||
|
||
robMap.put(index, Math.max(robThis, skipThis)); | ||
|
||
return robMap.get(index); | ||
} | ||
} | ||
|
||
// TODO: 비효율적으로 작성한 알고리즘의 동작 방식을 도식화 해서 그려보기. | ||
// NOTE: dfs를 사용한 완전탐색 | ||
// 탐색 방식이 매우 비효율적이라, 정답은 맞추지만 N이 커지면 시간초과 | ||
// 시간복잡도: O(2^n) + alpha(중복탐색) | ||
class WrongSolution { | ||
public boolean[] visit; | ||
public int mx = -987654321; | ||
public int curSum = 0; | ||
|
||
public int rob(int[] nums) { | ||
if(nums.length == 1) { | ||
return nums[0]; | ||
} | ||
|
||
visit = new boolean[nums.length]; | ||
dfs(nums, 0); | ||
dfs(nums, 1); | ||
|
||
return mx; | ||
} | ||
|
||
public void dfs(int[] arr, int idx) { | ||
int len = arr.length; | ||
int prevIdx = idx - 1; | ||
int nextIdx = idx + 1; | ||
|
||
|
||
if(idx == 0) { | ||
if(visit[idx]) return; | ||
} else { | ||
if(idx >= len || visit[idx] || visit[prevIdx]) { | ||
return; | ||
} | ||
} | ||
|
||
visit[idx] = true; | ||
curSum += arr[idx]; | ||
mx = Math.max(mx, curSum); | ||
|
||
for(int i = idx; i < len; i++) { | ||
dfs(arr, i); | ||
} | ||
|
||
visit[idx] = false; | ||
curSum -= arr[idx]; | ||
} | ||
} |
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,34 @@ | ||
import java.util.Arrays; | ||
|
||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
int curSeq = 1; | ||
int maxSeq = -987654321; | ||
|
||
if(nums.length == 0) { | ||
return 0; | ||
} | ||
|
||
Arrays.sort(nums); | ||
|
||
int cur = nums[0]; | ||
for(int i = 1; i < nums.length; i++) { | ||
if(cur == nums[i]) { | ||
continue; | ||
} | ||
|
||
if(cur < nums[i] && Math.abs(nums[i] - cur) == 1) { | ||
curSeq++; | ||
cur = nums[i]; | ||
continue; | ||
} | ||
|
||
// NOTE: 수열의 조건이 깨졌을 때 | ||
maxSeq = Math.max(maxSeq, curSeq); | ||
curSeq = 1; | ||
cur = nums[i]; | ||
} | ||
|
||
return Math.max(maxSeq, curSeq); | ||
} | ||
} |
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,47 @@ | ||
class Solution { | ||
// 시간복잡도: O(3n) -> O(n) | ||
public int[] productExceptSelf(int[] nums) { | ||
|
||
int zeroCount = 0; | ||
int[] result = new int[nums.length]; | ||
int productExceptZero = 1; | ||
int zeroIdx = 0; | ||
|
||
for(int i = 0; i < nums.length; i++) { | ||
if(nums[i] == 0) { | ||
zeroCount++; | ||
} | ||
} | ||
|
||
// NOTE: 0이 두개 이상일 때, 모든 배열의 원소가 0; | ||
if(zeroCount >= 2) { | ||
return result; | ||
} | ||
|
||
// NOTE: 0이 1개일 때, 0인 index만을 제외하고 모두 곱 | ||
if(zeroCount == 1) { | ||
for(int i = 0; i < nums.length; i++) { | ||
if(nums[i] == 0) { | ||
zeroIdx = i; | ||
continue; | ||
} | ||
productExceptZero *= nums[i]; | ||
} | ||
|
||
result[zeroIdx] = productExceptZero; | ||
return result; | ||
} | ||
|
||
// NOTE: 0이 없을 때 모든수를 곱한 뒤 idx를 나누기. | ||
for(int i = 0; i < nums.length; i++) { | ||
productExceptZero *= nums[i]; | ||
} | ||
|
||
for(int i = 0; i < nums.length; i++) { | ||
int copy = productExceptZero; | ||
result[i] = copy / nums[i]; | ||
} | ||
|
||
return result; | ||
} | ||
} |
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,23 @@ | ||
// time complexity: O(n); | ||
// 특정 nums[i] 가 target이 되기위한 보수 (target - nums[i])를 candidate Map에서 찾으면 종료 | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
Map<Integer, Integer> candidate = new HashMap<>(); | ||
|
||
for(int i = 0; i < nums.length; i++) { | ||
|
||
int diff = target - nums[i]; | ||
|
||
if(candidate.containsKey(diff)) { | ||
return new int[] { candidate.get(diff), i }; | ||
} | ||
|
||
candidate.put(nums[i], i); | ||
} | ||
return new int[0]; | ||
} | ||
} | ||
YoungSeok-Choi marked this conversation as resolved.
Show resolved
Hide resolved
|
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.