Skip to content

[kut7728] WEEK 02 solutions #1224

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 12, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 34 additions & 0 deletions climbing-stairs/kut7728.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swift 코드는 처음보는데 자바스크립트랑 유사한 느낌이군요! function 작성하는 건 뭔가 파이썬 쓰는 것 같네요.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
//계단 갯수 n 입력받음
func climbStairs(_ n: Int) -> Int {
var result = 0

///1계단씩 올라가는 경우 = 1스탭
///2계단씩 올라가는 경우 = 2스탭
///2스탭인 경우를 1씩 증가시켜줌
for i in 0...(n/2) {
///2스탭이 없는 경우 -> 전부 1스탭임
if i == 0 {
result += 1
continue
}

///n에서 2스탭 횟수를 빼서 1스탭 횟수 구하기
let x = n - (2 * i)

///조합계산식 (2스탭,1스탭 전체 횟수 C 2스탭 횟수)
result += ncm(x+i, i)
}

return result
}

///ncm함수로 조합 계산식을 구현
func ncm(_ n: Int, _ m: Int) -> Int {
if m == 1 { return n } ///nC1 이라면 전체횟수 n 반환
if m == n { return 1 } ///nCn 이라면 1반환

///조합 계산식을 코드로 계산할 수 있도록 최적화하면 다음과 같아짐
return (1...m).reduce(1) { $0 * ($1 + n-m)/$1 }
}
}
26 changes: 26 additions & 0 deletions product-of-array-except-self/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
///정수배열 nums가 주어질때 정수배열 answer를 반환하시오
///answer의 각 요소는 nums의 같은 인덱스의 요소를 제외한 나머지 요소의 곱

//복잡도 O(n)
class Solution {
func productExceptSelf(_ nums: [Int]) -> [Int] { //nums = 1,2,3,4
let n = nums.count
var answer = [Int](repeating: 1, count: n) //answer = 1,1,1,1
var left = [Int](repeating: 1, count: n) //left = 1,1,1,1

let revNums = Array(nums.reversed()) //revNums = 4,3,2,1
var right = [Int](repeating: 1, count: n) //right = 1,1,1,1

for i in 1..<n { //i = 1,2,3
left[i] = left[i-1] * nums[i-1] //left = 1,1,2,6
right[i] = right[i-1] * revNums[i-1] //right = 1,4,12,24
}

for i in answer.indices {
answer[i] = left[i] * right[n-i-1]
}

return answer

}
}
20 changes: 20 additions & 0 deletions valid-anagram/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//문자열 s, t를 받고, t가 s의 애너그램이면 true, 아니면 false 출력

class Solution {
func isAnagram(_ s: String, _ t: String) -> Bool {
///둘이 문자 갯수가 다르다면 바로 False
if s.count != t.count { return false }

///해쉬테이블 사용
var wordDic: [Character: Int] = [:]

///s의 글자들은 1씩 추가, t의 글자들은 1씩 감소
for (sChar, tChar) in zip(s, t) {
wordDic[sChar, default: 0] += 1
wordDic[tChar, default: 0] -= 1
}

///딕셔너리의 모든 값이 상쇄되어 0이 되면 true
return wordDic.values.allSatisfy { $0 == 0 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아예 조건으로 상쇄되는 경우를 체크하는 건 좋은 방법이네요! 참고하겠습니다.

}
}