Skip to content

[renovziee] WEEK 01 Solutions #1672

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

// tag renovizee 1week
// https://github.com/DaleStudy/leetcode-study/issues/217
// https://leetcode.com/problems/contains-duplicate/
class Solution {

// Solv2: hash set (feedback)
public boolean containsDuplicate(int[] nums) {
// 시간복잡도 : O(n)
// 공간복잡도 : O(n)
Set<Integer> numsSet = new HashSet<>();
for (int num : nums) {
if (numsSet.contains(num)) {
return true;
}
numsSet.add(num);
}
return false;
}

// Solv1: hash map
// public boolean containsDuplicate(int[] nums) {
// // 시간복잡도 : O(n)
// // 공간복잡도 : O(n)
// Map<Integer,Integer> countMap = new HashMap<>();
// for (int num : nums) {
// int count = countMap.getOrDefault(num, 0);
// int addCount = count + 1;
// countMap.put(num, addCount);
// if (addCount == 2) {
// return true;
// }
// }
// return false;
// }
}

//-------------------------------------------------------------------------------------------------------------
// 기본 문법 피드백
// 1) Map 기본 문법, ~.getOrDefault()
//-------------------------------------------------------------------------------------------------------------
9 changes: 9 additions & 0 deletions house-robber/renovizee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

// https://github.com/DaleStudy/leetcode-study/issues/264
// https://leetcode.com/problems/house-robber/
class Solution {
public int rob(int[] nums) {

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

// tag renovizee 1week unresolved
// https://github.com/DaleStudy/leetcode-study/issues/240
// https://leetcode.com/problems/longest-consecutive-sequence/
class Solution {
public int longestConsecutive(int[] nums) {
// 시간복잡도 : O(n)
// 공간복잡도 : O(n)

Set<Integer> numSet = new HashSet<>();
for (int num : nums) {
numSet.add(num);
}

int maxCount = 0;
for (int num : nums) {
if (numSet.contains(num - 1)) continue;
int currentCount = 1;
while (numSet.contains(num + currentCount)) {
currentCount++;
}
maxCount = Math.max(maxCount, currentCount);
}
return maxCount;
}
}


//-------------------------------------------------------------------------------------------------------------
// 기본 문법 피드백
// 1) Set<Integer> numSet = new HashSet<>();
// 2) Math 활용 Math.max(maxCount, currentCount);
//-------------------------------------------------------------------------------------------------------------
10 changes: 10 additions & 0 deletions top-k-frequent-elements/renovizee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

// https://github.com/DaleStudy/leetcode-study/issues/237
// https://leetcode.com/problems/top-k-frequent-elements/
class Solution {
public int[] topKFrequent(int[] nums, int k) {
int[] result = {1, 2, 3};

return result;
}
}
77 changes: 77 additions & 0 deletions two-sum/renovizee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.util.HashMap;
import java.util.Map;

// tag renovizee 1week
// https://github.com/DaleStudy/leetcode-study/issues/219
// https://leetcode.com/problems/two-sum/description/

// #요구사항 요약
// 1. int[] nums와 int target이 주어진다.
// 2. nums의 두 수의 합이 target과 같은 int[] index를 리턴한다. (순서 상관 x)
// 3. 똑같은 원소를 두번 사용하지 못하고, 정확히 하나의 정답만 있다.

class Solution {
// Solv3: map 최적화
// 시간복잡도 : O(n)
// 공간복잡도 : O(n)
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
int[] result = new int[2];
for (int i = 0; i < nums.length; i++) {
int key = target - nums[i];
if (map.containsKey(key) && map.get(key) != i) {
result[0] = i;
result[1] = map.get(key);
}
map.put(nums[i], i);
}
return result;
}
//-------------------------------------------------------------------------------------------------------------
// Solv2: map
// 시간복잡도 : O(n)
// 공간복잡도 : O(n)
// public int[] twoSum(int[] nums, int target) {
// Map<Integer, Integer> map = new HashMap<>();
// int[] result = new int[2];
// for (int i = 0; i < nums.length; i++) {
// map.put(nums[i], i);
// }
//
// for (int i = 0; i < nums.length; i++) {
// int key = target - nums[i];
// if (map.containsKey(key) && map.get(key) != i) {
// result[0] = i;
// result[1] = map.get(key);
// }
// }
// return result;
//
// }
//-------------------------------------------------------------------------------------------------------------
// Solv1: Brute Force
// 시간복잡도 : O(n^2)
// 공간복잡도 : O(1)
// public int[] twoSum(int[] nums, int target) {
// int size = nums.length;
// for(int i = 0; i < size - 1; i++) {
// for(int j = i+1; j < size; j++) {
// if(target == (nums[i] + nums[j])){
// return new int[]{i,j};
// }
// }
// }
// return new int[]{};
// }


// 1) ==: 두 값이 같은지 비교. 기본 타입은 값을 비교하고, 참조 타입은 메모리 주소(동일한 객체인지)를 비교
// 참조 타입 객체의 내용이 같은지를 비교하려면 주로 a.equals(b)를 사용
//
// 2) 초기화 배열과 맵
// - new int[2] :size 초기화
// - new int[]{1,2,3} : 실제 값 초기화
// - Map<String,String> test = new HashMap<>(); 맵의 k/v 타입은 앞 변수에 설정한다. val 만사용하다..
//-------------------------------------------------------------------------------------------------------------

}