Skip to content

[Algorithm] Triangleย #144

@hwangJi-dev

Description

@hwangJi-dev

๐Ÿ’ฌย ๋ฌธ์ œ

https://app.codility.com/programmers/lessons/6-sorting/triangle/


๐Ÿ’ฌย Idea

  1. ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ ๋’ค ์Œ์ˆ˜๊ฐ’์„ ํ•„ํ„ฐ๋งํ•ด์ค€๋‹ค.
  2. ์ด ๋•Œ ๋ฐฐ์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ(์‚ผ๊ฐํ˜• ์ถฉ์กฑ ์กฐ๊ฑด) ๋ฏธ๋งŒ์ด๋ผ๋ฉด 0์„ ๋ฆฌํ„ดํ•œ๋‹ค.
  3. ๋ฐฐ์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ์‚ผ๊ฐํ˜• ๊ฐœ์ˆ˜๋ฅผ ์ถฉ์กฑํ•œ๋‹ค๋ฉด, ๋ฐฐ์—ด์„ ๋Œ๋ฉด์„œ ์ธ์ ‘ํ•œ ์ž‘์€ ๋‘์ˆ˜์˜ ํ•ฉ๋ณด๋‹ค ๊ทธ ๋‹ค์Œ ์ˆ˜์˜ ๊ฐ’์ด ํฌ๋‹ค๋ฉด 1์„, ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๊ฐ€ ์—†๋‹ค๋ฉด 0์„ ๋ฆฌํ„ดํ•œ๋‹ค.

๐Ÿ’ฌย ํ’€์ด

import Foundation
import Glibc

// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")

public func solution(_ A : inout [Int]) -> Int {
    A = A.sorted()
    A = A.filter({ $0 > 0 })
    
    if A.count < 3 { return 0 }
    
    for i in 2..<A.count {
        if A[i - 2] + A[i - 1] > A[i] {
            return 1
        }
    }
    
    return 0
}

์†Œ์š”์‹œ๊ฐ„ : 15๋ถ„

์‹œ๊ฐ„ ๋ณต์žก๋„ : O(Nlog(N))*

ํ‰๊ฐ€ํ‘œ : https://app.codility.com/demo/results/training5KSK4X-9HQ/

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions