Skip to content

Commit ebb5f61

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 5ac4e42 + a0e4583 commit ebb5f61

File tree

10 files changed

+378
-0
lines changed

10 files changed

+378
-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+
```

โ€Ž3sum/nhistory.jsโ€Ž

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var threeSum = function (nums) {
2+
// Sort nums array
3+
const sortedNums = nums.sort((a, b) => a - b);
4+
let result = [];
5+
6+
// Start iteration to pick first element for 3sum
7+
for (let i = 0; i < sortedNums.length; i++) {
8+
// Check if the first element is already greater than 0, no valid triplets possible after this point
9+
if (sortedNums[i] > 0) {
10+
break;
11+
}
12+
13+
// Skip duplicates of the first element to avoid redundant triplets
14+
if (i > 0 && sortedNums[i] === sortedNums[i - 1]) {
15+
continue;
16+
}
17+
18+
// Iterate to find sum of two pointer and nums[i]
19+
let left = i + 1;
20+
let right = sortedNums.length - 1;
21+
22+
while (left < right) {
23+
let sum = sortedNums[i] + sortedNums[left] + sortedNums[right];
24+
25+
if (sum === 0) {
26+
result.push([sortedNums[i], sortedNums[left], sortedNums[right]]);
27+
// Skip duplicates of left and right pointers to avoid redundant triplets
28+
while (sortedNums[left] === sortedNums[left + 1]) left++;
29+
while (sortedNums[right] === sortedNums[right - 1]) right--;
30+
left++;
31+
right--;
32+
} else if (sum < 0) {
33+
left++;
34+
} else if (sum > 0) {
35+
right--;
36+
}
37+
}
38+
}
39+
return result;
40+
};
41+
42+
// TC: O(n^2)
43+
// SC: O(n)
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
/**
3+
* @param {string[]} strs
4+
* @returns {string}
5+
*/
6+
encode(strs) {
7+
let result = "";
8+
for (let str of strs) {
9+
result += str.length.toString() + "#" + str;
10+
}
11+
return result;
12+
}
13+
14+
/**
15+
* @param {string} str
16+
* @returns {string[]}
17+
*/
18+
decode(str) {
19+
let result = [];
20+
let i = 0;
21+
22+
while (i < str.length) {
23+
// Find the position of the next '#'
24+
let j = i;
25+
while (str[j] !== "#") {
26+
j++;
27+
}
28+
29+
// Length of the next string
30+
const len = parseInt(str.slice(i, j));
31+
i = j + 1; // Move past the '#'
32+
33+
// Extract the string of length 'len'
34+
result.push(str.slice(i, i + len));
35+
i += len; // Move past the extracted string
36+
}
37+
38+
return result;
39+
}
40+
}
41+
42+
// TC: O(n),O(n)
43+
// SC: O(n),O(n)
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var longestConsecutive = function (nums) {
2+
// Return 0 if there are no elements in nums
3+
if (nums.length === 0) return 0;
4+
5+
// Create a set to find values efficiently
6+
const numSet = new Set(nums);
7+
let maxLength = 0;
8+
9+
for (let num of numSet) {
10+
// Check if this is the start of a sequence
11+
if (!numSet.has(num - 1)) {
12+
let currentNum = num;
13+
let currentLength = 1;
14+
15+
// Count the length of the sequence
16+
while (numSet.has(currentNum + 1)) {
17+
currentNum += 1;
18+
currentLength += 1;
19+
}
20+
21+
// Update the maximum length
22+
maxLength = Math.max(maxLength, currentLength);
23+
}
24+
}
25+
26+
return maxLength;
27+
};
28+
29+
// TC: O(n)
30+
// SC: O(n)
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function (nums) {
6+
const len = nums.length;
7+
const lastIndex = len - 1;
8+
9+
// Declare prefix, postfix array
10+
let prefix = new Array(len).fill(1);
11+
let postfix = new Array(len).fill(1);
12+
13+
// Iterate loop to add prefix[i-1]*nums[i] values into prefix array
14+
prefix[0] = nums[0];
15+
for (let i = 1; i < nums.length; i++) {
16+
prefix[i] = prefix[i - 1] * nums[i];
17+
}
18+
19+
// Iterate loop to add postfix[i]*nums[i-1] values into postfix array
20+
postfix[lastIndex] = nums[lastIndex];
21+
for (let i = lastIndex; i >= 1; i--) {
22+
postfix[i - 1] = postfix[i] * nums[i - 1];
23+
}
24+
25+
// Make output array with prefix and postfix arrays
26+
let output = new Array(len).fill(1);
27+
28+
// First index value is equal to postfix[1]
29+
output[0] = postfix[1];
30+
// Last index value is equal to prefix[lastIndex-1]
31+
output[lastIndex] = prefix[lastIndex - 1];
32+
for (let i = 1; i < len - 1; i++) {
33+
output[i] = prefix[i - 1] * postfix[i + 1];
34+
}
35+
36+
return output;
37+
};
38+
39+
// TC: O(n)
40+
// SC: O(n)
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๊ฐ€ ์ƒ์„ฑ๋  ์ˆ˜ ์ž‡์Œ.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var topKFrequent = function (nums, k) {
2+
// 1. Hashmap { num : frequency }
3+
let map = {};
4+
5+
// 2. Iterate counts frequency of each value
6+
for (num of nums) {
7+
// Check there is already num key or not
8+
if (!map[num]) map[num] = 0;
9+
map[num]++;
10+
}
11+
12+
// 3. Sort frequency and return sliced array
13+
return [...Object.keys(map)].sort((a, b) => map[b] - map[a]).slice(0, k);
14+
};

0 commit comments

Comments
ย (0)