-
-
Notifications
You must be signed in to change notification settings - Fork 245
[minji-go] week 02 solutions #1211
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 all commits
Commits
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 |
---|---|---|
@@ -1,41 +1,30 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/3sum/ | ||
Description: return all the triplets (i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0) | ||
Concept: Array, Two Pointers, Sorting | ||
Time Complexity: O(N²), Runtime 70ms | ||
Space Complexity: O(N), Memory 51.63MB | ||
*/ | ||
/** | ||
* <a href="https://leetcode.com/problems/3sum/">week02-4.3sum</a> | ||
* Description: return all the triplets (i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0) | ||
* Concept: Array, Two Pointers, Sorting | ||
* Time Complexity: O(N²), Runtime 2021ms | ||
* Space Complexity: O(N), Memory 53.9MB | ||
*/ | ||
|
||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
Map<Integer, Integer> number = new HashMap<>(); | ||
for(int i=0; i<nums.length; i++) { | ||
number.put(nums[i], number.getOrDefault(nums[i], 0)+1); | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
for(int i=0; i<nums.length; i++){ | ||
map.put(nums[i], i); | ||
} | ||
|
||
Arrays.sort(nums); | ||
Set<List<Integer>> set = new HashSet<>(); | ||
List<List<Integer>> triplets = new ArrayList<>(); | ||
List<Integer> lastTriplet = null; | ||
for(int i=0; i<nums.length-1; i++) { | ||
if(i>0 && nums[i]==nums[i-1]) continue; | ||
|
||
for(int j=i+1; j<nums.length; j++){ | ||
if(j>i+1 && nums[j]==nums[j-1]) continue; | ||
|
||
int target = -(nums[i]+nums[j]); | ||
if(nums[j]>target) continue; | ||
|
||
int count = number.getOrDefault(target,0); | ||
if(nums[i]==target) count--; | ||
if(nums[j]==target) count--; | ||
if(count<=0) continue; | ||
|
||
List<Integer> triplet = List.of(nums[i], nums[j], target); | ||
if(triplet.equals(lastTriplet)) continue; | ||
lastTriplet = triplet; | ||
triplets.add(triplet); | ||
Set<List<Integer>> triplets = new HashSet<>(); | ||
for(int i=0; i<nums.length-2; i++){ | ||
for(int j=i+1; j<nums.length-1; j++){ | ||
int sum = nums[i]+nums[j]; | ||
if(map.containsKey(-sum) && map.get(-sum) > j){ | ||
List<Integer> list = Arrays.asList(nums[i],nums[j],-sum); | ||
Collections.sort(list); | ||
triplets.add(list); | ||
} | ||
} | ||
} | ||
return triplets; | ||
|
||
return new ArrayList<>(triplets); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,21 +1,24 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/climbing-stairs/ | ||
Description: how many distinct ways can you climb to the top, if you can either climb 1 or 2 steps | ||
Concept: Dynamic Programming, Memoization, Recursion, Math, Array, Iterator, Combinatorics ... | ||
Time Complexity: O(n), Runtime: 0ms | ||
Space Complexity: O(1), Memory: 40.63MB | ||
*/ | ||
/** | ||
* <a href="https://leetcode.com/problems/climbing-stairs/">week02-2.climbing-stairs</a> | ||
* <li> Description: how many distinct ways can you climb to the top, if you can either climb 1 or 2 steps </li> | ||
* <li> Concept: Dynamic Programming, Memoization, Recursion, Math, Array, Iterator, Combinatorics ... </li> | ||
* <li> Time Complexity: O(n), Runtime: 0ms </li> | ||
* <li> Space Complexity: O(1), Memory: 40.39MB </li> | ||
*/ | ||
|
||
class Solution { | ||
public int climbStairs(int n) { | ||
if(n==1) return 1; | ||
if(n==2) return 2; | ||
if (n <= 2) return n; | ||
|
||
int prev = 1; | ||
int curr = 2; | ||
|
||
int prev=1, cur=2; | ||
for(int i=3; i<=n; i++){ | ||
int next=prev+cur; | ||
prev=cur; | ||
cur=next; | ||
for (int i = 3; i <= n; i++) { | ||
int next = prev + curr; | ||
prev = curr; | ||
curr = next; | ||
} | ||
return cur; | ||
|
||
return curr; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,25 +1,23 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/valid-anagram/ | ||
Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other | ||
Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ... | ||
Time Complexity: O(n), Runtime: 27ms | ||
Space Complexity: O(n), Memory: 43.05MB | ||
*/ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
/** | ||
* <a href="https://leetcode.com/problems/valid-anagram/">week02-1.valid-anagram</a> | ||
* <li> Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other </li> | ||
* <li> Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ... </li> | ||
* <li> Time Complexity: O(n), Runtime: 15ms </li> | ||
* <li> Space Complexity: O(n), Memory: 44.66MB </li> | ||
*/ | ||
|
||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
if(s.length() != t.length()) return false; | ||
if (s.length() != t.length()) return false; | ||
|
||
Map<Character, Integer> charCount = new HashMap<>(); | ||
for(int i=0; i<s.length(); i++){ | ||
charCount.put(s.charAt(i), charCount.getOrDefault(s.charAt(i), 0)+1); | ||
charCount.put(t.charAt(i), charCount.getOrDefault(t.charAt(i), 0)-1); | ||
} | ||
for(Integer count : charCount.values()){ | ||
if(count !=0) return false; | ||
} | ||
return true; | ||
Map<Integer, Integer> tmap = t.chars() | ||
.boxed() | ||
.collect(Collectors.toMap(i -> i, i -> 1, (i1, i2) -> i1 + i2)); | ||
|
||
Map<Integer, Integer> smap = s.chars() | ||
.boxed() | ||
.collect(Collectors.toMap(i -> i, i -> 1, (i1, i2) -> i1 + i2)); | ||
|
||
return tmap.equals(smap); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 둘다 맵으로 변환해서 풀이만 했는데 둘의 길이가 다를 때 바로 리턴 시킬 수 있다는 걸 알았네요 ㅎㅎ 2주차 고생하셨습니다.