Skip to content

Commit c30495c

Browse files
committed
LeetCode: 997 solved
1 parent 8d5b21b commit c30495c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

CodingChallenges.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
1F2C534125F001C80082BC76 /* 1486. XOR Operation in an Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F2C534025F001C80082BC76 /* 1486. XOR Operation in an Array.swift */; };
11+
1FFBFF1825F1246B000FEEE4 /* 997. Find the Town Judge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FFBFF1725F1246B000FEEE4 /* 997. Find the Town Judge.swift */; };
1112
9102B4202080D673004ABC77 /* AddTwoNumbers_2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9102B41F2080D673004ABC77 /* AddTwoNumbers_2.swift */; };
1213
9102B4222080F196004ABC77 /* PalindromeNumber_9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9102B4212080F196004ABC77 /* PalindromeNumber_9.swift */; };
1314
91108B49217D949A003DA3BB /* RouteBetweenNodes_4_1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91108B48217D949A003DA3BB /* RouteBetweenNodes_4_1.swift */; };
@@ -113,6 +114,7 @@
113114

114115
/* Begin PBXFileReference section */
115116
1F2C534025F001C80082BC76 /* 1486. XOR Operation in an Array.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "1486. XOR Operation in an Array.swift"; sourceTree = "<group>"; };
117+
1FFBFF1725F1246B000FEEE4 /* 997. Find the Town Judge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "997. Find the Town Judge.swift"; sourceTree = "<group>"; };
116118
9102B41F2080D673004ABC77 /* AddTwoNumbers_2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AddTwoNumbers_2.swift; sourceTree = "<group>"; };
117119
9102B4212080F196004ABC77 /* PalindromeNumber_9.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PalindromeNumber_9.swift; sourceTree = "<group>"; };
118120
91108B48217D949A003DA3BB /* RouteBetweenNodes_4_1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RouteBetweenNodes_4_1.swift; sourceTree = "<group>"; };
@@ -420,6 +422,7 @@
420422
B5681F8722CF860800963355 /* LE_BackspaceStringCompare_844.swift */,
421423
B585E9B122D74C39009DE9CB /* LE_SearchInRotatedSortedArray.swift */,
422424
1F2C534025F001C80082BC76 /* 1486. XOR Operation in an Array.swift */,
425+
1FFBFF1725F1246B000FEEE4 /* 997. Find the Town Judge.swift */,
423426
);
424427
path = Easy;
425428
sourceTree = "<group>";
@@ -785,6 +788,7 @@
785788
913BD9F6214C0B5600520710 /* DeleteMiddleNode_2_3.swift in Sources */,
786789
B557C67021DD79910082D1A3 /* CourseraInterviewTests.swift in Sources */,
787790
9139FA6E2088CA9200229150 /* String+Ext.swift in Sources */,
791+
1FFBFF1825F1246B000FEEE4 /* 997. Find the Town Judge.swift in Sources */,
788792
91AE066F2143DE6D0046A910 /* OneAway_1_5.swift in Sources */,
789793
918D50992122261B00DEFB61 /* RingBuffer.swift in Sources */,
790794
911735B8215AD45A00A262E3 /* GoldmanSachsInterviewTest.swift in Sources */,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// 997. Find the Town Judge.swift
3+
// CodingChallenges
4+
//
5+
// Created on 3/4/21.
6+
//
7+
8+
/// https://leetcode.com/problems/find-the-town-judge/
9+
10+
import Foundation
11+
12+
class LE_FindTheTownJudge {
13+
func findJudge(_ n: Int, _ trust: [[Int]]) -> Int {
14+
// return solution1(n, trust)
15+
return solution2(n, trust)
16+
}
17+
18+
19+
func solution2(_ n: Int, _ trust: [[Int]]) -> Int {
20+
var candidates = Array(repeating: 0, count: n)
21+
22+
for pair in trust {
23+
let left = pair[0] - 1
24+
let right = pair[1] - 1
25+
26+
if candidates[right] != -1 {
27+
candidates[right] += 1
28+
}
29+
candidates[left] = -1
30+
}
31+
32+
guard let index = candidates.firstIndex(of: n - 1) else {
33+
return -1
34+
}
35+
return index + 1
36+
}
37+
38+
func solution1(_ n: Int, _ trust: [[Int]]) -> Int {
39+
var candidates = [Int: Int]()
40+
41+
for item in (1...n) {
42+
candidates[item] = 0
43+
}
44+
45+
for pair in trust {
46+
candidates[pair[0], default: 0] -= 1
47+
candidates[pair[1], default: 0] += 1
48+
}
49+
50+
return candidates.first { $0.value == n - 1 }?.key ?? -1
51+
}
52+
}

0 commit comments

Comments
 (0)