Skip to content

[HISEHOONAN] WEEK 01 solutions #1147

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 3 commits into from
Apr 3, 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
17 changes: 17 additions & 0 deletions contains-duplicate/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Contains_Duplicate.swift
// Algorithm
//
// Created by 안세훈 on 3/31/25.
//

import Foundation

class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
return nums.count != Set(nums).count
//Set : 중복된 값을 갖지 않음.
//문제로 주어진 배열의 개수와 중복을 갖지않는 Set연산의 개수의 차이 비교
Copy link
Contributor

Choose a reason for hiding this comment

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

문제에 주석으로 해설 달아놓으신거 맛도리네요~ 나이쓰합니다~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

기억하기 편한거 같아요~!

//비교 후 다르다면 true 같다면 false
}
}
27 changes: 27 additions & 0 deletions house-robber/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// House_Robber .swift
// Algorithm
//
// Created by 안세훈 on 3/31/25.
//
///https://leetcode.com/problems/house-robber/description/

class Solution {
//Dynamic Programming
func rob(_ nums: [Int]) -> Int {

if nums.count == 1 {return nums[0]} //배열이 1개인 경우
if nums.count == 2 {return max(nums[0],nums[1])} //배열이 2개인 경우

var dp = [nums[0], max(nums[0],nums[1])]
//제일 base가 되는 두 값을 찾는게 제일 중요함.
//nums[0]은 nums[1]보다 무조건 작아야함.
for i in 2..<nums.count{
dp.append(max(dp[i-2]+nums[i],dp[i-1]))
//dp배열의 i-2번째 배열 + nums의 i번째 배열 vs dp의 i-1번째 배열 중 큰걸 append
//
}

return dp[dp.count-1]
}
}
31 changes: 31 additions & 0 deletions longest-consecutive-sequence/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Longest_Consecutive_Sequence.swift
// Algorithm
//
// Created by 안세훈 on 3/31/25.
//

///https://leetcode.com/problems/longest-consecutive-sequence/


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

var sortednums = Array(Set(nums)).sorted() //중복제거 + 오름차순 정렬
var maxcount = 1 //최대 카운드
var count = 1 // 연산용 카운트

for i in 0..<sortednums.count - 1 {
if sortednums[i] == sortednums[i+1]-1 {
//i번째 정렬된 리스트 와 i+1번째 리스트의 값 - 1이 같을 경우
count += 1 //카운트에 1+
maxcount = max(maxcount, count) //maxcount는 연산과, max중 큰 것
} else {
count = 1 //아니면 1로 초기화.
}
}

return maxcount
}
}
26 changes: 26 additions & 0 deletions top-k-frequent-elements/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Top_K_Frequent_Elements .swift
// Algorithm
//
// Created by 안세훈 on 3/31/25.
//

class Solution {
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
var dic : [Int : Int] = [:]

for i in nums {
dic[i] = dic[i , default : 0] + 1
}

var sortarray = dic.sorted{$0.value > $1.value}
var answer : [Int] = []

for i in 0..<k{
answer.append(sortarray[i].key)
}
print(sortarray)
print(answer)
return answer
}
}
20 changes: 20 additions & 0 deletions two-sum/HISEHOONAN.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Two_Sum.swift
// Algorithm
//
// Created by 안세훈 on 3/31/25.
//

class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
for i in 0...nums.count-1{
for j in i+1...nums.count-1{
if nums[i] + nums[j] == target{
return [i,j]
}
}
}
return []
}
//i번째 인덱스의 값과, j번째 인덱스의 값이 target과 같으면 i,j를 리턴
}