Skip to content

Commit 99c80ed

Browse files
authored
Merge pull request #1211 from minji-go/week02
[minji-go] week 02 solutions
2 parents bc505c7 + fa77202 commit 99c80ed

File tree

4 files changed

+73
-81
lines changed

4 files changed

+73
-81
lines changed

3sum/minji-go.java

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
1-
/*
2-
Problem: https://leetcode.com/problems/3sum/
3-
Description: return all the triplets (i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0)
4-
Concept: Array, Two Pointers, Sorting
5-
Time Complexity: O(N²), Runtime 70ms
6-
Space Complexity: O(N), Memory 51.63MB
7-
*/
1+
/**
2+
* <a href="https://leetcode.com/problems/3sum/">week02-4.3sum</a>
3+
* Description: return all the triplets (i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0)
4+
* Concept: Array, Two Pointers, Sorting
5+
* Time Complexity: O(N²), Runtime 2021ms
6+
* Space Complexity: O(N), Memory 53.9MB
7+
*/
8+
89
class Solution {
910
public List<List<Integer>> threeSum(int[] nums) {
10-
Map<Integer, Integer> number = new HashMap<>();
11-
for(int i=0; i<nums.length; i++) {
12-
number.put(nums[i], number.getOrDefault(nums[i], 0)+1);
11+
Map<Integer, Integer> map = new HashMap<>();
12+
for(int i=0; i<nums.length; i++){
13+
map.put(nums[i], i);
1314
}
1415

15-
Arrays.sort(nums);
16-
Set<List<Integer>> set = new HashSet<>();
17-
List<List<Integer>> triplets = new ArrayList<>();
18-
List<Integer> lastTriplet = null;
19-
for(int i=0; i<nums.length-1; i++) {
20-
if(i>0 && nums[i]==nums[i-1]) continue;
21-
22-
for(int j=i+1; j<nums.length; j++){
23-
if(j>i+1 && nums[j]==nums[j-1]) continue;
24-
25-
int target = -(nums[i]+nums[j]);
26-
if(nums[j]>target) continue;
27-
28-
int count = number.getOrDefault(target,0);
29-
if(nums[i]==target) count--;
30-
if(nums[j]==target) count--;
31-
if(count<=0) continue;
32-
33-
List<Integer> triplet = List.of(nums[i], nums[j], target);
34-
if(triplet.equals(lastTriplet)) continue;
35-
lastTriplet = triplet;
36-
triplets.add(triplet);
16+
Set<List<Integer>> triplets = new HashSet<>();
17+
for(int i=0; i<nums.length-2; i++){
18+
for(int j=i+1; j<nums.length-1; j++){
19+
int sum = nums[i]+nums[j];
20+
if(map.containsKey(-sum) && map.get(-sum) > j){
21+
List<Integer> list = Arrays.asList(nums[i],nums[j],-sum);
22+
Collections.sort(list);
23+
triplets.add(list);
24+
}
3725
}
3826
}
39-
return triplets;
27+
28+
return new ArrayList<>(triplets);
4029
}
4130
}

climbing-stairs/minji-go.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
/*
2-
Problem: https://leetcode.com/problems/climbing-stairs/
3-
Description: how many distinct ways can you climb to the top, if you can either climb 1 or 2 steps
4-
Concept: Dynamic Programming, Memoization, Recursion, Math, Array, Iterator, Combinatorics ...
5-
Time Complexity: O(n), Runtime: 0ms
6-
Space Complexity: O(1), Memory: 40.63MB
7-
*/
1+
/**
2+
* <a href="https://leetcode.com/problems/climbing-stairs/">week02-2.climbing-stairs</a>
3+
* <li> Description: how many distinct ways can you climb to the top, if you can either climb 1 or 2 steps </li>
4+
* <li> Concept: Dynamic Programming, Memoization, Recursion, Math, Array, Iterator, Combinatorics ... </li>
5+
* <li> Time Complexity: O(n), Runtime: 0ms </li>
6+
* <li> Space Complexity: O(1), Memory: 40.39MB </li>
7+
*/
8+
89
class Solution {
910
public int climbStairs(int n) {
10-
if(n==1) return 1;
11-
if(n==2) return 2;
11+
if (n <= 2) return n;
12+
13+
int prev = 1;
14+
int curr = 2;
1215

13-
int prev=1, cur=2;
14-
for(int i=3; i<=n; i++){
15-
int next=prev+cur;
16-
prev=cur;
17-
cur=next;
16+
for (int i = 3; i <= n; i++) {
17+
int next = prev + curr;
18+
prev = curr;
19+
curr = next;
1820
}
19-
return cur;
21+
22+
return curr;
2023
}
2124
}

product-of-array-except-self/minji-go.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
/*
2-
Problem: https://leetcode.com/problems/product-of-array-except-self/
3-
Description: return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
4-
Concept: Array, Prefix Sum
5-
Time Complexity: O(N), Runtime 5ms
6-
Space Complexity: O(N), Memory 54.6MB - O(1) except the output array
7-
*/
1+
/**
2+
<a href="https://leetcode.com/problems/product-of-array-except-self/">week02-3.product-of-array-except-self</a>
3+
<li> Description: return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].</li>
4+
<li> Concept: Array, Prefix Sum </li>
5+
<li> Time Complexity: O(N), Runtime 2ms </li>
6+
<li> Space Complexity: O(1), Memory 55.62MB - except the output array </li>
7+
*/
88
class Solution {
99
public int[] productExceptSelf(int[] nums) {
10+
1011
int[] answer = new int[nums.length];
1112
Arrays.fill(answer, 1);
1213

13-
int prefixProduct = 1;
14-
int suffixProduct = 1;
15-
for(int i=1; i<nums.length; i++){
16-
prefixProduct = prefixProduct * nums[i-1];
17-
suffixProduct = suffixProduct * nums[nums.length-i];
18-
answer[i] *= prefixProduct;
19-
answer[nums.length-i-1] *= suffixProduct;
14+
int left = 1;
15+
for(int i=1; i<nums.length; i++) {
16+
answer[i] *= left *= nums[i-1];
17+
}
18+
19+
int right = 1;
20+
for(int i=nums.length-2; i>=0; i--) {
21+
answer[i] *= right *= nums[i+1];
2022
}
2123

2224
return answer;

valid-anagram/minji-go.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
/*
2-
Problem: https://leetcode.com/problems/valid-anagram/
3-
Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other
4-
Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ...
5-
Time Complexity: O(n), Runtime: 27ms
6-
Space Complexity: O(n), Memory: 43.05MB
7-
*/
8-
import java.util.HashMap;
9-
import java.util.Map;
1+
/**
2+
* <a href="https://leetcode.com/problems/valid-anagram/">week02-1.valid-anagram</a>
3+
* <li> Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other </li>
4+
* <li> Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ... </li>
5+
* <li> Time Complexity: O(n), Runtime: 15ms </li>
6+
* <li> Space Complexity: O(n), Memory: 44.66MB </li>
7+
*/
108

119
class Solution {
1210
public boolean isAnagram(String s, String t) {
13-
if(s.length() != t.length()) return false;
11+
if (s.length() != t.length()) return false;
1412

15-
Map<Character, Integer> charCount = new HashMap<>();
16-
for(int i=0; i<s.length(); i++){
17-
charCount.put(s.charAt(i), charCount.getOrDefault(s.charAt(i), 0)+1);
18-
charCount.put(t.charAt(i), charCount.getOrDefault(t.charAt(i), 0)-1);
19-
}
20-
for(Integer count : charCount.values()){
21-
if(count !=0) return false;
22-
}
23-
return true;
13+
Map<Integer, Integer> tmap = t.chars()
14+
.boxed()
15+
.collect(Collectors.toMap(i -> i, i -> 1, (i1, i2) -> i1 + i2));
16+
17+
Map<Integer, Integer> smap = s.chars()
18+
.boxed()
19+
.collect(Collectors.toMap(i -> i, i -> 1, (i1, i2) -> i1 + i2));
20+
21+
return tmap.equals(smap);
2422
}
2523
}

0 commit comments

Comments
 (0)