Skip to content

Commit 71233d5

Browse files
authored
Merge pull request #1996 from se6816/main
[se6816] WEEK 01 solutions
2 parents 3f383db + 51847b2 commit 71233d5

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

contains-duplicate/se6816.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
Problem 217 : contains duplicate
3+
Summary : 주어진 배열에서 중복이 있으면 true, 없으면 false
4+
5+
*/
6+
7+
class Solution {
8+
public boolean containsDuplicate(int[] nums) {
9+
long count = Arrays.stream(nums)
10+
.distinct()
11+
.count();
12+
13+
return count != nums.length;
14+
}
15+
}

house-robber/se6816.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
Problem 198 : House Robber
3+
Summary :
4+
- dp를 이용하여, 이전 결과를 저장하면서 문제를 해결한다.
5+
6+
*/
7+
8+
class Solution {
9+
public int rob(int[] nums) {
10+
int[] dp = new int[nums.length+1];
11+
dp[1] = nums[0];
12+
if(nums.length != 1) {
13+
dp[2] = nums[1];
14+
}
15+
16+
for(int i = 2; i < nums.length; i++) {
17+
dp[i+1] = Math.max((nums[i] + dp[i-1]), (nums[i] + dp[i-2]));
18+
}
19+
20+
return Math.max(dp[nums.length], dp[nums.length-1]);
21+
}
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
Problem 128 : Longest Consecutive Sequence
3+
Summary :
4+
- set에 모든 숫자를 저장한다.
5+
- 매개변수로 주어진 배열을 돌면서 해당 숫자의 이전 숫자가 set에 존재하는지 확인한다.
6+
- 연속적인 숫자 배열의 길이를 구하고, 별도의 set에 기록한다.
7+
- 해당 방법을 사용하면 시간복잡도가 O(N)이 된다.
8+
9+
*/
10+
class Solution {
11+
public int longestConsecutive(int[] nums) {
12+
int max = 0;
13+
14+
Set<Integer> set = new HashSet<>();
15+
for(int num : nums) {
16+
set.add(num);
17+
}
18+
Set<Integer> completeSet = new HashSet<>();
19+
for(int i = 0; i < nums.length; i++) {
20+
if(!set.contains(nums[i]-1) && !completeSet.contains(nums[i])){
21+
int sequence = getSequence(nums[i], set);
22+
max= Math.max(sequence, max);
23+
completeSet.add(nums[i]);
24+
}
25+
}
26+
27+
return max;
28+
29+
30+
}
31+
32+
public int getSequence(int startNum, Set<Integer> numSet) {
33+
int result = 1;
34+
while(true) {
35+
startNum++;
36+
if(!numSet.contains(startNum)) {
37+
break;
38+
}
39+
result++;
40+
}
41+
return result;
42+
}
43+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
Problem 347 : Top K Frequent Elements
3+
Summary :
4+
- Map을 통해 숫자의 중복 횟수를 구한다.
5+
- 배열을 통해, 중복 횟수를 인덱스로 해당 숫자를 연결 리스트를 통해 값을 저장한다.
6+
- 중복 횟수를 저장한 배열에서 마지막 인덱스부터 for문을 돌리면서 k개까지 리턴 배열에 숫자를 저장한다.
7+
- 제약 조건에서 응답이 유일하다는 것을 보장하고 있으므로, 개수가 부족하거나, 중복에 대해서는 고려하지 않아도 된다.
8+
- 정렬을 이용하면 최소한 시간복잡도가 O(NlogN)이지만 해당 방법은 O(N)이 가능하다.
9+
10+
*/
11+
12+
class Solution {
13+
class Node {
14+
int num;
15+
Node next;
16+
public Node(int n){
17+
num = n;
18+
}
19+
}
20+
class NodeList {
21+
Node head;
22+
Node tail;
23+
24+
public NodeList() {
25+
}
26+
27+
public void add(Node node){
28+
if(head == null){
29+
head = node;
30+
tail = node;
31+
return;
32+
}
33+
34+
tail.next = node;
35+
tail = tail.next;
36+
}
37+
38+
public boolean empty() {
39+
return head == null;
40+
}
41+
}
42+
43+
public int[] topKFrequent(int[] nums, int k) {
44+
NodeList[] list = new NodeList[nums.length+1];
45+
int[] result = new int[k];
46+
Map<Integer, Integer> map= new HashMap<>();
47+
48+
for(int i=0; i < list.length; i++) {
49+
list[i] = new NodeList();
50+
}
51+
52+
for(int i = 0; i < nums.length; i++) {
53+
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
54+
}
55+
56+
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
57+
list[entry.getValue()].add(new Node(entry.getKey()));
58+
}
59+
60+
int idx = 0;
61+
for(int i=list.length-1; (i >= 0) && (idx < k); i--) {
62+
if(!list[i].empty()) {
63+
Node head = list[i].head;
64+
while(head != null) {
65+
result[idx] = head.num;
66+
head = head.next;
67+
idx++;
68+
}
69+
}
70+
}
71+
return result;
72+
}
73+
}

two-sum/se6816.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
Problem 1 : Two Sum
3+
Summary :
4+
- for문으로 순회하면서, target에서 뺀 값을 저장한다.
5+
- 저장된 값과 일치하는 인덱스를 만나면 해당 값을 리턴한다.
6+
- 기본 For문이 O(N^2)이라면, 해당 방법의 경우 O(N)이 가능하다.
7+
8+
*/
9+
10+
class Solution {
11+
12+
public int[] twoSum(int[] nums, int target) {
13+
Map<Integer, Integer> indexMap = new HashMap<>();
14+
int[] result = null;
15+
for(int idx=0; idx<nums.length; idx++) {
16+
if(indexMap.containsKey(nums[idx])) {
17+
result = new int[]{ indexMap.get(nums[idx]), idx };
18+
break;
19+
}
20+
indexMap.put(target-nums[idx], idx);
21+
}
22+
return result;
23+
}
24+
}

0 commit comments

Comments
 (0)