Skip to content

Commit c935554

Browse files
week5 mission done
1 parent e0508f0 commit c935554

File tree

5 files changed

+208
-0
lines changed

5 files changed

+208
-0
lines changed

โ€Ž3sum/dev-jonghoonpark.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
- ๋ฌธ์ œ : https://leetcode.com/problems/3sum/
2+
- time complexity : O(n^2)
3+
- space complexity : O(1) (๊ฒฐ๊ณผ๊ฐ’์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด์˜ ๊ณต๊ฐ„ ๋ณต์žก๋„)
4+
- ๋ธ”๋กœ๊ทธ ๋งํฌ : https://algorithm.jonghoonpark.com/2024/05/07/leetcode-15
5+
6+
```java
7+
public List<List<Integer>> threeSum(int[] nums) {
8+
Arrays.sort(nums);
9+
10+
List<List<Integer>> result = new ArrayList<>();
11+
12+
int lastOne = Integer.MIN_VALUE;
13+
for (int i = 0; i < nums.length - 1; i++) {
14+
int num = nums[i];
15+
if (lastOne == num) {
16+
continue;
17+
}
18+
19+
twoSum(result, nums, i);
20+
lastOne = num;
21+
}
22+
23+
return result;
24+
}
25+
26+
public void twoSum(List<List<Integer>> result, int[] nums, int targetIndex) {
27+
int target = -nums[targetIndex];
28+
29+
int i = targetIndex + 1;
30+
int j = nums.length - 1;
31+
while (i < j) {
32+
int twoSum = nums[i] + nums[j];
33+
34+
if (target > twoSum) {
35+
i++;
36+
}
37+
38+
if (target < twoSum) {
39+
j--;
40+
}
41+
42+
if (target == twoSum) {
43+
result.add(List.of(-target, nums[i], nums[j]));
44+
int current = nums[i];
45+
while (i < nums.length - 2 && current == nums[i + 1]) {
46+
i++;
47+
}
48+
i++;
49+
}
50+
}
51+
}
52+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
- ๋ฌธ์ œ
2+
- ์œ ๋ฃŒ : https://leetcode.com/problems/encode-and-decode-strings/
3+
- ๋ฌด๋ฃŒ : https://www.lintcode.com/problem/659/
4+
- time complexity : O(n)
5+
- space complexity : O(n \* m), m์€ ๊ฐ ๋ฌธ์ž์—ด ๊ธธ์ด์˜ ํ‰๊ท 
6+
- ๋ธ”๋กœ๊ทธ ๋งํฌ : https://algorithm.jonghoonpark.com/2024/05/29/leetcode-271
7+
8+
```java
9+
public String encode(List<String> strs) {
10+
StringBuilder sb = new StringBuilder();
11+
for (String str : strs) {
12+
sb.append(str.replace("%", "%25").replace(",", "%2C")).append(",");
13+
}
14+
return sb.length() > 0 ? sb.toString() : "";
15+
}
16+
17+
public List<String> decode(String str) {
18+
List<String> decodedList = new ArrayList<>();
19+
if (str.length() > 0) {
20+
int commaIndex = str.indexOf(",");
21+
while (commaIndex > -1) {
22+
decodedList.add(str.substring(0, commaIndex).replace("%2C", ",").replace("%25", "%"));
23+
str = str.substring(commaIndex + 1);
24+
commaIndex = str.indexOf(",");
25+
}
26+
}
27+
return decodedList;
28+
}
29+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
- ๋ฌธ์ œ : https://leetcode.com/problems/longest-consecutive-sequence/
2+
- time complexity : O(n)
3+
- space complexity : O(n)
4+
- ๋ธ”๋กœ๊ทธ ๋งํฌ : https://algorithm.jonghoonpark.com/2024/05/28/leetcode-128
5+
6+
```java
7+
public int longestConsecutive(int[] nums) {
8+
Set<Integer> set = new HashSet<>();
9+
10+
for(int num : nums) {
11+
set.add(num);
12+
}
13+
14+
int max = 0;
15+
for(int num : set) {
16+
if (!set.contains(num - 1)) {
17+
int current = 1;
18+
while (set.contains(num + 1)) {
19+
current++;
20+
num++;
21+
}
22+
max = Math.max(current, max);
23+
}
24+
}
25+
26+
return max;
27+
}
28+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
- ๋ฌธ์ œ : https://leetcode.com/problems/product-of-array-except-self/
2+
- time complexity : O(n)
3+
- space complexity : O(n)
4+
- ๋ธ”๋กœ๊ทธ ๋งํฌ : https://algorithm.jonghoonpark.com/2024/05/08/leetcode-238
5+
6+
## case ๋‚˜๋ˆ ์„œ ํ’€๊ธฐ
7+
8+
```java
9+
public int[] productExceptSelf(int[] nums) {
10+
int[] products = new int[nums.length];
11+
int result = 1;
12+
int zeroCount = 0;
13+
14+
int p = 0;
15+
while (p < nums.length) {
16+
if (nums[p] != 0) {
17+
result *= nums[p];
18+
} else {
19+
zeroCount++;
20+
if (zeroCount >= 2) {
21+
Arrays.fill(products, 0);
22+
return products;
23+
}
24+
}
25+
p++;
26+
}
27+
28+
if (zeroCount == 1) {
29+
p = 0;
30+
while (p < nums.length) {
31+
if (nums[p] == 0) {
32+
products[p] = result;
33+
}
34+
p++;
35+
}
36+
} else {
37+
p = 0;
38+
while (p < nums.length) {
39+
products[p] = result / nums[p];
40+
p++;
41+
}
42+
}
43+
44+
return products;
45+
}
46+
```
47+
48+
## ์žฌ๋ฐ‹๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๊ธฐ (prefixProd)
49+
50+
```java
51+
public int[] productExceptSelf(int[] nums) {
52+
int[] result = new int[nums.length];
53+
54+
int[] prefixProd = new int[nums.length];
55+
int[] suffixProd = new int[nums.length];
56+
57+
prefixProd[0] = nums[0];
58+
for (int i = 1; i < nums.length; i++) {
59+
prefixProd[i] = prefixProd[i-1] * nums[i];
60+
}
61+
62+
suffixProd[nums.length - 1] = nums[nums.length - 1];
63+
for (int i = nums.length - 2; i > -1; i--) {
64+
suffixProd[i] = suffixProd[i + 1] * nums[i];
65+
}
66+
67+
result[0] = suffixProd[1];
68+
result[nums.length - 1] = prefixProd[nums.length - 2];
69+
for (int i = 1; i < nums.length - 1; i++) {
70+
result[i] = prefixProd[i - 1] * suffixProd[i + 1];
71+
}
72+
73+
return result;
74+
}
75+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
- ๋ฌธ์ œ : https://leetcode.com/problems/top-k-frequent-elements/
2+
- time complexity : O(nlogn)
3+
- space complexity : O(n)
4+
- ๋ธ”๋กœ๊ทธ ๋งํฌ : https://algorithm.jonghoonpark.com/2024/04/18/leetcode-347
5+
6+
```java
7+
public int[] topKFrequent(int[] nums, int k) {
8+
Map<Integer, Integer> counter = new HashMap<>();
9+
10+
Arrays.stream(nums)
11+
.forEach(num -> {
12+
counter.put(num, counter.getOrDefault(num, 0) + 1);
13+
});
14+
15+
return Arrays.copyOfRange(counter.entrySet().stream()
16+
.sorted(Map.Entry.<Integer, Integer>comparingByValue().reversed())
17+
.mapToInt(Map.Entry::getKey)
18+
.toArray(), 0, k);
19+
}
20+
```
21+
22+
## tc, sc ๊ด€๋ จ ๊ทธ๋ ‡๊ฒŒ ์ƒ๊ฐํ•œ ์ด์œ 
23+
24+
๋นˆ๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ map์„ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— n ๋ณด๋‹ค ์ ์€ ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋‹ค์ˆ˜ ์ด๊ฒ ์ง€๋งŒ, ์ตœ์•…์˜ ๊ฒฝ์šฐ n๊ฐœ์˜ map entry๊ฐ€ ์ƒ์„ฑ๋  ์ˆ˜ ์ž‡์Œ.

0 commit comments

Comments
ย (0)