Skip to content

Commit 3c58daa

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 841f937 + 130c3df commit 3c58daa

File tree

42 files changed

+997
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+997
-0
lines changed

3sum/WhiteHyun.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// 15. 3Sum
3+
// https://leetcode.com/problems/3sum/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/06/04.
7+
//
8+
9+
class Solution {
10+
func threeSum(_ nums: [Int]) -> [[Int]] {
11+
var result: [[Int]] = []
12+
let sorted = nums.sorted()
13+
14+
for (index, element) in sorted.enumerated() where index <= 0 || element != sorted[index - 1] {
15+
var left = index + 1
16+
var right = sorted.count - 1
17+
18+
while left < right {
19+
let threeSum = element + sorted[left] + sorted[right]
20+
if threeSum > 0 {
21+
right -= 1
22+
continue
23+
}
24+
if threeSum < 0 {
25+
left += 1
26+
continue
27+
}
28+
29+
result.append([element, sorted[left], sorted[right]])
30+
left += 1
31+
32+
while sorted[left] == sorted[left - 1] && left < right {
33+
left += 1
34+
}
35+
}
36+
}
37+
38+
39+
return result
40+
}
41+
}

3sum/bky373.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* time: O(n^2)
3+
* space: O(n)
4+
*
5+
* - time: becasue of two nested loop and inner loop having a linear time complexity.
6+
* - space: because of a HashSet to store the triplets.
7+
*/
8+
class Solution {
9+
10+
public List<List<Integer>> threeSum(int[] nums) {
11+
Arrays.sort(nums);
12+
int i = 0, j, k;
13+
int ni = 0, nj, nk;
14+
Set<List<Integer>> res = new HashSet<>();
15+
while (i < nums.length && ni <= 0) {
16+
ni = nums[i];
17+
j = i + 1;
18+
k = nums.length - 1;
19+
while (j < k) {
20+
nj = nums[j];
21+
nk = nums[k];
22+
int sum = ni + nj + nk;
23+
if (sum < 0) {
24+
j++;
25+
} else if (sum > 0) {
26+
k--;
27+
} else {
28+
res.add(List.of(ni, nj, nk));
29+
j++;
30+
}
31+
}
32+
i++;
33+
}
34+
return res.stream()
35+
.toList();
36+
}
37+
}

3sum/han.exs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule Solution do
2+
@spec three_sum(nums :: [integer]) :: [[integer]]
3+
def three_sum(nums) do
4+
length_of_nums = length nums
5+
num_index_map = nums |> Enum.with_index |> Map.new
6+
frequencies_of_num = nums |> Enum.frequencies()
7+
tuple_nums = nums |> List.to_tuple
8+
num_indexs_map = nums |>
9+
Enum.with_index() |>
10+
Enum.group_by(fn {v, _} -> v end, fn {_, i} -> i end)
11+
12+
Stream.unfold({0, 1}, fn {i, j} ->
13+
if j < length_of_nums - 1,
14+
do: {{i, j}, {i, j + 1}},
15+
else: {{i, j}, {i + 1, i + 2}}
16+
end) |>
17+
Stream.take_while(fn {i, _} ->
18+
i < length_of_nums - 1
19+
end) |>
20+
Stream.map(fn {i, j} ->
21+
a = elem tuple_nums, i
22+
b = elem tuple_nums, j
23+
c = -(a + b)
24+
25+
case frequencies_of_num[c] do
26+
nil -> nil
27+
count when count >= 3 -> [a, b, c] |> Enum.sort()
28+
_ ->
29+
if num_indexs_map[c] |> Enum.filter(& &1 != i && &1 != j) |> Enum.at(0),
30+
do: [a, b, c] |> Enum.sort(),
31+
else: nil
32+
end
33+
end) |>
34+
Stream.reject(& &1 == nil) |>
35+
Stream.uniq |>
36+
Enum.to_list
37+
end
38+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- 문제: https://leetcode.com/problems/binary-tree-level-order-traversal/
2+
- 풀이: https://www.algodale.com/problems/binary-tree-level-order-traversal/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
- 문제: https://leetcode.com/problems/container-with-most-water/
2+
- time complexity : O(n)
3+
- space complexity : O(1)
4+
- 블로그 주소 : https://algorithm.jonghoonpark.com/2024/06/02/leetcode-11
5+
6+
```java
7+
class Solution {
8+
public int maxArea(int[] height) {
9+
int start = 0;
10+
int end = height.length - 1;
11+
12+
int max = getArea(height, start, end);
13+
14+
while(start < end) {
15+
if(height[start] >= height[end]) {
16+
end--;
17+
max = Math.max(max, getArea(height, start, end));
18+
} else {
19+
start++;
20+
max = Math.max(max, getArea(height, start, end));
21+
}
22+
}
23+
24+
return max;
25+
}
26+
27+
public int getArea(int[] height, int start, int end) {
28+
if (start == end) {
29+
return 0;
30+
}
31+
return getMinHeight(height[end], height[start]) * (end - start);
32+
}
33+
34+
public int getMinHeight(int height1, int height2) {
35+
return Math.min(height1, height2);
36+
}
37+
}
38+
```

container-with-most-water/nhistory.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var maxArea = function (height) {
2+
// Two pointer: left and right
3+
// Amount of water: math.Min(height[left], height[right]) * (right-left)
4+
5+
// Exception case
6+
if (height.length === 0) return 0;
7+
8+
let left = 0;
9+
let right = height.length - 1;
10+
let result = 0;
11+
12+
// Iterate to find maxiume amount of water
13+
while (left < right) {
14+
const amount = Math.min(height[left], height[right]) * (right - left);
15+
result = Math.max(result, amount);
16+
height[left] <= height[right] ? left++ : right--;
17+
}
18+
19+
return result;
20+
};
21+
22+
// TC: O(n)
23+
// SC: O(1)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def maxArea(self, height: List[int]) -> int:
5+
low = 0
6+
high = len(height) - 1
7+
max_val = 0
8+
9+
while low < high:
10+
val = min(height[low], height[high]) * (high - low)
11+
max_val = max(val, max_val)
12+
if height[low] < height[high]:
13+
low += 1
14+
elif height[low] > height[high]:
15+
high -= 1
16+
# increment low or decrement high
17+
else:
18+
low += 1
19+
20+
return max_val

container-with-most-water/yolophg.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
var maxArea = function(height) {
5+
let left = 0;
6+
let right = height.length - 1;
7+
8+
// to store the maximum area.
9+
let maxArea = 0;
10+
11+
// iterate until the two pointers meet.
12+
while (left < right) {
13+
// calculate the width between the two pointers.
14+
let width = right - left;
15+
16+
// calculate the area with the current area.
17+
let currentArea = Math.min(height[left], height[right]) * width;
18+
19+
// update the maximum area if the current area is larger.
20+
maxArea = Math.max(maxArea, currentArea);
21+
22+
// move the pointer to the shorter line towards the center.
23+
if (height[left] < height[right]) {
24+
left++;
25+
} else {
26+
right--;
27+
}
28+
}
29+
30+
// return the maximum area found.
31+
return maxArea;
32+
};

counting-bits/bky373.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* https://leetcode.com/problems/counting-bits/
3+
*
4+
* time: O(n * log n)
5+
* space: O(1)
6+
*/
7+
class Solution {
8+
9+
public int[] countBits(int n) {
10+
int[] ans = new int[n + 1];
11+
for (int i = 0; i <= n; i++) {
12+
int x = i;
13+
int cnt = 0;
14+
while (x != 0) {
15+
cnt++;
16+
x &= (x - 1);
17+
}
18+
ans[i] = cnt;
19+
}
20+
return ans;
21+
}
22+
}
23+

encode-and-decode-strings/bky373.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
public class Codec {
6+
7+
// Encodes a list of strings to a single string.
8+
public String encode(List<String> strs) {
9+
StringBuilder sb = new StringBuilder();
10+
for (String str : strs) {
11+
sb.append(str.length())
12+
.append(':')
13+
.append(str);
14+
}
15+
return sb.toString();
16+
}
17+
18+
// Decodes a single string to a list of strings.
19+
public List<String> decode(String s) {
20+
List<String> decoded = new ArrayList<>();
21+
int i = 0;
22+
while (i < s.length()) {
23+
int searchIndex = s.indexOf(':', i);
24+
int chunkSize = Integer.parseInt(s.substring(i, searchIndex));
25+
i = searchIndex + chunkSize + 1;
26+
decoded.add(s.substring(searchIndex + 1, i));
27+
}
28+
return decoded;
29+
}
30+
}

0 commit comments

Comments
 (0)