-
-
Notifications
You must be signed in to change notification settings - Fork 245
[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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8cd8fc
feat: Set up Week 1 problems
renovizee 0be0251
feat(leetcode/1): Solve Two Sum (Easy)
renovizee f13c67c
feat(leetcode/217): Solve Contains Duplicate (Easy)
renovizee b8140d5
feat(leetcode/128): Solve Longest Consecutive Sequence (Medium)
renovizee 31290f2
feat(leetcode/217): Solve Contains Duplicate (Easy) - feedback map to…
renovizee d4312fb
feat(leetcode/1): Solve Two Sum (Easy) - feedback 최적화
renovizee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
// tag renovizee 1week | ||
// https://github.com/DaleStudy/leetcode-study/issues/217 | ||
// https://leetcode.com/problems/contains-duplicate/ | ||
class Solution { | ||
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() | ||
//------------------------------------------------------------------------------------------------------------- |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 문제에 대한 힌트는 DP 입니다! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
//------------------------------------------------------------------------------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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 { | ||
// 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); | ||
} | ||
} | ||
renovizee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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 만사용하다.. | ||
//------------------------------------------------------------------------------------------------------------- | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.