Skip to content

Commit 90b2c50

Browse files
author
mykoo
committed
Merge remote-tracking branch 'upstream/main'
2 parents 971815e + bde1ed2 commit 90b2c50

File tree

15 files changed

+695
-0
lines changed

15 files changed

+695
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TC: O(n)
2+
// SC: O(n)
3+
class Solution {
4+
public int longestConsecutive(int[] nums) {
5+
int output = 0;
6+
Set<Integer> set = new HashSet<>();
7+
8+
for (int num : nums) set.add(num);
9+
10+
for (int num : nums) {
11+
int count = 1;
12+
if (!set.contains(num - count)){
13+
while (set.contains(num + count)) {
14+
count += 1;
15+
}
16+
}
17+
output = Math.max(output, count);
18+
}
19+
return output;
20+
}
21+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.max
6+
7+
class `longest-consecutive-sequence` {
8+
fun longestConsecutive(nums: IntArray): Int {
9+
if (nums.isEmpty()) return 0
10+
return usingUnionFind(nums)
11+
}
12+
13+
/**
14+
* 1. 배열을 μ •λ ¬ν•˜μ—¬ μˆœμ„œλŒ€λ‘œ μˆœνšŒν•˜λ©° 연속 μˆ˜μ—΄ 길이λ₯Ό ν™•μΈν•œλ‹€.
15+
* TC: O(n * log(n)), SC: O(1)
16+
*/
17+
private fun usingSort(nums: IntArray): Int {
18+
nums.sort()
19+
20+
var (length, maxLength) = 1 to 0
21+
for (index in 0 until nums.size - 1) {
22+
if (nums[index] == nums[index + 1]) {
23+
continue
24+
} else if (nums[index] + 1 == nums[index + 1]) {
25+
length++
26+
} else {
27+
maxLength = max(length, maxLength)
28+
length = 1
29+
}
30+
}
31+
return max(length, maxLength)
32+
}
33+
34+
/**
35+
* 2. Set의 자료ꡬ쑰λ₯Ό ν™œμš©ν•˜μ—¬ κ°€μž₯ μž‘μ€ κ°’λΆ€ν„° while문을 ν†΅ν•œ μ΅œλŒ€ 증가 값을 λ°˜ν™˜ν•œλ‹€.
36+
* TC: O(n), SC: O(n)
37+
*/
38+
private fun usingSet(nums: IntArray): Int {
39+
val numberSet = nums.toSet()
40+
var maxLength = 0
41+
42+
for (number in nums) {
43+
if (numberSet.contains(number - 1)) {
44+
continue
45+
}
46+
var length = 1
47+
while (numberSet.contains(number + length)) {
48+
length++
49+
}
50+
maxLength = max(maxLength, length)
51+
}
52+
53+
return maxLength
54+
}
55+
56+
/**
57+
* 3. Union-Find
58+
* TC: O(n), SC: O(n)
59+
*/
60+
private fun usingUnionFind(nums: IntArray): Int {
61+
val nodes = mutableMapOf<Int, Int>()
62+
val dsu = DSU(nums.size)
63+
64+
for ((i,n) in nums.withIndex()) {
65+
if (n in nodes) continue
66+
67+
nodes[n - 1]?.let { dsu.union(i, it) }
68+
nodes[n + 1]?.let { dsu.union(i, it) }
69+
70+
nodes[n] = i
71+
}
72+
73+
return dsu.maxLength()
74+
}
75+
76+
@Test
77+
fun `μž…λ ₯받은 μ •μˆ˜ λ°°μ—΄μ˜ μ΅œλŒ€ 연속 μˆ˜μ—΄ 길이λ₯Ό λ°˜ν™˜ν•œλ‹€`() {
78+
longestConsecutive(intArrayOf()) shouldBe 0
79+
longestConsecutive(intArrayOf(100,4,200,1,3,2)) shouldBe 4
80+
longestConsecutive(intArrayOf(11,23,12,13,14,21)) shouldBe 4
81+
longestConsecutive(intArrayOf(0,3,7,2,5,8,4,6,0,1)) shouldBe 9
82+
longestConsecutive(intArrayOf(11,64,43,12,13,10,9,8,7)) shouldBe 7
83+
}
84+
}
85+
86+
class DSU(val n: Int) {
87+
private val parent = IntArray(n) { it }
88+
private val size = IntArray(n) { 1 }
89+
90+
private fun find(x: Int): Int {
91+
if (parent[x] != x)
92+
parent[x] = find(parent[x])
93+
return parent[x]
94+
}
95+
96+
fun union(x: Int, y: Int) {
97+
val root = find(x)
98+
val child = find(y)
99+
if(root != child) {
100+
parent[child] = root
101+
size[root] += size[child]
102+
}
103+
}
104+
105+
fun maxLength(): Int {
106+
var res = 0
107+
for (i in parent.indices) {
108+
if (parent[i] == i)
109+
res = maxOf(res, size[i])
110+
}
111+
return res
112+
}
113+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function longestConsecutive(nums: number[]): number {
6+
const n = nums.length;
7+
8+
if (n <= 1) {
9+
return n;
10+
}
11+
12+
const numsSet = new Set(nums);
13+
let longestLen = 0;
14+
15+
for (let i = 0; i < n; i++) {
16+
let current = nums[i];
17+
18+
if (!numsSet.has(current - 1)) {
19+
let currentLen = 1;
20+
21+
while (numsSet.has(current + 1)) {
22+
current++;
23+
currentLen++;
24+
}
25+
26+
longestLen = Math.max(longestLen, currentLen);
27+
}
28+
}
29+
return longestLen;
30+
}
31+
32+
const t1 = longestConsecutive([100, 4, 200, 1, 3, 2]);
33+
console.info("πŸš€ : tolluset.ts:5: t1=", t1); // 4
34+
35+
const t2 = longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]);
36+
console.info("πŸš€ : tolluset.ts:8: t2=", t2); // 9
37+
38+
const t3 = longestConsecutive([9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]);
39+
console.info("πŸš€ : tolluset.ts:40: t3=", t3); // 7
40+
41+
const t4 = longestConsecutive([-6, -1, -1, 9, -8, -6, -6, 4, 4, -3, -8, -1]);
42+
console.info("πŸš€ : tolluset.ts:59: t4=", t4); // 1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
class Solution {
4+
public int maxProduct(int[] nums) {
5+
int currentMax = nums[0];
6+
int currentMin = nums[0];
7+
int maxProduct = nums[0];
8+
9+
for (int i = 1; i < nums.length; i++) {
10+
if (nums[i] < 0) {
11+
int temp = currentMax;
12+
currentMax = currentMin;
13+
currentMin = temp;
14+
}
15+
16+
currentMax = Math.max(nums[i], currentMax * nums[i]);
17+
currentMin = Math.min(nums[i], currentMin * nums[i]);
18+
19+
maxProduct = Math.max(maxProduct, currentMax);
20+
}
21+
22+
return maxProduct;
23+
}
24+
}

β€Žmaximum-product-subarray/jdalma.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import kotlin.math.max
6+
import kotlin.math.min
7+
8+
class `maximum-product-subarray` {
9+
10+
fun maxProduct(nums: IntArray): Int {
11+
return usingOptimizedDP(nums)
12+
}
13+
14+
/**
15+
* ν˜„μž¬μ˜ κ°’, 이전 μœ„μΉ˜μ˜ μ΅œλŒ€ λˆ„μ κ³±, 이전 μœ„μΉ˜μ˜ μ΅œμ†Œ λˆ„μ κ³± 이 μ„Έ 개λ₯Ό λΉ„κ΅ν•˜μ—¬ ν•œ 번의 순회둜 μ΅œλŒ€ 값을 λ°˜ν™˜ν•œλ‹€.
16+
* μŒμˆ˜μ™€ μŒμˆ˜κ°€ κ³±ν•΄μ Έ μ΅œλŒ€ 값이 λ„μΆœλ  수 μžˆκΈ°μ— DP 배열을 두 개 μƒμ„±ν•œλ‹€.
17+
* TC: O(n), SC: O(n)
18+
*/
19+
private fun usingDP(nums: IntArray): Int {
20+
val (min, max) = IntArray(nums.size) { 11 }.apply { this[0] = nums[0] } to IntArray(nums.size) { -11 }.apply { this[0] = nums[0] }
21+
var result = nums[0]
22+
for (index in 1 until nums.size) {
23+
max[index] = max(max(nums[index], nums[index] * max[index - 1]), nums[index] * min[index - 1])
24+
min[index] = min(min(nums[index], nums[index] * max[index - 1]), nums[index] * min[index - 1])
25+
result = max(max(min[index], max[index]), result)
26+
}
27+
28+
return result
29+
}
30+
31+
/**
32+
* DP 배열이 μž…λ ₯λ°›λŠ” μ •μˆ˜μ˜ λ°°μ—΄λ§ŒνΌ 생성할 ν•„μš”κ°€ μ—†κ³ , 이전 κ°’κ³Ό ν˜„μž¬ κ°’λ§Œ κΈ°μ–΅ν•˜λ©΄ λ˜λ―€λ‘œ κ³΅κ°„λ³΅μž‘λ„κ°€ κ°œμ„ λ˜μ—ˆλ‹€.
33+
* TC: O(n), SC: O(1)
34+
*/
35+
private fun usingOptimizedDP(nums: IntArray): Int {
36+
var (min, max) = nums[0] to nums[0]
37+
var result = nums[0]
38+
for (index in 1 until nums.size) {
39+
val (tmpMin, tmpMax) = min to max
40+
max = max(max(nums[index], nums[index] * tmpMax), nums[index] * tmpMin)
41+
min = min(min(nums[index], nums[index] * tmpMax), nums[index] * tmpMin)
42+
result = max(max(min, max), result)
43+
}
44+
45+
return result
46+
}
47+
48+
@Test
49+
fun `μž…λ ₯받은 μ •μˆ˜ λ°°μ—΄μ˜ κ°€μž₯ 큰 곱을 λ°˜ν™˜ν•œλ‹€`() {
50+
maxProduct(intArrayOf(2,3,-2,4)) shouldBe 6
51+
maxProduct(intArrayOf(-2,0,-1)) shouldBe 0
52+
maxProduct(intArrayOf(-10)) shouldBe -10
53+
maxProduct(intArrayOf(-2,3,-4)) shouldBe 24
54+
maxProduct(intArrayOf(-4,-3,-2)) shouldBe 12
55+
}
56+
}

β€Žmaximum-subarray/tolluset.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(1)
4+
* */
5+
function maxProduct(nums: number[]): number {
6+
const n = nums.length;
7+
8+
if (n === 1) {
9+
return nums[0];
10+
}
11+
12+
let max = 0,
13+
min = 0,
14+
res = 0;
15+
16+
for (let i = 0; i < n; i++) {
17+
const cur = nums[i];
18+
19+
if (cur < 0) {
20+
[max, min] = [min, max];
21+
}
22+
23+
max = Math.max(cur, max * cur);
24+
min = Math.min(cur, min * cur);
25+
26+
res = Math.max(res, max);
27+
}
28+
29+
return res;
30+
}
31+
32+
const t1 = maxProduct([2, 3, -2, 4]);
33+
console.info("πŸš€ : tolluset.ts:3: t1=", t1); // 6
34+
35+
const t2 = maxProduct([-2, 0, -1]);
36+
console.info("πŸš€ : tolluset.ts:6: t2=", t2); // 0
37+
38+
const t3 = maxProduct([-2]);
39+
console.info("πŸš€ : tolluset.ts:34: t3=", t3); // -2

β€Žmissing-number/TonyKim9401.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// TC: O(n)
2+
// -> add all nums into set
3+
// SC: O(n)
4+
// -> set contains all nums' elements
5+
class Solution {
6+
public int missingNumber(int[] nums) {
7+
Set<Integer> set = new HashSet<>();
8+
for (int num : nums) set.add(num);
9+
10+
int output = 0;
11+
while (set.contains(output)) output += 1;
12+
13+
return output;
14+
}
15+
}

β€Žmissing-number/jdalma.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
class `missing-number` {
7+
8+
fun missingNumber(nums: IntArray): Int {
9+
return usingSum(nums)
10+
}
11+
12+
/**
13+
* 1. 배열을 μΆ”κ°€λ‘œ μƒμ„±ν•˜μ—¬ μ‘΄μž¬ν•˜λŠ” μ •μˆ˜μ˜ μΈλ±μŠ€μ— trueλ₯Ό μ €μž₯ν•˜μ—¬, false인 인덱슀λ₯Ό λ°˜ν™˜ν•œλ‹€.
14+
* TC: O(n), SC: O(n)
15+
*/
16+
private fun usingIndex(nums: IntArray): Int {
17+
val existed = BooleanArray(nums.size + 1)
18+
nums.forEach { existed[it] = true }
19+
20+
existed.forEachIndexed { i, e ->
21+
if (!e) return i
22+
}
23+
return -1
24+
}
25+
26+
/**
27+
* 2. 0λΆ€ν„° μ •μˆ˜ λ°°μ—΄μ˜ μ‚¬μ΄μ¦ˆλ§ŒνΌ μ •μˆ˜λ₯Ό ν•©μ‚°ν•˜μ—¬ κΈ°λŒ€ν•˜λŠ” ν•©μ‚°κ³Ό λΊ€ κ²°κ³Όλ₯Ό λ°˜ν™˜ν•œλ‹€.
28+
* TC: O(n), SC: O(1)
29+
*/
30+
private fun usingSum(nums: IntArray): Int {
31+
val size = nums.size
32+
return nums.fold((size + 1) * size / 2) { acc , i -> acc - i }
33+
}
34+
35+
@Test
36+
fun `μž…λ ₯받은 μ •μˆ˜ λ°°μ—΄μ—μ„œ λΉ„μ–΄μžˆλŠ” μ •μˆ˜λ₯Ό λ°˜ν™˜ν•œλ‹€`() {
37+
missingNumber(intArrayOf(3,2,1)) shouldBe 0
38+
missingNumber(intArrayOf(3,0,1)) shouldBe 2
39+
missingNumber(intArrayOf(9,6,4,2,3,5,7,0,1)) shouldBe 8
40+
}
41+
}

β€Žmissing-number/tolluset.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(1)
4+
* */
5+
function missingNumber(nums: number[]): number {
6+
const n = nums.length;
7+
8+
const sum = (n * (n + 1)) / 2;
9+
const actualSum = nums.reduce((acc, curr) => acc + curr, 0);
10+
11+
return sum - actualSum;
12+
}
13+
14+
const t1 = missingNumber([3, 0, 1]);
15+
console.info("πŸš€ : tolluset.ts:3: t1=", t1); // 2
16+
17+
const t2 = missingNumber([0, 1]);
18+
console.info("πŸš€ : tolluset.ts:6: t2=", t2); // 2
19+
20+
const t3 = missingNumber([9, 6, 4, 2, 3, 5, 7, 0, 1]);
21+
console.info("πŸš€ : tolluset.ts:9: t3=", t3); // 8

0 commit comments

Comments
Β (0)