if let leader = classmate.firstIndex(of: classmate.max()!) {
print(leader+1)
}
//[Set([1, 2]), Set([0, 2]), Set([1, 4, 3, 0]), Set([2]), Set([2])]
let classmateCount = classmate.map { $0.count }
if let leader = classmateCount.firstIndex(of: classmateCount.max()!) {
print(leader+1)
}
먼저 map 이용해서 배열로 바꾸기!
그러면 배열에서 가장 큰 수의 인덱스 찾는거랑 똑같음!!
....X
..XX.
.....
.XX..
X....
for i in 0..<n {
let splitRow = room[i].split(separator: "X").map { $0.count }
let splitCol = room.map({ $0[i] }).split(separator: "X").map { $0.count }
}
row기준으로 나누기, col기준으로 나누기 이렇게 하면 됨
var frame = [(Int, Int, Int)]() // (학생 번호, 추천 횟수, 게시된 시간)
for r in recommend {
if let index = frame.firstIndex(where: { $0.0 == r }) {
frame[index].1 += 1
}
firstIndex, where
let minRecommend = frame.min(by: { $0.1 < $1.1 })!.1
min, by
print(frame.map { $0.0 }.sorted().map { String($0) }.joined(separator: " "))
// [(Int, Int, Int)]() 여기서 첫번째 값 기준으로 sort -> map -> joined
만약에 어떤 값을 입력 받았을 때 그 값이 중복이지 않아도 될 때
readLine()!.split(separator: " ").map { Int($0)! }
Set(readLine()!.split(separator: " ").map { Int($0)! })
처음걸로 contains하면 O(n), Set으로 변환하면 O(1)로 줄어든다.
for i in stride(from: n-1, through: 0, by: -1)
through ~까지
let result = cloudInfo.flatMap { $0 }.filter { $0 == k }.count
cloudInfo라는 2차원 배열을 flatMap해서 하나로 만들어준 다음
print(map.flatMap { $0 }.reduce(0, +))
var numbers = [1,2,3,4,5,6]
numbers.removeSubrange(1..<4) // 인덱스 1,2,3 범위의 값을 제거한다.
print(numbers) // [1,5,6]
print(index+1, terminator: " ")