Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[donghyeon95] Week 1 #701

Merged
merged 7 commits into from
Dec 14, 2024
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
31 changes: 31 additions & 0 deletions contains-duplicate/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.*;

class Solution {
public boolean containsDuplicate(int[] nums) {
/*
* -- 풀이 --
* nums를 순회하면서 HashSet에 데이터를 넣어서 중복되었는 지 확인한다.
*
* -- 시간 복잡도 --
* 길이 N인 nums를 순환하는데 대한 시간 복잡도 => O(N)
* hashSet의 add에 대한 시간 복잡도 => O(1)
* 전체 시간 복잡도 O(1)*O(N) =O(n)
* ------------------------------------------
*
* -- 공간 복잡도 --
* 길이 N인 nums를 넣을 Hashset이 있어야 하기에 O(N)
* -------------------------------------------
*/

// 중복을 확인할 수 있는 set 선언
HashSet<Integer> hashSet = new HashSet<>();

for (int num: nums) {
// set에 있다면
if (!hashSet.add(num)) return true;
}

return false;
}
}

40 changes: 40 additions & 0 deletions house-robber/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.Arrays;

class Solution {
private int[] dp;

public int rob(int[] nums) {
// 점화식
// f(x) = f(나를 선택) + f(나를 안선택)
// 100개라서 가능은 할 거 같다.

dp = new int[100];

// 0도 가능 하다
Arrays.fill(dp, -1);


return recurse(nums, 0);

}

public int recurse(int[] nums, int index) {
// 종료 조건
if (index >= nums.length) return 0;

// 이미 한번 처리가 되었다면
if (dp[index] != -1) return dp[index];

int result = 0;

// 나를 선택하는 경우,
result = Math.max(recurse(nums, index+2)+nums[index], result);

// 나를 선택하지 않는 경우,
result = Math.max(recurse(nums, index+1), result);
Comment on lines +31 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

재귀로 푸신 방법도 너무 좋아보이네요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

반갑습니다 동훈님.
재귀 말고 반복문으로 푸는 게 성능 상 이점이 있을 거 같은데
시간이 없어서 못 해본 게 아쉽습니다.

리뷰 감사드리고, 3기 화이팅입니다.


dp[index] = result;
return result;
}
}

34 changes: 34 additions & 0 deletions longest-consecutive-sequence/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.HashSet;

public class Solution {
public int longestConsecutive(int[] nums) {
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}

int maxStreak = 0;

for (int num : nums) {
// 내가 시작 값이라면
if (!set.contains(num - 1)) {
int currentNum = num;
int currentStreak = 1;

// 나로부터 연결되는 값을 찾는다.
while (set.contains(currentNum + 1)) {
currentNum++;
currentStreak++;
}

maxStreak = Math.max(maxStreak, currentStreak);
}
}

return maxStreak;
}
}




29 changes: 29 additions & 0 deletions top-k-frequent-elements/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
public int[] topKFrequent(int[] nums, int k) {
HashMap<Integer, Integer> hm = new HashMap<>();

for (int num: nums) {
hm.put(num, hm.getOrDefault(num, 0)+1);
}

List<Map.Entry<Integer, Integer>> list = new ArrayList<>(hm.entrySet());
list.sort(Map.Entry.comparingByValue(Collections.reverseOrder()));

int index = 1;
ArrayList<Integer> answer = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry: list) {
if (index > k) break;
answer.add(entry.getKey());
index++;
}

return answer.stream().mapToInt(Integer::intValue).toArray();
}
}

17 changes: 17 additions & 0 deletions valid-palindrome/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* 첫 시도
leetCode 기준 18ms
*/

class Solution {
public boolean isPalindrome(String s) {
// 1. remove non-alphanumeric using regex
String alphanumeric = s.replaceAll("[^a-zA-Z0-9]", "");

// 2. change lowerCase
String lowerCase = alphanumeric.toLowerCase();

// 3. compare reverse String
return lowerCase.contentEquals(new StringBuffer(lowerCase).reverse());
}
}

Loading