Skip to content

Commit 1230e6b

Browse files
authored
Merge pull request #672 from eunhwa99/main
[eunhwa99] week 1
2 parents dcb6bb6 + 4c8241e commit 1230e6b

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

contains-duplicate/eunhwa99.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public boolean containsDuplicate(int[] nums) {
3+
HashSet<Integer> seen = new HashSet<>();
4+
for (int num : nums) {
5+
if (!seen.add(num)) {
6+
return true;
7+
}
8+
}
9+
10+
return false;
11+
12+
13+
}
14+
}

house-robber/eunhwa99.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
int size = 0;
5+
int[] numArray;
6+
int[] dp;
7+
8+
public int rob(int[] nums) {
9+
size = nums.length;
10+
dp = new int[size];
11+
// 배열의 모든 값을 -1로 변경
12+
Arrays.fill(dp, -1);
13+
numArray = nums;
14+
return fun(0);
15+
}
16+
17+
private int fun(int idx) {
18+
if (idx >= size) return 0;
19+
if (dp[idx] != -1) return dp[idx];
20+
dp[idx] = 0; // check
21+
dp[idx] += Math.max(fun(idx + 2) + numArray[idx], fun(idx + 1));
22+
return dp[idx];
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
HashSet<Integer> mySet = new HashSet<Integer>();
6+
7+
for (int num : nums) {
8+
mySet.add(num);
9+
}
10+
11+
int result = 0;
12+
for (int num : mySet) {
13+
int cnt = 1;
14+
if (!mySet.contains(num - 1)) {
15+
while (mySet.contains(++num)) {
16+
++cnt;
17+
}
18+
result = Math.max(cnt, result);
19+
}
20+
}
21+
return result;
22+
}
23+
}

top-k-frequent-elements/eunhwa99.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
class Solution {
4+
public static int[] topKFrequent(int[] nums, int k) {
5+
Map<Integer, Integer> myMap = new HashMap<>();
6+
for (int num : nums) {
7+
myMap.put(num, myMap.getOrDefault(num, 0) + 1);
8+
}
9+
return myMap.entrySet()
10+
.stream()
11+
.sorted((v1, v2) -> Integer.compare(v2.getValue(),v1.getValue()))
12+
.map(Map.Entry::getKey)
13+
.mapToInt(Integer::intValue)
14+
.toArray();
15+
}
16+
}

valid-palindrome/eunhwa99.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
StringBuilder str = new StringBuilder();
4+
5+
for (int i = 0; i < s.length(); i++) {
6+
char c = s.charAt(i);
7+
if (Character.isLetterOrDigit(c)) {
8+
str.append(Character.toLowerCase(c));
9+
}
10+
}
11+
12+
int left = 0, right = str.length() - 1;
13+
while (left < right) {
14+
if (str.charAt(left) != str.charAt(right)) {
15+
return false;
16+
}
17+
left++;
18+
right--;
19+
}
20+
21+
return true;
22+
}
23+
}

0 commit comments

Comments
 (0)