Skip to content

Commit d1b731f

Browse files
Merge branch 'DaleStudy:main' into main
2 parents 418c176 + cdc550f commit d1b731f

File tree

35 files changed

+619
-0
lines changed

35 files changed

+619
-0
lines changed

insert-interval/Leo.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
3+
left = []
4+
right = []
5+
6+
for interval in intervals:
7+
if interval[1] < newInterval[0]:
8+
left.append(interval)
9+
elif interval[0] > newInterval[1]:
10+
right.append(interval)
11+
else:
12+
newInterval[0] = min(newInterval[0], interval[0])
13+
newInterval[1] = max(newInterval[1], interval[1])
14+
15+
return left + [newInterval] + right
16+
17+
## TC: O(n), SC: O(n)

insert-interval/nhistory.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var insert = function (intervals, newInterval) {
2+
let result = [];
3+
let i = 0;
4+
5+
// 1. Add all intervals before the new interval
6+
while (i < intervals.length && intervals[i][1] < newInterval[0]) {
7+
result.push(intervals[i]);
8+
i++;
9+
}
10+
11+
// 2. Merge all overlapping intervals with new interval
12+
while (i < intervals.length && intervals[i][0] <= newInterval[1]) {
13+
newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
14+
newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
15+
i++;
16+
}
17+
result.push(newInterval);
18+
19+
// 3. Add all intervals after the new interval
20+
while (i < intervals.length && intervals[i][1] >= newInterval[0]) {
21+
result.push(intervals[i]);
22+
i++;
23+
}
24+
25+
return result;
26+
};
27+
28+
// TC = O(n)
29+
// SC = O(n)

insert-interval/yolophg.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Time Complexity: O(n log n)
2+
// Space Complexity: O(n)
3+
4+
var insert = function (intervals, newInterval) {
5+
// append the newInterval to the intervals array
6+
intervals.push(newInterval);
7+
8+
// sort the intervals array by start time
9+
intervals.sort((a, b) => a[0] - b[0]);
10+
11+
// to store merged intervals
12+
let result = [];
13+
14+
// iterate through the sorted intervals array
15+
for (let i = 0; i < intervals.length; i++) {
16+
// if the result array is empty or the current interval does not overlap with the last interval
17+
if (result.length === 0 || result[result.length - 1][1] < intervals[i][0]) {
18+
result.push(intervals[i]); // Add the current interval to the result array
19+
} else {
20+
// if there is an overlap, merge the current interval with the last interval
21+
result[result.length - 1][1] = Math.max(
22+
result[result.length - 1][1],
23+
intervals[i][1]
24+
);
25+
}
26+
}
27+
28+
// return the final merged intervals
29+
return result;
30+
};

jump-game/bky373.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// time: O(N)
2+
// space: O(1)
3+
class Solution {
4+
5+
public boolean canJump(int[] nums) {
6+
int lastPosition = nums.length - 1;
7+
8+
for (int i = nums.length - 1; i >= 0; i--) {
9+
if (i + nums[i] >= lastPosition) {
10+
lastPosition = i;
11+
}
12+
}
13+
return lastPosition == 0;
14+
}
15+
}

jump-game/evan.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def canJump(nums):
2+
n = len(nums)
3+
dp = [False] * n
4+
dp[0] = True
5+
6+
for i in range(n):
7+
if dp[i]:
8+
maxJump = min(i + nums[i], n - 1)
9+
10+
for j in range(i + 1, maxJump + 1):
11+
dp[j] = True
12+
13+
return dp[n - 1]

jump-game/samthekorean.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
class Solution:
4+
def canJump(self, nums):
5+
reachable = 0
6+
for i in range(len(nums)):
7+
if i > reachable:
8+
return False
9+
reachable = max(reachable, i + nums[i])
10+
return True
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// time: O(m*n)
2+
// space: O(m*n)
3+
class Solution {
4+
5+
public int longestCommonSubsequence(String text1, String text2) {
6+
int[][] dp = new int[text1.length() + 1][text2.length() + 1];
7+
8+
for (int col = text2.length() - 1; col >= 0; col--) {
9+
for (int row = text1.length() - 1; row >= 0; row--) {
10+
if (text1.charAt(row) == text2.charAt(col)) {
11+
dp[row][col] = 1 + dp[row + 1][col + 1];
12+
} else {
13+
dp[row][col] = Math.max(dp[row + 1][col], dp[row][col + 1]);
14+
}
15+
}
16+
}
17+
18+
return dp[0][0];
19+
}
20+
}

longest-common-subsequence/evan.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def longestCommonSubsequence(self, text1, text2):
3+
text1_length, text2_length = len(text1), len(text2)
4+
# dp[i][j]는 text1의 처음 i 글자와 text2의 처음 j 글자의 LCS 길이를 의미합니다.
5+
dp = [[0] * (text2_length + 1) for _ in range(text1_length + 1)]
6+
7+
for text1_char in range(1, text1_length + 1):
8+
for text2_char in range(1, text2_length + 1):
9+
if text1[text1_char - 1] == text2[text2_char - 1]:
10+
# 두 문자가 같으면 그 이전까지의 LCS 길이에 1을 더한 값으로 현재 위치를 갱신합니다.
11+
dp[text1_char][text2_char] = dp[text1_char - 1][text2_char - 1] + 1
12+
else:
13+
# 두 문자가 다르면 이전까지의 최대 LCS 길이 중 더 큰 값을 현재 위치에 저장합니다.
14+
dp[text1_char][text2_char] = max(
15+
dp[text1_char - 1][text2_char], dp[text1_char][text2_char - 1]
16+
)
17+
18+
return dp[text1_length][text2_length]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# m is the length of text1 and n is the length of text2
2+
# TC : O(m * n)
3+
# SC : O(m * n)
4+
class Solution:
5+
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
6+
dp = [[0] * (len(text2) + 1) for _ in range(len(text1) + 1)]
7+
for r in range(1, len(text1) + 1):
8+
for c in range(1, len(text2) + 1):
9+
if text1[r - 1] == text2[c - 1]:
10+
dp[r][c] = 1 + dp[r - 1][c - 1]
11+
else:
12+
dp[r][c] = max(dp[r - 1][c], dp[r][c - 1])
13+
return dp[-1][-1]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// time: O(N^2)
2+
// space: O(N)
3+
class Solution {
4+
5+
public int lengthOfLIS(int[] nums) {
6+
int[] dp = new int[nums.length];
7+
Arrays.fill(dp, 1);
8+
9+
for (int i = 1; i < nums.length; i++) {
10+
for (int j = 0; j < i; j++) {
11+
if (nums[i] > nums[j]) {
12+
dp[i] = Math.max(dp[i], dp[j] + 1);
13+
}
14+
}
15+
}
16+
17+
int longest = 0;
18+
for (int count : dp) {
19+
longest = Math.max(longest, count);
20+
}
21+
22+
return longest;
23+
}
24+
}

0 commit comments

Comments
 (0)