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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions contains-duplicate/renovizee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

// https://github.com/DaleStudy/leetcode-study/issues/217
// https://leetcode.com/problems/contains-duplicate/
class Solution {
public boolean containsDuplicate(int[] nums) {

return true;
}
}
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;
}
}
10 changes: 10 additions & 0 deletions longest-consecutive-sequence/renovizee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

// https://github.com/DaleStudy/leetcode-study/issues/240
// https://leetcode.com/problems/longest-consecutive-sequence/
class Solution {
public int longestConsecutive(int[] nums) {

return 1;

}
}
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;
}
}
60 changes: 60 additions & 0 deletions two-sum/renovizee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.HashMap;
import java.util.Map;

// tag renovizee
// 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 {
// Solv2: map
// 시간복잡도 : O(n)
// 공간복잡도 : O(1)
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 만사용하다..
//-------------------------------------------------------------------------------------------------------------

}