-
Notifications
You must be signed in to change notification settings - Fork 126
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
[donghyeon95] Week 1 #701
Merged
Merged
[donghyeon95] Week 1 #701
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
afd0af8
add solution: Contains Duplicate #217
bcb35c2
add solution: Valid Palindrome #220
fa9d506
fix: 개행 문자 추가
f7bf00a
feat: Top K Frequent Elements #237
27d89c9
feat: Longest Consecutive Sequence
a41297f
feat: House Robber #264
5487b35
Merge branch 'DaleStudy:main' into main
donghyeon95 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 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 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
/* | ||
* -- 풀이 -- | ||
* nums를 순회하면서 HashSet에 데이터를 넣어서 중복되었는 지 확인한다. | ||
* | ||
* -- 시간 복잡도 -- | ||
* 길이 N인 nums를 순환하는데 대한 시간 복잡도 => O(N) | ||
* hashSet의 add에 대한 시간 복잡도 => O(1) | ||
* 전체 시간 복잡도 O(1)*O(N) =O(n) | ||
* ------------------------------------------ | ||
* | ||
* -- 공간 복잡도 -- | ||
* 길이 N인 nums를 넣을 Hashset이 있어야 하기에 O(N) | ||
* ------------------------------------------- | ||
*/ | ||
|
||
// 중복을 확인할 수 있는 set 선언 | ||
HashSet<Integer> hashSet = new HashSet<>(); | ||
|
||
for (int num: nums) { | ||
// set에 있다면 | ||
if (!hashSet.add(num)) return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
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,40 @@ | ||
import java.util.Arrays; | ||
|
||
class Solution { | ||
private int[] dp; | ||
|
||
public int rob(int[] nums) { | ||
// 점화식 | ||
// f(x) = f(나를 선택) + f(나를 안선택) | ||
// 100개라서 가능은 할 거 같다. | ||
|
||
dp = new int[100]; | ||
|
||
// 0도 가능 하다 | ||
Arrays.fill(dp, -1); | ||
|
||
|
||
return recurse(nums, 0); | ||
|
||
} | ||
|
||
public int recurse(int[] nums, int index) { | ||
// 종료 조건 | ||
if (index >= nums.length) return 0; | ||
|
||
// 이미 한번 처리가 되었다면 | ||
if (dp[index] != -1) return dp[index]; | ||
|
||
int result = 0; | ||
|
||
// 나를 선택하는 경우, | ||
result = Math.max(recurse(nums, index+2)+nums[index], result); | ||
|
||
// 나를 선택하지 않는 경우, | ||
result = Math.max(recurse(nums, index+1), result); | ||
|
||
dp[index] = result; | ||
return result; | ||
} | ||
} | ||
|
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,34 @@ | ||
import java.util.HashSet; | ||
|
||
public class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
HashSet<Integer> set = new HashSet<>(); | ||
for (int num : nums) { | ||
set.add(num); | ||
} | ||
|
||
int maxStreak = 0; | ||
|
||
for (int num : nums) { | ||
// 내가 시작 값이라면 | ||
if (!set.contains(num - 1)) { | ||
int currentNum = num; | ||
int currentStreak = 1; | ||
|
||
// 나로부터 연결되는 값을 찾는다. | ||
while (set.contains(currentNum + 1)) { | ||
currentNum++; | ||
currentStreak++; | ||
} | ||
|
||
maxStreak = Math.max(maxStreak, currentStreak); | ||
} | ||
} | ||
|
||
return maxStreak; | ||
} | ||
} | ||
|
||
|
||
|
||
|
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 @@ | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
public int[] topKFrequent(int[] nums, int k) { | ||
HashMap<Integer, Integer> hm = new HashMap<>(); | ||
|
||
for (int num: nums) { | ||
hm.put(num, hm.getOrDefault(num, 0)+1); | ||
} | ||
|
||
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(hm.entrySet()); | ||
list.sort(Map.Entry.comparingByValue(Collections.reverseOrder())); | ||
|
||
int index = 1; | ||
ArrayList<Integer> answer = new ArrayList<>(); | ||
for (Map.Entry<Integer, Integer> entry: list) { | ||
if (index > k) break; | ||
answer.add(entry.getKey()); | ||
index++; | ||
} | ||
|
||
return answer.stream().mapToInt(Integer::intValue).toArray(); | ||
} | ||
} | ||
|
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,17 @@ | ||
/* 첫 시도 | ||
leetCode 기준 18ms | ||
*/ | ||
|
||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
// 1. remove non-alphanumeric using regex | ||
String alphanumeric = s.replaceAll("[^a-zA-Z0-9]", ""); | ||
|
||
// 2. change lowerCase | ||
String lowerCase = alphanumeric.toLowerCase(); | ||
|
||
// 3. compare reverse String | ||
return lowerCase.contentEquals(new StringBuffer(lowerCase).reverse()); | ||
} | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
재귀로 푸신 방법도 너무 좋아보이네요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
반갑습니다 동훈님.
재귀 말고 반복문으로 푸는 게 성능 상 이점이 있을 거 같은데
시간이 없어서 못 해본 게 아쉽습니다.
리뷰 감사드리고, 3기 화이팅입니다.