Skip to content

Commit 77a2b14

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents a5d6ab6 + bde1ed2 commit 77a2b14

File tree

22 files changed

+850
-0
lines changed

22 files changed

+850
-0
lines changed

โ€Žclimbing-stairs/joon.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import Dict
2+
3+
4+
class Solution:
5+
# Time: O(n)
6+
# Space: O(n)
7+
# ---
8+
# 1. Prepare a table for caching. Initially put trivial answers for n = 1, 2.
9+
# Space: O(n)
10+
cacheTableOfAnswerByN: Dict[int, int] = {1: 1, 2: 2}
11+
12+
def climbStairs(self, n: int) -> int:
13+
# 2. Use caching.
14+
# Time: O(1)
15+
try: return self.cacheTableOfAnswerByN[n]
16+
except KeyError:
17+
# 3. Simply, the answers follow Fibonacci sequence. Use recursion.
18+
# O(n)
19+
answerBeforeTwoSteps = self.climbStairs(n - 2)
20+
# 4. Cache the answer during recursion.
21+
self.cacheTableOfAnswerByN[n - 2] = answerBeforeTwoSteps
22+
answerBeforeOneStep = self.climbStairs(n - 1)
23+
self.cacheTableOfAnswerByN[n - 1] = answerBeforeOneStep
24+
return answerBeforeTwoSteps + answerBeforeOneStep

โ€Žclimbing-stairs/mangodm-web.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
dp = [0] * (n + 1)
4+
dp[0], dp[1] = 1, 1
5+
6+
for i in range(2, n + 1):
7+
dp[i] = dp[i - 1] + dp[i - 2]
8+
9+
return dp[n]

โ€Žcombination-sum/joon.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import List
2+
from typing import Set
3+
from typing import Tuple
4+
5+
6+
class Solution:
7+
target: int
8+
candidates: List[int]
9+
answers: Set[Tuple[int]]
10+
11+
# Time: O(k^N)
12+
# Space: O(N^2)
13+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
14+
self.target = target
15+
self.candidates = candidates
16+
self.answers = set()
17+
self.findAnswers(0, [])
18+
return list(self.answers)
19+
20+
def findAnswers(self, currentSum: int, nums: List[int]):
21+
assert currentSum < self.target, "Given sum should be smaller than the target."
22+
for num in self.candidates:
23+
if currentSum + num < self.target:
24+
# 1. Continue finding.
25+
# Time: O(k^N)
26+
# Space: O(N^2). Max total size of all "nums" = 1 + 2 + ... + N.
27+
self.findAnswers(currentSum + num, nums + [num])
28+
if currentSum + num > self.target:
29+
# 2. Stop finding.
30+
continue
31+
if currentSum + num == self.target:
32+
# 3. Add the answer.
33+
self.answers.add(tuple(sorted(list(nums + [num]))))
34+
return
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+
}
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+
}

0 commit comments

Comments
ย (0)