Skip to content

Commit 3e49d66

Browse files
authored
Merge pull request #1724 from sora0319/main
[sora0319] WEEK 01 solutions
2 parents c9f3606 + dcd9377 commit 3e49d66

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

contains-duplicate/sora0319.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
import java.util.*;
2+
// 개선 방향
3+
class Solution {
4+
public boolean containsDuplicate(int[] nums) {
5+
Set duplication = new HashSet<>();
6+
7+
for(int n : nums){
8+
if(duplication.contains(n)){
9+
return true;
10+
}
11+
duplication.add(n);
12+
}
13+
return false;
14+
}
15+
}
16+
17+
18+
// 초기 문제 풀이
219
class Solution {
320
public boolean containsDuplicate(int[] nums) {
421
Arrays.sort(nums);

house-robber/sora0319.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
import java.util.*;
2+
// 2번째 풀이
3+
class Solution {
4+
public int rob(int[] nums) {
5+
int[] dp = new int[nums.length+1];
6+
dp[1] = nums[0];
7+
8+
for(int i = 1; i < nums.length; i++){
9+
dp[i+1] = Math.max(dp[i-1] + nums[i], dp[i]);
10+
}
11+
return dp[nums.length];
12+
}
13+
}
14+
15+
16+
17+
// 1번째 풀이
218
class Solution {
319
public int rob(int[] nums) {
420
int[] house = new int[nums.length];

longest-consecutive-sequence/sora0319.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
import java.util.*;
2+
// 2번째 푼 코드
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
Set<Integer> set = new HashSet<>();
6+
for (int n : nums) {
7+
set.add(n);
8+
}
9+
10+
int cntMax = 0;
11+
for (int n : set) {
12+
if (!set.contains(n - 1)) {
13+
int end = n;
14+
while (set.contains(end + 1)) {
15+
end++;
16+
}
17+
cntMax = Math.max(cntMax, end - n + 1);
18+
}
19+
}
20+
21+
return cntMax;
22+
}
23+
}
224

25+
// 처음 풀어본 코드
326
class Solution {
427
public int longestConsecutive(int[] nums) {
528
Set<Integer> checkList = new HashSet<>();

top-k-frequent-elements/sora0319.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
import java.util.*;
2+
// 다른 방안
3+
class Solution {
4+
public int[] topKFrequent(int[] nums, int k) {
5+
Map <Integer,Integer> counting = new HashMap<>();
6+
7+
for(int n : nums){
8+
if(!counting.containsKey(n)){
9+
counting.put(n,0);
10+
}
11+
counting.put(n, counting.get(n)+1);
12+
}
13+
14+
List<Map.Entry<Integer,Integer>>countList = new LinkedList<>(counting.entrySet());
15+
countList.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));
16+
17+
int[] answer = countList.stream()
18+
.limit(k)
19+
.mapToInt(Map.Entry::getKey)
20+
.toArray();
21+
return answer;
22+
}
23+
}
24+
225

26+
// 초안
327
class Solution {
428
public int[] topKFrequent(int[] nums, int k) {
529
Map<Integer,Integer> counts = new HashMap<>();

two-sum/sora0319.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
import java.util.*;
2+
3+
// 개선안
4+
class Solution {
5+
public int[] twoSum(int[] nums, int target) {
6+
Map<Integer,Integer> checkNums = new HashMap<>();
7+
8+
for(int i = 0; i < nums.length; i++){
9+
if(checkNums.containsKey(target - nums[i])){
10+
int place = checkNums.get(target - nums[i]);
11+
return new int[]{place, i};
12+
}
13+
checkNums.put(nums[i], i);
14+
}
15+
return new int[]{};
16+
}
17+
}
18+
19+
20+
// 초기 구성안
221
class Solution {
322
public int[] twoSum(int[] nums, int target) {
423
Map<Integer,Integer> element = new HashMap<>();

0 commit comments

Comments
 (0)