-
-
Notifications
You must be signed in to change notification settings - Fork 248
[eunhwa99] week 1 #672
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
[eunhwa99] week 1 #672
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
39be389
contains duplicate solution
ff39fde
valid palindrome solution
58e2901
Merge branch 'DaleStudy:main' into main
eunhwa99 32ed2ba
top k frequent
da9d2bf
줄바꿈 추가
898fa6f
Longest Consecutive Sequence
378a096
House Robber
4c8241e
Add line
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,14 @@ | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
HashSet<Integer> seen = new HashSet<>(); | ||
for (int num : nums) { | ||
if (!seen.add(num)) { | ||
return 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,24 @@ | ||
import java.util.Arrays; | ||
|
||
class Solution { | ||
int size = 0; | ||
int[] numArray; | ||
int[] dp; | ||
|
||
public int rob(int[] nums) { | ||
size = nums.length; | ||
dp = new int[size]; | ||
// 배열의 모든 값을 -1로 변경 | ||
Arrays.fill(dp, -1); | ||
numArray = nums; | ||
return fun(0); | ||
} | ||
|
||
private int fun(int idx) { | ||
if (idx >= size) return 0; | ||
if (dp[idx] != -1) return dp[idx]; | ||
dp[idx] = 0; // check | ||
dp[idx] += Math.max(fun(idx + 2) + numArray[idx], fun(idx + 1)); | ||
return dp[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,23 @@ | ||
import java.util.HashSet; | ||
|
||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
HashSet<Integer> mySet = new HashSet<Integer>(); | ||
|
||
for (int num : nums) { | ||
mySet.add(num); | ||
} | ||
|
||
int result = 0; | ||
for (int num : mySet) { | ||
int cnt = 1; | ||
if (!mySet.contains(num - 1)) { | ||
while (mySet.contains(++num)) { | ||
++cnt; | ||
} | ||
result = Math.max(cnt, result); | ||
} | ||
} | ||
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,16 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
class Solution { | ||
public static int[] topKFrequent(int[] nums, int k) { | ||
Map<Integer, Integer> myMap = new HashMap<>(); | ||
for (int num : nums) { | ||
myMap.put(num, myMap.getOrDefault(num, 0) + 1); | ||
} | ||
return myMap.entrySet() | ||
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.stream() | ||
.sorted((v1, v2) -> Integer.compare(v2.getValue(),v1.getValue())) | ||
.map(Map.Entry::getKey) | ||
.mapToInt(Integer::intValue) | ||
.toArray(); | ||
} | ||
} |
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 @@ | ||
class Solution { | ||
public boolean isPalindrome(String s) { | ||
StringBuilder str = new StringBuilder(); | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
char c = s.charAt(i); | ||
if (Character.isLetterOrDigit(c)) { | ||
str.append(Character.toLowerCase(c)); | ||
} | ||
} | ||
|
||
int left = 0, right = str.length() - 1; | ||
while (left < right) { | ||
if (str.charAt(left) != str.charAt(right)) { | ||
return false; | ||
} | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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.