Skip to content

Commit 34e4d86

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents bb29608 + af72b96 commit 34e4d86

File tree

15 files changed

+413
-0
lines changed

15 files changed

+413
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Contains_Duplicate.swift
3+
// Algorithm
4+
//
5+
// Created by 안세훈 on 3/31/25.
6+
//
7+
8+
import Foundation
9+
10+
class Solution {
11+
func containsDuplicate(_ nums: [Int]) -> Bool {
12+
return nums.count != Set(nums).count
13+
//Set : 중복된 값을 갖지 않음.
14+
//문제로 주어진 배열의 개수와 중복을 갖지않는 Set연산의 개수의 차이 비교
15+
//비교 후 다르다면 true 같다면 false
16+
}
17+
}

contains-duplicate/ayosecu.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
- Time Complexity: O(n), n = len(nums)
6+
- Space Complexity: O(N)
7+
- N = len(set_check) = The number of unique numbers
8+
- If there is no duplicated numbers, N = n
9+
"""
10+
def containsDuplicate(self, nums: List[int]) -> bool:
11+
set_check = set()
12+
13+
for num in nums:
14+
if num in set_check:
15+
return True
16+
else:
17+
set_check.add(num)
18+
19+
return False
20+
21+
tc = [
22+
([1, 2, 3, 1], True),
23+
([1, 2, 3, 4], False),
24+
([1,1,1,3,3,4,3,2,4,2], True)
25+
]
26+
27+
for i, (t, e) in enumerate(tc, 1):
28+
sol = Solution()
29+
result = sol.containsDuplicate(t)
30+
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")

contains-duplicate/kut7728.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// Time, Space Complexity O(N)
2+
3+
class Solution {
4+
func containsDuplicate(_ nums: [Int]) -> Bool {
5+
var seen = Set<Int>()
6+
for num in nums {
7+
if seen.contains(num) {
8+
return true
9+
}
10+
seen.insert(num)
11+
}
12+
return false
13+
}
14+
}

house-robber/HISEHOONAN.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// House_Robber .swift
3+
// Algorithm
4+
//
5+
// Created by 안세훈 on 3/31/25.
6+
//
7+
///https://leetcode.com/problems/house-robber/description/
8+
9+
class Solution {
10+
//Dynamic Programming
11+
func rob(_ nums: [Int]) -> Int {
12+
13+
if nums.count == 1 {return nums[0]} //배열이 1개인 경우
14+
if nums.count == 2 {return max(nums[0],nums[1])} //배열이 2개인 경우
15+
16+
var dp = [nums[0], max(nums[0],nums[1])]
17+
//제일 base가 되는 두 값을 찾는게 제일 중요함.
18+
//nums[0]은 nums[1]보다 무조건 작아야함.
19+
for i in 2..<nums.count{
20+
dp.append(max(dp[i-2]+nums[i],dp[i-1]))
21+
//dp배열의 i-2번째 배열 + nums의 i번째 배열 vs dp의 i-1번째 배열 중 큰걸 append
22+
//
23+
}
24+
25+
return dp[dp.count-1]
26+
}
27+
}

house-robber/ayosecu.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
- Time Complexity: O(n), n = len(nums)
6+
- Space Complexity: O(n), n = len(nums)
7+
"""
8+
def rob(self, nums: List[int]) -> int:
9+
# DP Formation
10+
# money[0] = 0
11+
# money[1] = nums[0]
12+
# money[i+1] = max(money[i-1] + nums[i], money[i])
13+
14+
money = [0] * (len(nums) + 1)
15+
money[1] = nums[0]
16+
for i in range(1, len(nums)):
17+
money[i+1] = max(money[i-1] + nums[i], money[i])
18+
19+
return money[-1]
20+
21+
tc = [
22+
([1, 2, 3, 1], 4),
23+
([2, 7, 9, 3, 1], 12),
24+
([1, 2, 0, 5, 10], 12)
25+
]
26+
27+
for i, (nums, e) in enumerate(tc, 1):
28+
sol = Solution()
29+
result = sol.rob(nums)
30+
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed! - Expected: {e}, Result: {result}")

house-robber/kut7728.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
func rob(_ nums: [Int]) -> Int {
3+
let n = nums.count
4+
if n == 0 { return 0 }
5+
if n == 1 { return nums[0] }
6+
if n == 2 { return max(nums[0], nums[1]) }
7+
8+
//dp[i]는 i번까지 고려했을 때 가능한 최대 금액
9+
var dp = [Int](repeating: 0, count: n)
10+
dp[0] = nums[0]
11+
dp[1] = max(nums[0], nums[1]) //첫째 or 둘째 집 중 더 비싼 집 털기
12+
13+
//각 dp의 자리에는 바로 전 값을 그대로 가져오거나(i를 안털기), 전전집까지 턴거+i턴 값 중에서 비싼쪽 저장
14+
for i in 2..<n {
15+
dp[i] = max(dp[i-1], dp[i-2] + nums[i])
16+
}
17+
18+
//마지막 집까지 고려한 최대 이익 반환하기
19+
return dp[n - 1]
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Longest_Consecutive_Sequence.swift
3+
// Algorithm
4+
//
5+
// Created by 안세훈 on 3/31/25.
6+
//
7+
8+
///https://leetcode.com/problems/longest-consecutive-sequence/
9+
10+
11+
class Solution {
12+
func longestConsecutive(_ nums: [Int]) -> Int {
13+
guard nums.count != 0 else { return 0 }
14+
15+
var sortednums = Array(Set(nums)).sorted() //중복제거 + 오름차순 정렬
16+
var maxcount = 1 //최대 카운드
17+
var count = 1 // 연산용 카운트
18+
19+
for i in 0..<sortednums.count - 1 {
20+
if sortednums[i] == sortednums[i+1]-1 {
21+
//i번째 정렬된 리스트 와 i+1번째 리스트의 값 - 1이 같을 경우
22+
count += 1 //카운트에 1+
23+
maxcount = max(maxcount, count) //maxcount는 연산과, max중 큰 것
24+
} else {
25+
count = 1 //아니면 1로 초기화.
26+
}
27+
}
28+
29+
return maxcount
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""
5+
- Time Complexity: O(n), n = len(set_nums) = The number of unique numbers.
6+
- Space Complexity: O(n)
7+
"""
8+
def longestConsecutive(self, nums: List[int]) -> int:
9+
set_nums = set(nums)
10+
longest = 0
11+
12+
for num in set_nums:
13+
if num - 1 not in set_nums:
14+
# Only check for the start number
15+
cnt = 1
16+
next_num = num + 1
17+
while next_num in set_nums:
18+
cnt += 1
19+
next_num += 1
20+
longest = max(longest, cnt)
21+
22+
return longest
23+
24+
tc = [
25+
([100,4,200,1,3,2], 4),
26+
([0,3,7,2,5,8,4,6,0,1], 9),
27+
([1,0,1,2], 3)
28+
]
29+
30+
for i, (nums, e) in enumerate(tc, 1):
31+
sol = Solution()
32+
result = sol.longestConsecutive(nums)
33+
print(f"TC {i} is Passed!" if result == e else f"TC {i} is Failed!, Expected: {e}, Result: {result}")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
func longestConsecutive(_ nums: [Int]) -> Int {
3+
let numSet = Set(nums)
4+
var longest = 0
5+
6+
for num in numSet {
7+
// 연속 수열의 시작점인지 확인
8+
if !numSet.contains(num - 1) {
9+
var currentNum = num
10+
var currentStreak = 1
11+
12+
while numSet.contains(currentNum + 1) {
13+
currentNum += 1
14+
currentStreak += 1
15+
}
16+
17+
longest = max(longest, currentStreak)
18+
}
19+
}
20+
21+
return longest
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Top_K_Frequent_Elements .swift
3+
// Algorithm
4+
//
5+
// Created by 안세훈 on 3/31/25.
6+
//
7+
8+
class Solution {
9+
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
10+
var dic : [Int : Int] = [:]
11+
12+
for i in nums {
13+
dic[i] = dic[i , default : 0] + 1
14+
}
15+
16+
var sortarray = dic.sorted{$0.value > $1.value}
17+
var answer : [Int] = []
18+
19+
for i in 0..<k{
20+
answer.append(sortarray[i].key)
21+
}
22+
print(sortarray)
23+
print(answer)
24+
return answer
25+
}
26+
}

0 commit comments

Comments
 (0)