Skip to content

[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

Merged
merged 4 commits into from
Apr 12, 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
48 changes: 48 additions & 0 deletions 3sum/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// 241.swift
// Algorithm
//
// Created by 안세훈 on 4/8/25.
//

//3Sum
class Solution { //정렬 + two pointer
func threeSum(_ nums: [Int]) -> [[Int]] {
let nums = nums.sorted() //배열을 오름차순으로 정렬
var result: [[Int]] = [] // 결과를 저장할 배열.

for i in 0..<nums.count { // i를 0번째 배열부터 순회.
if i > 0 && nums[i] == nums[i - 1] {
continue
}

var left = i + 1 //left는 i+1번째 인덱스
var right = nums.count - 1 //right는 배열의 끝번째 인덱스

while left < right {
let sum = nums[i] + nums[left] + nums[right]

if sum == 0 {
result.append([nums[i], nums[left], nums[right]])

// 중복 제거
while left < right && nums[left] == nums[left + 1] {
left += 1
}
while left < right && nums[right] == nums[right - 1] {
right -= 1
}

left += 1
right -= 1
} else if sum < 0 {
left += 1
} else {
right -= 1
}
}
}

return result
}
}
134 changes: 134 additions & 0 deletions climbing-stairs/HISEHOONAN.swift
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보다 작을때 까지는 걍 그 숫자 리턴
Copy link
Contributor

Choose a reason for hiding this comment

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

전과 전전만 있으면 될것같아서 n < 3 조건으로 해도 가능할것같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

앗 그렇네요 ! 감사합니다 !


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


? 피보나치자나?
*/
46 changes: 46 additions & 0 deletions product-of-array-except-self/HISEHOONAN.swift
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
}
}
16 changes: 16 additions & 0 deletions valid-anagram/HISEHOONAN.swift
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
}
}
40 changes: 40 additions & 0 deletions validate-binary-search-tree/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// 251.swift
// Algorithm
//
// Created by 안세훈 on 4/8/25.
//

//Validate Binary Search Tree

/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/

class Solution {
func isValidBST(_ root: TreeNode?) -> Bool {
return validate(root, min: nil, max: nil)
}

private func validate(_ node: TreeNode?, min: Int?, max: Int?) -> Bool {
guard let node = node else { return true }

if let min = min, node.val <= min { return false }
if let max = max, node.val >= max { return false }

return validate(node.left, min: min, max: node.val) &&
validate(node.right, min: node.val, max: max)
}
}