Skip to content

Commit 2cbedd3

Browse files
committed
0405
1 parent bea1ffe commit 2cbedd3

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func solution(_ strings:[String], _ n:Int) -> [String] {
2+
var strings = strings.sorted()
3+
var dic: [String : String] = [:]
4+
var answer: [String] = []
5+
6+
for (i, str) in strings.enumerated() {
7+
let value = "\(str[str.index(str.startIndex, offsetBy: n)])"
8+
dic.updateValue(str, forKey: "\(value)\(i)")
9+
}
10+
11+
12+
let keys = Array(dic.keys).sorted()
13+
14+
print(dic)
15+
print(keys)
16+
17+
18+
for key in keys {
19+
answer.append(dic[key]!)
20+
}
21+
22+
23+
return answer
24+
}

완전탐색/모의고사.swift

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Foundation
2+
3+
func solution(_ answers:[Int]) -> [Int] {
4+
var person1 = [1, 2, 3, 4, 5]
5+
var person2 = [2, 1, 2, 3, 2, 4, 2, 5]
6+
var person3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
7+
var score = [0, 0, 0]
8+
var answer: [Int] = []
9+
10+
for i in 0..<answers.count {
11+
if answers[i] == person1[i % 5] {
12+
score[0] += 1
13+
}
14+
if answers[i] == person2[i % 8] {
15+
score[1] += 1
16+
}
17+
if answers[i] == person3[i % 10] {
18+
score[2] += 1
19+
}
20+
}
21+
22+
let max = score.max()!
23+
24+
for i in 0..<score.count {
25+
if score[i] == max {
26+
answer.append(i + 1)
27+
}
28+
}
29+
30+
return answer
31+
}

정렬/K번째 수.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Foundation
2+
3+
func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
4+
var answer: [Int] = []
5+
6+
for cmd in commands {
7+
let range: ClosedRange = (cmd[0] - 1)...(cmd[1] - 1)
8+
let sortedArr = array[range].sorted()
9+
answer.append(sortedArr[cmd[2] - 1])
10+
}
11+
12+
return answer
13+
}

탐욕법(Greedy)/체육복.swift

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Foundation
2+
3+
func solution(_ n:Int, _ lost:[Int], _ reserve:[Int]) -> Int {
4+
var possible = Array(repeating: 1, count: n)
5+
var answer = 0
6+
7+
for i in lost { possible[i - 1] -= 1 }
8+
for i in reserve { possible[i - 1] += 1 }
9+
10+
for i in 0..<n {
11+
if i != 0 && possible[i - 1] == 2 && possible[i] == 0 {
12+
possible[i - 1] = 1
13+
possible[i] = 1
14+
} else if i != n - 1 && possible[i] == 0 && possible[i + 1] == 2 {
15+
possible[i] = 1
16+
possible[i + 1] = 1
17+
}
18+
}
19+
20+
for i in 0..<n {
21+
if possible[i] != 0 {
22+
answer += 1
23+
}
24+
}
25+
26+
return answer
27+
}

0 commit comments

Comments
 (0)