Skip to content

Commit

Permalink
Add CountTriangles
Browse files Browse the repository at this point in the history
  • Loading branch information
omalovichko committed Jun 22, 2019
1 parent 82da04a commit 1a54078
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CodilityLessons.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
FA9037CE1EA8CF13005A071F /* Lesson90_LongestPassword.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA9037CD1EA8CF13005A071F /* Lesson90_LongestPassword.swift */; };
FA9037D01EA8E302005A071F /* Lesson90_FloodDepth.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA9037CF1EA8E302005A071F /* Lesson90_FloodDepth.swift */; };
FA9037D21EA920D8005A071F /* Lesson92_SocksLaundering.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA9037D11EA920D8005A071F /* Lesson92_SocksLaundering.swift */; };
FA96D8EE22BE91AA000BD7E9 /* Lesson15_CountTriangles.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA96D8ED22BE91AA000BD7E9 /* Lesson15_CountTriangles.swift */; };
FABB1B421E9FFBD600B2E6A5 /* Lesson9_MaxSliceSum.swift in Sources */ = {isa = PBXBuildFile; fileRef = FABB1B411E9FFBD600B2E6A5 /* Lesson9_MaxSliceSum.swift */; };
FACD0A291EF7E2B600B4BDEA /* Lesson10_Peaks.swift in Sources */ = {isa = PBXBuildFile; fileRef = FACD0A281EF7E2B600B4BDEA /* Lesson10_Peaks.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -136,6 +137,7 @@
FA9037CF1EA8E302005A071F /* Lesson90_FloodDepth.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lesson90_FloodDepth.swift; sourceTree = "<group>"; };
FA9037D11EA920D8005A071F /* Lesson92_SocksLaundering.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lesson92_SocksLaundering.swift; sourceTree = "<group>"; };
FA9620F41FBEF20000A4B668 /* .travis.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .travis.yml; sourceTree = "<group>"; };
FA96D8ED22BE91AA000BD7E9 /* Lesson15_CountTriangles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Lesson15_CountTriangles.swift; sourceTree = "<group>"; };
FABB1B411E9FFBD600B2E6A5 /* Lesson9_MaxSliceSum.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lesson9_MaxSliceSum.swift; sourceTree = "<group>"; };
FAC133E01EF15A8100BCEBB9 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
FACD0A281EF7E2B600B4BDEA /* Lesson10_Peaks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Lesson10_Peaks.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -363,6 +365,7 @@
isa = PBXGroup;
children = (
FA8EF8891EAF872F00B0523E /* Lesson15_CountDistinctSlices.swift */,
FA96D8ED22BE91AA000BD7E9 /* Lesson15_CountTriangles.swift */,
FA8EF8871EAF7F9900B0523E /* Lesson15_AbsDistinct.swift */,
);
name = "Lesson 15 Caterpillar method";
Expand Down Expand Up @@ -560,6 +563,7 @@
FA1F2A2C1E953C66002437F3 /* Lesson3_FrogJmp.swift in Sources */,
FA8EF8881EAF7F9900B0523E /* Lesson15_AbsDistinct.swift in Sources */,
FA9037CE1EA8CF13005A071F /* Lesson90_LongestPassword.swift in Sources */,
FA96D8EE22BE91AA000BD7E9 /* Lesson15_CountTriangles.swift in Sources */,
FA12631D2234627900F07C20 /* Lesson12_CommonPrimeDivisors.swift in Sources */,
FA9037CA1EA8C5A5005A071F /* Lesson16_MaxNonoverlappingSegments.swift in Sources */,
FA7C20921EA77E290048284F /* Lesson99_PolygonConcavityIndex.swift in Sources */,
Expand Down
85 changes: 85 additions & 0 deletions CodilityLessonsTests/Lesson15_CountTriangles.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// Lesson15_CountTriangles.swift
// CodilityLessonsTests
//
// Created by Oleksandr Malovichko on 22.06.2019.
//

import XCTest

/*
CountTriangles
Count the number of triangles that can be built from a given set of edges.


An array A consisting of N integers is given. A triplet (P, Q, R) is triangular if it is possible to build a triangle with sides of lengths A[P], A[Q] and A[R]. In other words, triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and:

A[P] + A[Q] > A[R],
A[Q] + A[R] > A[P],
A[R] + A[P] > A[Q].
For example, consider array A such that:

A[0] = 10 A[1] = 2 A[2] = 5
A[3] = 1 A[4] = 8 A[5] = 12
There are four triangular triplets that can be constructed from elements of this array, namely (0, 2, 4), (0, 2, 5), (0, 4, 5), and (2, 4, 5).

Write a function:

public func solution(_ A : inout [Int]) -> Int
that, given an array A consisting of N integers, returns the number of triangular triplets in this array.

For example, given array A such that:

A[0] = 10 A[1] = 2 A[2] = 5
A[3] = 1 A[4] = 8 A[5] = 12
the function should return 4, as explained above.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [0..1,000];
each element of array A is an integer within the range [1..1,000,000,000].
*/

class Lesson15_CountTriangles: XCTestCase {

func test() {
var a = [Int]()

a = [10, 2, 5, 1, 8, 12]
XCTAssertEqual(solution(&a), 4)
}

func testPerformance() {
var a = [Int]()
for i in 0..<1000 {
a.append(i)
}
measure {
_ = solution(&a)
}
}

public func solution(_ A : inout [Int]) -> Int {
guard A.count > 2 else {
return 0
}
let sorted = A.sorted(by: { (l, r) -> Bool in
return l < r
})
var result = 0
for aIndex in 0..<sorted.count - 2 {
let a = sorted[aIndex]
var cIndex = aIndex + 2
for bIndex in aIndex + 1..<sorted.count - 1 {
let b = sorted[bIndex]
let ab = a + b
while cIndex < sorted.count && ab > sorted[cIndex] {
cIndex += 1
}
result += cIndex - bIndex - 1
}
}
return result
}

}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ __Lesson 14 - Binary search algorithm__

__Lesson 15 - Caterpillar method__
* CountDistinctSlices: Count the number of distinct slices (containing only unique numbers).
* ~~CountTriangles: Count the number of triangles that can be built from a given set of edges.~~
* CountTriangles: Count the number of triangles that can be built from a given set of edges.
* AbsDistinct: Compute number of distinct absolute values of sorted array elements.
* ~~MinAbsSumOfTwo: Find the minimal absolute value of a sum of two elements.~~

Expand Down

0 comments on commit 1a54078

Please sign in to comment.