Skip to content

[sora0319] WEEK 01 solutions #1724

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

Merged
merged 5 commits into from
Jul 27, 2025
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
17 changes: 17 additions & 0 deletions contains-duplicate/sora0319.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import java.util.*;
// 개선 방향
class Solution {
public boolean containsDuplicate(int[] nums) {
Set duplication = new HashSet<>();

for(int n : nums){
if(duplication.contains(n)){
return true;
}
duplication.add(n);
}
return false;
}
}


// 초기 문제 풀이
class Solution {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
Expand Down
16 changes: 16 additions & 0 deletions house-robber/sora0319.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import java.util.*;
// 2번째 풀이
class Solution {
public int rob(int[] nums) {
int[] dp = new int[nums.length+1];
dp[1] = nums[0];

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



// 1번째 풀이
class Solution {
public int rob(int[] nums) {
int[] house = new int[nums.length];
Expand Down
23 changes: 23 additions & 0 deletions longest-consecutive-sequence/sora0319.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import java.util.*;
// 2번째 푼 코드
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int n : nums) {
set.add(n);
}

int cntMax = 0;
for (int n : set) {
if (!set.contains(n - 1)) {
int end = n;
while (set.contains(end + 1)) {
end++;
}
cntMax = Math.max(cntMax, end - n + 1);
}
}

return cntMax;
}
}

// 처음 풀어본 코드
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> checkList = new HashSet<>();
Expand Down
24 changes: 24 additions & 0 deletions top-k-frequent-elements/sora0319.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import java.util.*;
// 다른 방안
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map <Integer,Integer> counting = new HashMap<>();

for(int n : nums){
if(!counting.containsKey(n)){
counting.put(n,0);
}
counting.put(n, counting.get(n)+1);
}

List<Map.Entry<Integer,Integer>>countList = new LinkedList<>(counting.entrySet());
countList.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));

int[] answer = countList.stream()
.limit(k)
.mapToInt(Map.Entry::getKey)
.toArray();
return answer;
}
}


// 초안
class Solution {
public int[] topKFrequent(int[] nums, int k) {
Map<Integer,Integer> counts = new HashMap<>();
Expand Down
19 changes: 19 additions & 0 deletions two-sum/sora0319.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import java.util.*;

// 개선안
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> checkNums = new HashMap<>();

for(int i = 0; i < nums.length; i++){
if(checkNums.containsKey(target - nums[i])){
int place = checkNums.get(target - nums[i]);
return new int[]{place, i};
}
checkNums.put(nums[i], i);
}
return new int[]{};
}
}


// 초기 구성안
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> element = new HashMap<>();
Expand Down