Skip to content

[mintheon] Week 1 #633

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

class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> numSet = new HashSet();

for(int num : nums) {
numSet.add(num);
}

return numSet.size() != nums.length;
}
}
28 changes: 28 additions & 0 deletions house-robber/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int rob(int[] nums) {
int[] sums = new int[nums.length];

sums[0] = nums[0];

if (nums.length > 1) {
sums[1] = nums[1];
}

if (nums.length > 2) {
sums[2] = nums[0] + nums[2];
}

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

int max = 0;
for(int sum : sums) {
max = Math.max(sum, max);
}

return max;
}
}
30 changes: 30 additions & 0 deletions longest-consecutive-sequence/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.HashSet;
import java.util.Set;

class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> numSet = new HashSet<>();

for(int num : nums) {
numSet.add(num);
}

int longestSize = 0;

for(int num : numSet) {
if(!numSet.contains(num - 1)) {
int current = num;
int count = 1;

while(numSet.contains(current + 1)) {
count++;
current++;
}

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

return longestSize;
}
}
24 changes: 24 additions & 0 deletions top-k-frequent-elements/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;

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

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

PriorityQueue<Entry<Integer, Integer>> pq = new PriorityQueue<>((a, b) -> b.getValue().compareTo(a.getValue()));
pq.addAll(frequent.entrySet());
Comment on lines +15 to +16
Copy link
Member

Choose a reason for hiding this comment

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

가장 큰 수 k개만 필요한데 반드시 모든 숫자를 정렬해야할까를 고민해보시면 보다 효율적인 알고리즘을 얻으실 수도 있을 것 같습니다.

Copy link
Member Author

Choose a reason for hiding this comment

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

가장 큰 수 k개만 필요한데 반드시 모든 숫자를 정렬해야할까를 고민해보시면 보다 효율적인 알고리즘을 얻으실 수도 있을 것 같습니다.

올려주신 풀이의 배열 방법은 생각치 못했어요.
다만 자바로 변환해서 푸니 List<List<>> 방식이 되어 코드가 복잡해지는 경향이 있는것 같아요. 그래도 속도는 확실하네요!! 👍🏻


Copy link
Contributor

Choose a reason for hiding this comment

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

PriortyQueue랑 정렬은 시간 복잡도는 O(nlogn)으로 동일하지만, 상위 k개 요소를 구할 때는 PriorityQueue가 더 효율적이겠네요! 하나 배워갑니다 :)

for(int i = 0; i < k; i++) {
answer[i] = pq.poll().getKey();
}

return answer;
}
}
27 changes: 27 additions & 0 deletions valid-palindrome/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public boolean isPalindrome(String s) {
int start = 0;
int end = s.length() - 1;

while(start < end) {
if(!Character.isLetterOrDigit(s.charAt(start))) {
start++;
continue;
}

if(!Character.isLetterOrDigit(s.charAt(end))){
end--;
continue;
}

if(Character.toLowerCase(s.charAt(start)) != Character.toLowerCase(s.charAt(end))) {
return false;
}

start++;
end--;
}

return true;
}
}
Loading