Skip to content
Open
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
24 changes: 24 additions & 0 deletions contains-duplicate/dohyeon2.java
Copy link
Contributor

Choose a reason for hiding this comment

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

worst case 시간 복잡도에 대해서 생각해본적이 없었는데 리뷰하면서 공부할 기회가 되었습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.HashSet;

class Solution {
// The problem is to check if there are any duplicate elements in the array.
// So, I decided to use HashSet because it has O(1) time complexity for add and contains operations which are good to use for checking duplicates.
public boolean containsDuplicate(int[] nums) {
// the element type of the array is int, so create Integer HashSet
// O(n) space complexity
HashSet<Integer> exists = new HashSet<Integer>();
// the nums array has length up to 10^5, so use int type
// O(n) time complexity average
// worst case: O(n log n) if many elements hash to the same bucket
for(int i = 0; i < nums.length; i++){
int num = nums[i];

if(exists.contains(num)){
return true;
}else{
exists.add(num);
}
}
return false;
}
}
30 changes: 30 additions & 0 deletions house-robber/dohyeon2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public int rob(int[] nums) {
// Dynamic Planning
// Time Complexity: O(n)
// Space Complexity: O(n) for the dp array
// When applying DP, it is important to define the accumulated state clearly.

if (nums.length == 1) {
return nums[0];
}

// The state of dp is maximum amount of robbery at i
int[] dp = new int[nums.length];

dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);

for (int i = 2; i < nums.length; i++) {
dp[i] = Math.max(
// If we skip the current house,
// we can take the maximum amount of robbery at i - 1
dp[i - 1],
// If we rob the current house, we cannot rob the previous house
// so we can take the maximum amount of robbery at i - 2
dp[i - 2] + nums[i]);
}

return dp[nums.length - 1];
}
}
79 changes: 79 additions & 0 deletions longest-consecutive-sequence/dohyeon2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.util.HashSet;

class Solution {
// This solution was inspired by:
// https://www.algodale.com/problems/longest-consecutive-sequence/
//
// I initially believed this algorithm would run in O(n) time,
Copy link
Contributor

Choose a reason for hiding this comment

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

사고흐름을 상세하게 기술해놓는 것 정말 좋은 것 같습니다!

// but it resulted in a Time Limit Exceeded error.
//
// Although the expected time complexity is O(n),
// repeatedly calling set.iterator().next() might introduce overhead.
// Iterating through the set using a for-loop may be a better approach.
//
// In this case, it seems preferable to follow the approach described here:
// https://www.algodale.com/problems/longest-consecutive-sequence/#%ED%92%80%EC%9D%B4-3

public int oldApproach(int[] nums) {
int count = 0;
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}

while (set.size() > 0) {
int buffer = 1;
// This may cause a Time Limit Exceeded error.
Integer curr = set.iterator().next();
set.remove(curr);
Integer next = curr + 1;
Integer prev = curr - 1;

while (set.contains(next)) {
set.remove(next);
next++;
buffer++;
}

while (set.contains(prev)) {
set.remove(prev);
prev--;
buffer++;
}

count = Math.max(count, buffer);
}

return count;
}

public int longestConsecutive(int[] nums) {
int count = 0;

// The Set has O(n) space complexity,
// because it may store up to n elements in memory.
// Is this the correct way to evaluate space complexity?
Copy link
Contributor

Choose a reason for hiding this comment

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

넵! 지금 분석하신 것처럼 주어진 것에서 추가적으로 사용된 공간에 대해서 분석하시면 됩니다!

HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}

for (int num : set) {
if (set.contains(num - 1)) {
continue;
}

int currentNum = num;
int currentCount = 1;

while (set.contains(currentNum + 1)) {
currentNum++;
currentCount++;
}

count = Math.max(count, currentCount);
}

return count;
}
}
47 changes: 47 additions & 0 deletions top-k-frequent-elements/dohyeon2.java
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM!

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.HashMap;
import java.util.ArrayList;

class Solution {
public int[] topKFrequent(int[] nums, int k) {
// time complexity O(n)
// space complexity O(n)

HashMap<Integer, Integer> frequentMap = new HashMap<>();

for (int i = 0; i < nums.length; i++) {
int num = nums[i];
frequentMap.merge(num, 1, Integer::sum);
}

ArrayList<Integer>[] buckets = new ArrayList[nums.length + 1];

for (int i = 0; i <= nums.length; i++) {
buckets[i] = new ArrayList<>();
}

frequentMap.forEach((Integer a, Integer b) -> {
// Assign the largest values from the front of the array
buckets[nums.length - b].add(a);
});

int[] result = new int[k];

int pointer = 0;

for (int i = 0; i < buckets.length; i++) {
ArrayList<Integer> list = buckets[i];
if (list.size() == 0) {
continue;
}
for (Integer num : list) {
result[pointer] = (int) num;
// Return the result when the pointer reaches k
if (pointer == (k - 1))
return result;
pointer++;
}
}

return result;
}
}
32 changes: 32 additions & 0 deletions two-sum/dohyeon2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.HashMap;

class Solution {
public int[] twoSum(int[] nums, int target) {
// Approach : using HashMap to get index with the element in O(n) time
// complexity
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick : 이부분은 time complexity 라고 명시해주는게 정확할 것 같습니다!

Copy link
Author

Choose a reason for hiding this comment

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

넵 줄바꿈이 조금 이상하게 적용되어있네요
해당부분도 신경써보겠습니다
감사합니다.

// SpaceComplexity is also O(n)
HashMap<Integer, Integer> numIndexMap = new HashMap<Integer, Integer>();

// Make key and value HashMap
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
numIndexMap.put(num, i);
}

// Search for the other operand looping nums
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
int operand = target - num;
Integer index = numIndexMap.get(operand);
boolean indexExists = index != null;
boolean indexExistsAndIndexIsNotTheNum = indexExists && i != index;
if (indexExistsAndIndexIsNotTheNum) {
return new int[] { i, index };
}
}

// If the valid answer is always exists this is not needed
// But the compiler don't know about that
return new int[2];
}
}