Skip to content

Commit 1f19ed8

Browse files
author
mykoo
committed
Merge remote-tracking branch 'upstream/main'
2 parents 90b2c50 + 9ae8457 commit 1f19ed8

31 files changed

+946
-0
lines changed

longest-common-subsequence/kayden.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 시간복잡도: O(N)
2+
# 공간복잡도: O(N)
3+
class Solution:
4+
def longestConsecutive(self, nums: List[int]) -> int:
5+
nums = set(nums)
6+
answer = 0
7+
8+
for num in nums:
9+
if num - 1 not in nums:
10+
length = 1
11+
12+
while num + length in nums:
13+
length += 1
14+
15+
answer = max(answer, length)
16+
17+
return answer
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* https://leetcode.com/problems/longest-consecutive-sequence/
3+
* T.C.: O(n)
4+
* S.C.: O(n)
5+
*/
6+
function longestConsecutive(nums: number[]): number {
7+
const numSet = new Set(nums);
8+
let max = 0;
9+
10+
for (const num of numSet) {
11+
if (numSet.has(num - 1)) {
12+
continue;
13+
}
14+
15+
let count = 0;
16+
while (numSet.has(num + count)) {
17+
count++;
18+
}
19+
20+
if (count > max) max = count;
21+
}
22+
23+
return max;
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* https://leetcode.com/problems/longest-consecutive-sequence
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
const findStreak = (set: Set<number>) => (num: number): number => {
7+
if (!set.has(num - 1)) return takeWhile(num, currentNum => set.has(currentNum)).length;
8+
return 0;
9+
};
10+
11+
const takeWhile = (start: number, predicate: (value: number) => boolean): number[] => {
12+
const result: number[] = [];
13+
let currentNum = start;
14+
while (predicate(currentNum)) {
15+
result.push(currentNum);
16+
currentNum += 1;
17+
}
18+
return result;
19+
}
20+
21+
const max = (maxStreak: number, currentStreak: number): number => Math.max(maxStreak, currentStreak);
22+
23+
function longestConsecutive(nums: number[]): number {
24+
const numSet = new Set(nums);
25+
26+
return [...numSet]
27+
.map(findStreak(numSet))
28+
.reduce(max, 0);
29+
}
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(n)
3+
4+
const longestConsecutive = function (nums) {
5+
let longest = 0;
6+
const set = new Set(nums);
7+
8+
while (set.size > 0) {
9+
let count = 0;
10+
const originalSeed = set.values().next().value; //set의 첫 번째 원소 get
11+
12+
seed = originalSeed;
13+
14+
while (set.has(seed)) {
15+
set.delete(seed);
16+
count++;
17+
seed += 1;
18+
}
19+
20+
seed = originalSeed - 1;
21+
22+
while (set.has(seed)) {
23+
set.delete(seed);
24+
count++;
25+
seed -= 1;
26+
}
27+
28+
if (count > longest) longest = count;
29+
}
30+
31+
return longest;
32+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class SolutionLongestConsecutiveSequence {
5+
6+
public int longestConsecutive(int[] nums) {
7+
// 정렬되지 않은 정수 nums 배열이 주어지면 가장 긴 연속 요소 시퀀스 길이를 반환
8+
// O(N) 시간 내 실행되야함
9+
// 전부 해시맵에 때려넣고, 키를 꺼내 연속 요소가 있는지 확인한다
10+
// 연속 요소가 있으면 answer를 1 증가시키고, 연속 요소는 제거한다
11+
// 시간복잡도: O(N), 공간복잡도: O(N)
12+
13+
Set<Integer> set = new HashSet<>();
14+
for (var num : nums) {
15+
set.add(num);
16+
}
17+
var answer = 0;
18+
for (var num : nums) {
19+
var length = 1;
20+
21+
if (set.contains(num-1)) {
22+
set.remove(num);
23+
var minusKey = num;
24+
while (set.contains(--minusKey)) {
25+
length++;
26+
set.remove(minusKey);
27+
}
28+
}
29+
30+
if (set.contains(num+1)) {
31+
set.remove(num);
32+
var plusKey = num;
33+
while (set.contains(++plusKey)) {
34+
length++;
35+
set.remove(plusKey);
36+
}
37+
}
38+
39+
if (length > answer) {
40+
answer = length;
41+
}
42+
}
43+
44+
return answer;
45+
}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var longestConsecutive = function (nums) { // sorting approach
2+
if (!nums.length) return 0;
3+
let set = new Set(nums);
4+
let sorted = [...set].sort((a, b) => a - b);
5+
let longestSeq = 0;
6+
let currSeq = 1;
7+
8+
for (let i = 1; i < sorted.length; i++) { // loop through sorted list to find sequence
9+
if (sorted[i - 1] + 1 === sorted[i]) {
10+
currSeq++;
11+
} else {
12+
longestSeq = Math.max(longestSeq, currSeq); // compare sequence to figure out the longest
13+
currSeq = 1;
14+
}
15+
}
16+
17+
return Math.max(longestSeq, currSeq);
18+
};
19+
20+
// time - O(nlong) using sort
21+
// space - O(n) store nums in set
22+
23+
24+
// TODO - try O(n) TC approach

maximum-product-subarray/HC-kang.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* https://leetcode.com/problems/maximum-product-subarray
3+
* T.C. O(n)
4+
* S.C. O(1)
5+
* All numbers are integers, so multiplying two numbers cannot result in a smaller absolute value.
6+
* It's important to pay attention to zeros and negative numbers.
7+
*/
8+
function maxProduct(nums: number[]): number {
9+
if (nums.length === 0) return 0;
10+
11+
let max = nums[0];
12+
let min = nums[0];
13+
let result = nums[0];
14+
15+
for (let i = 1; i < nums.length; i++) {
16+
const num = nums[i];
17+
if (num < 0) [max, min] = [min, max];
18+
19+
max = Math.max(num, max * num);
20+
min = Math.min(num, min * num);
21+
22+
result = Math.max(result, max);
23+
}
24+
25+
return result;
26+
}

maximum-product-subarray/gitsunmin.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/maximum-product-subarray
3+
* time complexity : O(n)
4+
* space complexity : O(1)
5+
*/
6+
function maxProduct(nums: number[]): number {
7+
let r = nums[0];
8+
let mx = 1, mn = 1;
9+
10+
for (let i = 0; i < nums.length; i++) {
11+
const tempMx = mx * nums[i];
12+
const tempMn = mn * nums[i];
13+
14+
mx = Math.max(tempMx, tempMn, nums[i]);
15+
mn = Math.min(tempMx, tempMn, nums[i]);
16+
17+
r = Math.max(r, mx);
18+
}
19+
20+
return r;
21+
}

maximum-product-subarray/highball.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//time-complexity : O(n)
2+
//space-complexity : O(n)
3+
4+
const maxProduct = function (nums) {
5+
let subarrays = [];
6+
let subarray = [];
7+
let zeroCount = 0;
8+
9+
/* -------------------------------------------------------------------------- */
10+
/* nonzero subarray 잘라서 subarrays에 넣기 */
11+
/* -------------------------------------------------------------------------- */
12+
for (let i = 0; i < nums.length; i++) {
13+
if (nums[i] === 0) {
14+
if (subarray.length > 0) {
15+
subarrays.push([...subarray]);
16+
subarray.length = 0; //각 element의 metadata를 garbage collection에 들어가야 하는 것으로 marking할 뿐이므로 O(1)!
17+
}
18+
zeroCount++;
19+
continue;
20+
}
21+
subarray.push(nums[i]);
22+
}
23+
if (subarray.length > 0) {
24+
subarrays.push([...subarray]);
25+
}
26+
27+
/* -------------------------------------------------------------------------- */
28+
/* 각 subarray의 maxProduct들 중 최대값 리턴하기 */
29+
/* -------------------------------------------------------------------------- */
30+
if (zeroCount === 0) {
31+
return maxProductNonzero(nums);
32+
} else {
33+
let max = 0;
34+
for (let i = 0; i < subarrays.length; i++) {
35+
max = Math.max(maxProductNonzero(subarrays[i]), max);
36+
}
37+
return max;
38+
}
39+
};
40+
41+
const maxProductNonzero = function (nonZeroNums) {
42+
if (nonZeroNums.length === 1) return nonZeroNums[0]; //firstNegativeIndex = lastNegativeIndex = 0이 되어 버려서 frontProduct와 backProduct 중 어느 것도 계산이 안 돼버리므로 early return으로 처리.
43+
44+
let firstNegativeIndex = -1;
45+
let lastNegativeIndex = -1;
46+
let negativeCount = 0;
47+
for (let i = 0; i < nonZeroNums.length; i++) {
48+
if (nonZeroNums[i] < 0 && firstNegativeIndex !== lastNegativeIndex) {
49+
lastNegativeIndex = i;
50+
negativeCount++;
51+
}
52+
if (nonZeroNums[i] < 0 && firstNegativeIndex === lastNegativeIndex) {
53+
firstNegativeIndex = i;
54+
negativeCount++;
55+
}
56+
}
57+
58+
if (negativeCount === 1) {
59+
lastNegativeIndex = firstNegativeIndex;
60+
}
61+
62+
if (negativeCount % 2 === 0) {
63+
/* -------------------------------------------------------------------------- */
64+
/* 음수 개수가 짝수 개면 그냥 전체 곱한 게 최대값임 */
65+
/* -------------------------------------------------------------------------- */
66+
let product = 1;
67+
for (let i = 0; i < nonZeroNums.length; i++) {
68+
product *= nonZeroNums[i];
69+
}
70+
return product;
71+
} else {
72+
/* -------------------------------------------------------------------------- */
73+
/* 음수 개수가 홀수 개면 처음부터 lastNegativeIndex 직전까지 곱한 수 혹은 */
74+
/* firstNegativeIndex부터 끝까지 곱한 수 중 하나가 최대값임 */
75+
/* -------------------------------------------------------------------------- */
76+
let frontProduct = 1;
77+
let backProduct = 1;
78+
for (let i = 0; i < nonZeroNums.length; i++) {
79+
if (i < lastNegativeIndex) frontProduct *= nonZeroNums[i];
80+
if (i > firstNegativeIndex) backProduct *= nonZeroNums[i];
81+
}
82+
return Math.max(frontProduct, backProduct);
83+
}
84+
};

maximum-product-subarray/kayden.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 시간복잡도: O(N)
2+
# 공간복잡도: O(1)
3+
class Solution:
4+
def maxProduct(self, nums: List[int]) -> int:
5+
n = len(nums)
6+
positive, negative = 0, 0
7+
8+
if nums[0] > 0:
9+
positive = nums[0]
10+
else:
11+
negative = nums[0]
12+
13+
answer = max(nums)
14+
15+
for i in range(1, n):
16+
if nums[i] >= 0:
17+
positive *= nums[i]
18+
negative *= nums[i]
19+
20+
if positive == 0:
21+
positive = nums[i]
22+
23+
else:
24+
temp = positive
25+
positive = negative * nums[i]
26+
negative = temp * nums[i]
27+
28+
if negative == 0:
29+
negative = nums[i]
30+
31+
answer = max(answer, positive)
32+
33+
return answer

0 commit comments

Comments
 (0)