Skip to content

[sm9171] Week01 Solutions #1158

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 8 commits into from
Apr 5, 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
13 changes: 13 additions & 0 deletions contains-duplicate/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> set = new HashSet<>();

for (int i = 0; i < nums.length; i++) {
if (set.contains(nums[i])) {
return true;
}
set.add(nums[i]);
Copy link
Member

Choose a reason for hiding this comment

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

add 할 때 존재하는지 확인하고 바로 return 하는 건 어떨까요?

Copy link
Contributor

Choose a reason for hiding this comment

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

wisdon08님이 좋은 의견 남겨주신것에 저도 동의합니다!
현재는 배열을 끝까지 순회한 후에 중복을 판단하는 구조인것 같아서, 중복을 미리 찾고 빠르게 결과 반환을 하는 구조로 변경하는 것도 좋아 보입니다 😀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

두 분 의견 감사합니다. 얘기 해주신대로 얼리 리턴을 하니 17ms에서 12ms로 단축 되었네요 :)

}
return false;
}
}
14 changes: 14 additions & 0 deletions house-robber/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int rob(int[] nums) {
if (nums.length == 0) return 0;
if (nums.length == 1) return nums[0];

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

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

return nums[nums.length - 1];
}
}
20 changes: 20 additions & 0 deletions longest-consecutive-sequence/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public int longestConsecutive(int[] nums) {
int longest = 0;
Set<Integer> set = Arrays.stream(nums)
.boxed()
.collect(Collectors.toSet());

for (Integer i : set) {
if (set.contains(i - 1)) continue;
int length = 1;

while (set.contains(i + 1)) {
length++;
i++;
}
longest = Math.max(longest, length);
}
return longest;
}
}
21 changes: 21 additions & 0 deletions top-k-frequent-elements/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public static int[] topKFrequent(int[] nums, int k) {
Map<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
hashMap.put(num, hashMap.getOrDefault(num, 0) + 1);
}

List<Integer> list = hashMap.entrySet().stream()
.sorted(Map.Entry.<Integer, Integer>comparingByValue().reversed())
.limit(k)
.map(Map.Entry::getKey)
.toList();

int[] result = list.stream()
.mapToInt(Integer::intValue)
.toArray();

return result;
Copy link
Contributor

Choose a reason for hiding this comment

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

위에 List list 부분에서 stream을 사용하신 것 처럼, 반환값도 stream을 활용해보시는 건 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

네 array로 타입 변경하는 부분도 스트림을 이용하여 해결해 보았습니다 :)

}
}
15 changes: 15 additions & 0 deletions two-sum/sm9171.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> hashMap = new HashMap<>();

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