Skip to content

[kut7728] Week 03 solutions #1294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions combination-sum/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
///고유한 정수배열의 candidate와 목표 정수 target가 주어졌을 때,
///요소들을 더해서 target을 만들수 있는 candidate의 유니크한 조합 목록을 반환합니다.
///- 어떤 순서로든 조합을 반환할 수 있습니다.
///- 후보에서 같은 숫자를 무제한으로 선택할 수 있습니다.
///- 선택한 숫자 중 하나 이상의 빈도가 다른 경우 두 조합은 고유한 조합입니다.
///- 테스트 케이스는 주어진 입력에 대해 합산되는 고유 조합의 수가 150개 미만이 되도록 생성됩니다.
///Ex) candidate [2, 3, 6, 7] target = 7 -> [[2, 2, 3], [7]]


class Solution {
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
result: [[Int]] = []
nums: [Int] = []

func dfs(_ start: Int, _ total: Int) {
//타겟에 적중하면 result로 복사
if total == target {
result.append(nums)
return
}

//합이 타겟보다 크다면 그냥 종료
if total > target {
return
}

//start부터 시작하는것은 이전 요소들에 대한 조합은 이미 테스트했기 때문
for i in start..<candidates.count {
nums.append(candidates[i])
dfs(i, total + candidates[i])
nums.removeLast()
}
}

dfs(0, 0)
return result
}
}
33 changes: 33 additions & 0 deletions decode-ways/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
///정수 문자열 암호문을 습득했고, 1 -> A, 2 -> B ... 25 -> Y, 26 -> Z 이런식으로 해독됨
///정수 문자열을 어떻게 끊느냐에 따라 해독이 다르게 된다는 점을 고려해서
///해독 될수 있는 모든 경우의 수를 반환하라, 해독이 안된다면 0을 반환하라
///
///EX) s = "12"
///output : 2 -> (1 2) "AB" or (12) "L"


class Solution {
func numDecodings(_ s: String) -> Int {
let arr = Array(s)
var memo: [Int:Int] = [arr.count:1]

func dfs(_ start: Int) -> Int {
if let result = memo[start] {
return result
}


if arr[start] == "0" {
memo[start] = 0
} else if start + 1 < arr.count, Int(String(arr[start...start+1]))! <= 26 {
memo[start] = dfs(start+1) + dfs(start+2)
} else {
memo[start] = dfs(start + 1)
}

return memo[start]!
}

return dfs(0)
}
}
20 changes: 20 additions & 0 deletions maximum-subarray/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
///53. Maximum Subarray
///정수 배열 Nums가 주어질때, subarray들 중에서 가장 합이 큰걸 구하고 그 합을 반환하라
///subarray: 비지 않은 연속된 요소들의 배열


class Solution {
func maxSubArray(_ nums: [Int]) -> Int {
guard !nums.isEmpty else { return 0 }

var currentSum = nums[0]
var maxSum = nums[0]

for num in nums.dropFirst() {
currentSum = max(num, currentSum + num)
maxSum = max(maxSum, currentSum)
}

return maxSum
}
}
10 changes: 10 additions & 0 deletions number-of-1-bits/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
///양의 정수 n이 주어졌을 때,
///이진 표현으로 setbit의 수를 반환하는 함수(해밍 가중치라고도 함)를 작성합니다.
///setbit이란 이진수 중에서 1의 값을 가지는 비트의 갯수

class Solution {
func hammingWeight(_ n: Int) -> Int {
let count = String(n, radix: 2).filter { $0 == "1" }.count
return count
}
}
26 changes: 26 additions & 0 deletions valid-palindrome/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
///주어진 문자열에서 문자, 숫자가 아닌 경우를 지우고, 모두 소문자로 바꿨을때
///앞, 뒤에서 읽었을 때 동일한 경우를 palindrome이라고 부름
///문자열 s가 주어질때 palindrome이면 true, 아니면 False 리턴하시오

class Solution {
func isPalindrome(_ s: String) -> Bool {
let alpList = Set("abcdefghijklmnopqrstuvwxyz0123456789")

if s == " " { return true } // 공백인 경우 바로 true

var pureString = s.lowercased().filter { alpList.contains($0) }


let reverseString = String(pureString.reversed())

if pureString == reverseString { return true } else { return false }

}

//.isLetter, .isNumber이라는 끝내주는 메서드가 있었다...
func isPalindrome2(_ s: String) -> Bool {
let s = s.lowercased().filter { $0.isLetter || $0.isNumber }
return s == String(s.reversed())
}

}