-
-
Notifications
You must be signed in to change notification settings - Fork 245
[HISEHOONAN] WEEK 02 solutions #1236
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// | ||
// 230.swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 4/8/25. | ||
// | ||
|
||
//Climbing Stairs | ||
|
||
class Solution { //다이나믹 프로그래밍. | ||
func climbStairs(_ n: Int) -> Int { | ||
if n < 4 {return n} // 0부터 4보다 작을때 까지는 걍 그 숫자 리턴 | ||
|
||
var dp = [0,1,2,3] //dp에 쓸 기본 배열. | ||
|
||
for i in 4...n{ | ||
dp.append(dp[i-2] + dp[i-1]) // 숫자의 전 + 숫자의 전전 = n의 숫자. | ||
} | ||
return dp[dp.count-1] //마지막 배열 리턴 | ||
} | ||
} | ||
|
||
// 패턴 | ||
|
||
/* | ||
|
||
** 1 or 2계단** | ||
--------------------------- | ||
n = 1 | ||
|
||
1스탭 | ||
--------------------------- | ||
n = 2 | ||
1 + 1 | ||
|
||
2 | ||
|
||
2스탭 | ||
--------------------------- | ||
n = 3 | ||
1 + 1 + 1 | ||
|
||
2 + 1 | ||
1 + 2 | ||
|
||
3스탭 | ||
--------------------------- | ||
n = 4 | ||
1 + 1 + 1 + 1 | ||
|
||
2 + 1 + 1 | ||
1 + 2 + 1 | ||
1 + 1 + 2 | ||
|
||
2 + 2 | ||
|
||
5스탭 | ||
n + 1 | ||
--------------------------- | ||
n = 5 | ||
1 + 1 + 1 + 1 + 1 | ||
|
||
2 + 1 + 1 + 1 | ||
1 + 2 + 1 + 1 | ||
1 + 1 + 2 + 1 | ||
1 + 1 + 1 + 2 | ||
|
||
2 + 2 + 1 | ||
2 + 1 + 2 | ||
1 + 2 + 2 | ||
|
||
8스탭 | ||
n + 3 | ||
--------------------------- | ||
n = 6 | ||
1 + 1 + 1 + 1 + 1 + 1 | ||
|
||
2 + 1 + 1 + 1 + 1 | ||
1 + 2 + 1 + 1 + 1 | ||
1 + 1 + 2 + 1 + 1 | ||
1 + 1 + 1 + 2 + 1 | ||
1 + 1 + 1 + 1 + 2 | ||
|
||
2 + 2 + 1 + 1 | ||
2 + 1 + 2 + 1 | ||
2 + 1 + 1 + 2 | ||
|
||
1 + 2 + 2 + 1 | ||
1 + 2 + 1 + 2 | ||
|
||
1 + 1 + 2 + 2 | ||
|
||
2 + 2 + 2 | ||
|
||
13 스탭 | ||
--------------------------- | ||
n = 7 | ||
1 + 1 + 1 + 1 + 1 + 1 + 1 | ||
|
||
2 + 1 + 1 + 1 + 1 + 1 | ||
1 + 2 + 1 + 1 + 1 + 1 | ||
1 + 1 + 2 + 1 + 1 + 1 | ||
1 + 1 + 1 + 2 + 1 + 1 | ||
1 + 1 + 1 + 1 + 2 + 1 | ||
1 + 1 + 1 + 1 + 1 + 2 | ||
|
||
2 + 2 + 1 + 1 + 1 | ||
2 + 1 + 2 + 1 + 1 | ||
2 + 1 + 1 + 2 + 1 | ||
2 + 1 + 1 + 1 + 2 | ||
|
||
1 + 2 + 2 + 1 + 1 | ||
1 + 2 + 1 + 2 + 1 | ||
1 + 2 + 1 + 1 + 2 | ||
|
||
1 + 1 + 2 + 2 + 1 | ||
1 + 1 + 2 + 1 + 2 | ||
|
||
1 + 1 + 1 + 2 + 2 | ||
|
||
2 + 2 + 2 + 1 | ||
2 + 2 + 1 + 2 | ||
2 + 1 + 2 + 2 | ||
|
||
1 + 2 + 2 + 2 | ||
|
||
21개 | ||
|
||
n = 1 2 3 | 4 5 6 7 | ||
cnt = 1, 2, 3,| 5, 8, 13, 21 | ||
|
||
|
||
? 피보나치자나? | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// 239.swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 4/8/25. | ||
// | ||
|
||
//Product of Array Except Self | ||
class Solution { | ||
func productExceptSelf(_ nums: [Int]) -> [Int] { | ||
|
||
var array1 : [Int] = nums // 원래 배열 | ||
var array2 : [Int] = nums.reversed() // 뒤집은 배열 | ||
|
||
var array1Forloop : [Int] = [] // 원래 배열을 계산 후 저장할 배열 | ||
var array2Forloop : [Int] = [] // 뒤집은 배열을 계산 후 저장할 배열 | ||
|
||
var multiply = 1 // 연산용 | ||
|
||
var result : [Int] = [] // 최종 결과를 담을 배열 | ||
|
||
// 원래 누적 곱 계산 (자기 자신 제외) | ||
for num in array1 { | ||
array1Forloop.append(multiply) // 현재까지의 누적 곱을 저장 (시작은1) | ||
multiply = num * multiply // 누적 곱 업데이트 | ||
} | ||
|
||
multiply = 1 //뒤집은 배열 계산을 위해 초기화 | ||
|
||
// 뒤집은 배열 누적 곱 계산 (자기 자신 제외) | ||
for num in array2 { | ||
array2Forloop.append(multiply) // 현재까지의 누적 곱을 저장 | ||
multiply = num * multiply // 누적 곱 업데이트 | ||
} | ||
|
||
array2Forloop = array2Forloop.reversed()// 뒤집은 배열 곱을 원래 순서로 되돌림 | ||
|
||
// 원래 배열 곱과 뒤집은 배열 곱의 인덱스가 같은놈들끼리 | ||
// 곱해서 최종 결과 생성 | ||
for i in 0..<nums.count { | ||
result.append(array1Forloop[i] * array2Forloop[i]) | ||
} | ||
|
||
return result | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sort를 해서 간편하게 비교할 수 있어서 좋았습니다!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 ! 제가 알기로도 �Swift에서도 시간복잡도가 O(n log n)으로 알고있습니다 ! introsort라는 알고리즘으로 작동합니다 ! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// 218.swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 4/8/25. | ||
// | ||
|
||
//Valid Anagram | ||
class Solution { | ||
func isAnagram(_ s: String, _ t: String) -> Bool { | ||
var sortS = s.sorted() //s를 sort하여 sortS에 저장 | ||
var sortT = t.sorted() //t를 sort하여 sortT에 저장 | ||
|
||
return sortS == sortT ? true : false //sortS와 sortT가 같다면 true 아니면 false | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
전과 전전만 있으면 될것같아서 n < 3 조건으로 해도 가능할것같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앗 그렇네요 ! 감사합니다 !