Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions contains-duplicate/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
Problem 217 : contains duplicate
Summary : 주어진 배열에서 중복이 있으면 true, 없으면 false

*/

class Solution {
public boolean containsDuplicate(int[] nums) {
long count = Arrays.stream(nums)
.distinct()
.count();

return count != nums.length;
}
}
22 changes: 22 additions & 0 deletions house-robber/se6816.java
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dp 배열 크기를 nums.length + 1로 하는 것이 아닌 nums.length로 하는 방법으로 하는 것도 해보면 좋을것같습니다👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for문 안에 if문을 작성하고 싶지 않아서 dp[0]이라는 빈 공간을 만들었는데, 지금 보니 뭔가 가독성이 살짝 떨어지고, 실수를 했을 때 발견하기 쉽지 않을 것 같네요. nayeongdev님의 말씀대로, 원 배열 길이를 따르는 형식이 좀 더 좋은 것 같습니다. 좋은 피드백 감사합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
Problem 198 : House Robber
Summary :
- dp를 이용하여, 이전 결과를 저장하면서 문제를 해결한다.

*/

class Solution {
public int rob(int[] nums) {
int[] dp = new int[nums.length+1];
dp[1] = nums[0];
if(nums.length != 1) {
dp[2] = nums[1];
}

for(int i = 2; i < nums.length; i++) {
dp[i+1] = Math.max((nums[i] + dp[i-1]), (nums[i] + dp[i-2]));
}

return Math.max(dp[nums.length], dp[nums.length-1]);
}
}
43 changes: 43 additions & 0 deletions longest-consecutive-sequence/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
Problem 128 : Longest Consecutive Sequence
Summary :
- set에 모든 숫자를 저장한다.
- 매개변수로 주어진 배열을 돌면서 해당 숫자의 이전 숫자가 set에 존재하는지 확인한다.
- 연속적인 숫자 배열의 길이를 구하고, 별도의 set에 기록한다.
- 해당 방법을 사용하면 시간복잡도가 O(N)이 된다.

*/
class Solution {
public int longestConsecutive(int[] nums) {
int max = 0;

Set<Integer> set = new HashSet<>();
for(int num : nums) {
set.add(num);
}
Set<Integer> completeSet = new HashSet<>();
for(int i = 0; i < nums.length; i++) {
if(!set.contains(nums[i]-1) && !completeSet.contains(nums[i])){
int sequence = getSequence(nums[i], set);
max= Math.max(sequence, max);
completeSet.add(nums[i]);
}
}

return max;


}

public int getSequence(int startNum, Set<Integer> numSet) {
int result = 1;
while(true) {
startNum++;
if(!numSet.contains(startNum)) {
break;
}
result++;
}
return result;
}
}
73 changes: 73 additions & 0 deletions top-k-frequent-elements/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
Problem 347 : Top K Frequent Elements
Summary :
- Map을 통해 숫자의 중복 횟수를 구한다.
- 배열을 통해, 중복 횟수를 인덱스로 해당 숫자를 연결 리스트를 통해 값을 저장한다.
- 중복 횟수를 저장한 배열에서 마지막 인덱스부터 for문을 돌리면서 k개까지 리턴 배열에 숫자를 저장한다.
- 제약 조건에서 응답이 유일하다는 것을 보장하고 있으므로, 개수가 부족하거나, 중복에 대해서는 고려하지 않아도 된다.
- 정렬을 이용하면 최소한 시간복잡도가 O(NlogN)이지만 해당 방법은 O(N)이 가능하다.

*/

class Solution {
class Node {
int num;
Node next;
public Node(int n){
num = n;
}
}
class NodeList {
Node head;
Node tail;

public NodeList() {
}

public void add(Node node){
if(head == null){
head = node;
tail = node;
return;
}

tail.next = node;
tail = tail.next;
}

public boolean empty() {
return head == null;
}
}

public int[] topKFrequent(int[] nums, int k) {
NodeList[] list = new NodeList[nums.length+1];
int[] result = new int[k];
Map<Integer, Integer> map= new HashMap<>();

for(int i=0; i < list.length; i++) {
list[i] = new NodeList();
}

for(int i = 0; i < nums.length; i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
}

for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
list[entry.getValue()].add(new Node(entry.getKey()));
}

int idx = 0;
for(int i=list.length-1; (i >= 0) && (idx < k); i--) {
if(!list[i].empty()) {
Node head = list[i].head;
while(head != null) {
result[idx] = head.num;
head = head.next;
idx++;
}
}
}
return result;
}
}
24 changes: 24 additions & 0 deletions two-sum/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
Problem 1 : Two Sum
Summary :
- for문으로 순회하면서, target에서 뺀 값을 저장한다.
- 저장된 값과 일치하는 인덱스를 만나면 해당 값을 리턴한다.
- 기본 For문이 O(N^2)이라면, 해당 방법의 경우 O(N)이 가능하다.

*/

class Solution {

public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> indexMap = new HashMap<>();
int[] result = null;
for(int idx=0; idx<nums.length; idx++) {
if(indexMap.containsKey(nums[idx])) {
result = new int[]{ indexMap.get(nums[idx]), idx };
break;
}
indexMap.put(target-nums[idx], idx);
}
return result;
}
}