Skip to content

[mumunuu] WEEK 01 solutions #1710

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 6 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
28 changes: 28 additions & 0 deletions contains-duplicate/mumunuu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.HashSet;
import java.util.Set;


class Solution {

/**
*
* Set 판단으로 O(n)
*
* */
public boolean containsDuplicate(int[] nums) {

Set<Integer> set = new HashSet<>();

for (int num : nums) {

boolean isAdded = set.add(num);
Copy link
Contributor

Choose a reason for hiding this comment

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

set.add 의 반환 값을 이용해서 확인하셨군요! ㅎㅎ


if (!isAdded) {
return true;
}
}

return false;

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

class Solution {


// priority queue 로 풀어보기
public int[] topKFrequent(int[] nums, int k) {

// 빈도수를 셈
Map<Integer, Integer> freqMap = new HashMap<>();
for (int num : nums) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}

// 갯수만큼 배열 생성 (빈도수를 인덱스로 가지는)
List<Integer>[] bucket = new List[nums.length + 1]; // freq는 최대 nums.length
for (int i = 0; i < bucket.length; i++) {
bucket[i] = new ArrayList<>();
}

for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
int num = entry.getKey();
int freq = entry.getValue();
bucket[freq].add(num);
}

// 빈도수가 높은 뒤에서부터 넣어줌
List<Integer> result = new ArrayList<>();
for (int i = bucket.length - 1; i >= 0 && result.size() < k; i--) {
if (!bucket[i].isEmpty()) {
result.addAll(bucket[i]);
}
}

// k개만 반환
return result.subList(0, k).stream().mapToInt(i -> i).toArray();
}
}
46 changes: 46 additions & 0 deletions two-sum/mumunuu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.HashMap;
import java.util.Map;

class Solution {
/**
* 배열의 요소 2개를 더해서 target 숫자들을 만들고 인덱스를 반환
* 같은 숫자를 여러번 사용할 수 없고, 해답은 반드시 존재함
* Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity?
*/

/*
// naive 한 풀이법. O(n^2)
public int[] twoSum(int[] nums, int target) {
// naive 한 방법: 나를 제외한 나머지 숫자가 있는지 찾음
for (int i=0; i<nums.length-1; i++) {
for (int k=i+1; k<nums.length; k++) {
if (nums[i] + nums[k] == target) {
return new int[]{i,k};
}
}
}
return new int[]{};
}
*/


// 처음에 Map<Integer, List<Integer>>라고 생각했지만 그냥 Integer, Integer로 가능함
public int[] twoSum(int[] nums, int target) {

Map<Integer, Integer> map = new HashMap<>();

for (int i = 0; i < nums.length; i++) {

int needValue = target - nums[i];
if (map.containsKey(needValue)) {
return new int[]{map.get(needValue), i};
}
map.put(nums[i], i); // 항상 현재 인덱스를 나중에 저장

}

return new int[]{}; // 절대 도달하지 않음 (문제 조건상 정답이 항상 존재)

}

}