Skip to content

Commit b6da95b

Browse files
authored
Merge pull request #1751 from geegong/main
[geegong] Week 02 solutions
2 parents 831b097 + 05655f1 commit b6da95b

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

β€Ž3sum/Geegong.javaβ€Ž

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import java.util.*;
2+
3+
public class Geegong {
4+
5+
/**
6+
* Time complexity : O(n^2)
7+
* space complexity : O(n^2)
8+
* @param nums
9+
* @return
10+
*/
11+
public List<List<Integer>> threeSum(int[] nums) {
12+
13+
// μ€‘λ³΅λ˜λŠ” 값은 μ—†μ–΄μ•Ό ν•˜κΈ°μ— HashSet 으둜 result
14+
HashSet<List<Integer>> result = new HashSet<>();
15+
16+
// Key : λ°°μ—΄ μ›μ†Œ , value : List<String> μΈλ±μŠ€λ“€
17+
// elementMap 은 two pointer 의 값을 λ”ν•œ κ°’μ—μ„œ 0이 되기 μœ„ν•œ μš”μ†Œλ₯Ό μ°ΎκΈ°μœ„ν•΄ μ‚¬μš©λ  κ²ƒμž„
18+
// tc : O(n)
19+
Map<Integer, List<Integer>> elementMap = new HashMap<>();
20+
for (int index = 0; index<nums.length; index++) {
21+
int value = nums[index];
22+
if (elementMap.containsKey(value)) {
23+
List<Integer> indices = elementMap.get(value);
24+
indices.add(index);
25+
elementMap.put(value, indices);
26+
} else {
27+
List<Integer> newIndices = new ArrayList<>();
28+
newIndices.add(index);
29+
elementMap.put(value, newIndices);
30+
}
31+
}
32+
33+
// leftIndex : 0μ—μ„œ λΆ€ν„° μ‹œμž‘ν•˜λŠ” index
34+
// rightIndex : nums.length - 1μ—μ„œλΆ€ν„° κ°μ†Œν•˜λŠ” index
35+
// leftIndex > rightIndex λ˜λŠ” μˆœκ°„κΉŒμ§€λ§Œ for문을 돌 것이닀.
36+
// tc : O(N^2 / 2)
37+
for (int leftIndex=0; leftIndex < nums.length; leftIndex++) {
38+
for (int rightIndex=nums.length - 1; rightIndex >= 0; rightIndex--) {
39+
40+
if (leftIndex >= rightIndex) {
41+
break;
42+
}
43+
44+
45+
int leftValue = nums[leftIndex];
46+
int rightValue = nums[rightIndex];
47+
48+
int neededValueToZero = -leftValue - rightValue;
49+
if (elementMap.containsKey(neededValueToZero)) {
50+
// elementMap의 value κ°€ leftIndex, rightIndex 은 μ•„λ‹Œμ§€ 확인
51+
52+
List<Integer> indices = elementMap.get(neededValueToZero);
53+
// zero λ₯Ό λ§Œλ“€ 수 μžˆλŠ” μ„Έλ²ˆμ¨° μΈλ±μŠ€κ°€ μžˆλŠ”μ§€ 확인
54+
int thirdIndex = findThirdIndexToBeZero(leftIndex, rightIndex, indices);
55+
if (-1 != thirdIndex) {
56+
List<Integer> newOne = new ArrayList<>();
57+
newOne.add(nums[leftIndex]);
58+
newOne.add(nums[rightIndex]);
59+
newOne.add(nums[thirdIndex]);
60+
result.add(newOne.stream().sorted().toList());
61+
}
62+
63+
}
64+
65+
}
66+
}
67+
68+
return result.stream().toList();
69+
70+
}
71+
72+
public int findThirdIndexToBeZero(int leftIndex, int rightIndex, List<Integer> indices) {
73+
for (int index : indices) {
74+
if (index != leftIndex && index != rightIndex) {
75+
return index;
76+
}
77+
}
78+
79+
return -1;
80+
}
81+
}
82+

β€Žhouse-robber/Geegong.javaβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class Geegong {
2+
// 이 λ¬Έμ œλŠ” μ‹œκ°„μ΄ λ‚¨μ„λ•Œ ν’€ μ˜ˆμ • πŸ˜…
3+
}
4+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.Arrays;
2+
3+
public class Geegong {
4+
5+
/**
6+
* Time complexity : O(n)
7+
* Space complexity : O(1) (except result)
8+
*
9+
* 풀이 : prefix array , suffix array λ₯Ό 각 λ°°μ—΄λ§ˆλ‹€ κ΅¬ν•˜μ—¬ 각 array에 ν•΄λ‹Ήλ˜λŠ” 인덱슀의 값듀을 κ³±ν•˜μ—¬ 결과값을 λ„μΆœ
10+
*
11+
* @param nums
12+
* @return
13+
*/
14+
public int[] productExceptSelf(int[] nums) {
15+
16+
int[] result = new int[nums.length];
17+
18+
// 1. result λŠ” λ¨Όμ € prefix λ°°μ—΄λ“€μ˜ 곱으둜 μ±„μš΄λ‹€.
19+
// ( prefix λ°°μ—΄μ΄λž€ ν•΄λ‹Ήλ˜λŠ” index μ΄μ „μ˜ λ°°μ—΄μš”μ†Œκ°’λ“€μ„ 의미)
20+
21+
// μ•žμ—μ„œλΆ€ν„° λˆ„μ λ˜λŠ” 총 곱의 κ°’
22+
int accumulatedProduct = 1;
23+
for (int index=0; index<nums.length; index++) {
24+
if (index == 0) {
25+
result[index] = 1;
26+
continue;
27+
}
28+
29+
result[index] = accumulatedProduct * nums[index - 1];
30+
accumulatedProduct = result[index];
31+
}
32+
33+
// 2. λ°°μ—΄μ˜ λ’€μ—μ„œλΆ€ν„° product of suffix 값은 result λ°°μ—΄ ν•˜λ‚˜ν•˜λ‚˜μ— λŒ€μ²΄ν•œλ‹€.
34+
35+
// nums λ°°μ—΄ μ•ˆμ—μ„œ λ’€μ—μ„œλΆ€ν„° λˆ„μ λ˜λŠ” 총 곱의 κ°’
36+
accumulatedProduct = 1;
37+
for (int index=nums.length - 1; index >= 0; index--) {
38+
if (index == nums.length - 1) {
39+
accumulatedProduct = nums[index];
40+
continue;
41+
}
42+
43+
result[index] = result[index] * accumulatedProduct;
44+
accumulatedProduct = accumulatedProduct * nums[index];
45+
}
46+
47+
return result;
48+
}
49+
50+
}
51+

0 commit comments

Comments
Β (0)