Skip to content

[dohee789] WEEK 01 solutions #1695

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

Closed
wants to merge 12 commits into from
17 changes: 17 additions & 0 deletions contains-duplicate/dohee789.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
https://leetcode.com/problems/contains-duplicate/
*/
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet();

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

return false;
}
}
21 changes: 21 additions & 0 deletions house-robber/dohee789.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
https://leetcode.com/problems/house-robber/
*/
class Solution{
public int rob(int[] nums){
if(nums.length == 0 || nums == null) return 0;
if(nums.length == 1) return 0;

int prev1 = 0;
int prev2 = 0;
for(int num: nums){
int temp = Math.max(prev1, prev2+num);
prev2 = prev1;
prev1 = temp;
}

return prev1;
}
}


26 changes: 26 additions & 0 deletions longest-consecutive-sequence/dohee789.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
https://leetcode.com/problems/longest-consecutive-sequence/
*/
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int num: nums){
set.add(num);
}

int maxLength = 0;
for(int num: set){
if(!set.contains(num-1)){
int currentNum = num;
int length = 1;
while(set.contains(currentNum+1)){
currentNum++;
length++;
}
maxLength = Math.max(maxLength,length);
}
}
return maxLength;
}
}

31 changes: 31 additions & 0 deletions top-k-frequent-elements/dohee789.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
https://leetcode.com/problems/top-k-frequent-elements/
*/
class Solution {
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);
}

PriorityQueue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>(
Comparator.comparingInt(Map.Entry::getValue)
);

for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
minHeap.offer(entry);
if (minHeap.size() > k) {
minHeap.poll();
}
}

int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = minHeap.poll().getKey();
}

return result;

}
}

19 changes: 19 additions & 0 deletions two-sum/dohee789.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
https://leetcode.com/problems/two-sum/
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();

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

return null;
}
}