Skip to content

Commit 9078962

Browse files
committed
week01 solved with java
1 parent 53ce7c7 commit 9078962

File tree

5 files changed

+87
-0
lines changed

5 files changed

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

house-robber/std-freejia.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
int numsLen = nums.length;
4+
int[] dp = new int[numsLen];
5+
6+
if (numsLen == 1) return nums[0];
7+
dp[0] = nums[0];
8+
dp[1] = Math.max(nums[0], nums[1]);
9+
10+
for (int i = 2; i < numsLen; i++){
11+
dp[i] = Math.max(dp[i-2] + nums[i], dp[i-1]);
12+
}
13+
return dp[numsLen-1];
14+
}
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/** 시간초과 발생합니다. 해법 찾는 중입니다 */
2+
class Solution {
3+
public int longestConsecutive(int[] nums) {
4+
5+
Set<Integer> uniqueNum = new HashSet<>();
6+
for (int num : nums) { // 수열 내 중복 숫자 제거
7+
uniqueNum.add(num);
8+
}
9+
10+
int uniqueLen = uniqueNum.size();
11+
int longest = 0;
12+
13+
for(int targetNum: uniqueNum){
14+
15+
int n = 1;
16+
for(int j = targetNum + 1; j < targetNum + uniqueLen; j++) {
17+
if (!uniqueNum.contains(j)) break;
18+
n++;
19+
}
20+
longest = Math.max(n, longest);
21+
22+
}
23+
return longest;
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] topKFrequent(int[] nums, int k) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
for (int i = 0; i < nums.length; i++) {
5+
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
6+
}
7+
// value 를 기준으로 정렬
8+
List<Map.Entry<Integer, Integer>> entrySet = new ArrayList<>(map.entrySet());
9+
entrySet.sort((a, b) -> b.getValue().compareTo(a.getValue()));
10+
11+
int[] result = new int[k];
12+
for (int i = 0; i < k; i++) {
13+
14+
result[i] = entrySet.get(i).getKey();
15+
}
16+
return result;
17+
}
18+
}

two-sum/std-freejia.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public static int[] twoSum(int[] nums, int target) {
3+
HashMap<Integer, Integer> map = new HashMap<>();
4+
5+
for (int first = 0; first < nums.length; first++) {
6+
int targetKey = target - nums[first];
7+
if (map.containsKey(targetKey)) {
8+
int second = map.get(targetKey);
9+
return new int[]{second, first};
10+
}
11+
map.put(nums[first], first);
12+
}
13+
14+
return null;
15+
}
16+
}

0 commit comments

Comments
 (0)