From d96abb6ca655d2c53e47ea087b08219818921cbe Mon Sep 17 00:00:00 2001 From: David Wright Date: Wed, 14 Oct 2020 23:59:04 -0400 Subject: [PATCH 1/3] Added a playground for solving coding interview practice problems --- .../CSPracticeProblems.playground/Contents.swift | 1 + .../CSPracticeProblems.playground/contents.xcplayground | 4 ++++ .../playground.xcworkspace/contents.xcworkspacedata | 7 +++++++ 3 files changed, 12 insertions(+) create mode 100644 Practice-Problems/CSPracticeProblems.playground/Contents.swift create mode 100644 Practice-Problems/CSPracticeProblems.playground/contents.xcplayground create mode 100644 Practice-Problems/CSPracticeProblems.playground/playground.xcworkspace/contents.xcworkspacedata diff --git a/Practice-Problems/CSPracticeProblems.playground/Contents.swift b/Practice-Problems/CSPracticeProblems.playground/Contents.swift new file mode 100644 index 00000000..ba8bb2dd --- /dev/null +++ b/Practice-Problems/CSPracticeProblems.playground/Contents.swift @@ -0,0 +1 @@ +// LeetCode Practice Problems: Algorithms diff --git a/Practice-Problems/CSPracticeProblems.playground/contents.xcplayground b/Practice-Problems/CSPracticeProblems.playground/contents.xcplayground new file mode 100644 index 00000000..a751024c --- /dev/null +++ b/Practice-Problems/CSPracticeProblems.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Practice-Problems/CSPracticeProblems.playground/playground.xcworkspace/contents.xcworkspacedata b/Practice-Problems/CSPracticeProblems.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..919434a6 --- /dev/null +++ b/Practice-Problems/CSPracticeProblems.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + From 47bbddad77405ac816525563739b3527c4ec98af Mon Sep 17 00:00:00 2001 From: David Wright Date: Thu, 15 Oct 2020 00:00:36 -0400 Subject: [PATCH 2/3] Added a binary search problem --- .../Contents.swift | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Practice-Problems/CSPracticeProblems.playground/Contents.swift b/Practice-Problems/CSPracticeProblems.playground/Contents.swift index ba8bb2dd..1819d953 100644 --- a/Practice-Problems/CSPracticeProblems.playground/Contents.swift +++ b/Practice-Problems/CSPracticeProblems.playground/Contents.swift @@ -1 +1,45 @@ // LeetCode Practice Problems: Algorithms + +//--------------------------------------------------------------------------------------------- + +// MARK: - Binary Search Problems +// Key Words: sorted, range +// Notes: +// - Binary search should be muscle memory +// - Can be implemented iteratively or recursively + +//--------------------------------------------------------------------------------------------- + +// Sqrt(x) +// Compute and return the square root of x, where x is guaranteed to be a non-negative integer. +// Since the return type is an integer, the decimal digits are truncated and only the integer +// part of the result is returned. + +func squareRoot(_ x: Int) -> Int { + var min = 0 + var max = x + var res = 0 + + while min <= max { + let mid = Int((min + max) / 2) + let squared = mid * mid + if squared == x { + return mid + } else if squared > x { + max = mid - 1 + } else { + min = mid + 1 + res = mid + } + } + + return res +} + +print(squareRoot(16)) // 4 +print(squareRoot(9)) // 3 +print(squareRoot(8)) // 2 +print(squareRoot(1)) // 1 +print(squareRoot(0)) // 0 + +//--------------------------------------------------------------------------------------------- From 929d41fb8c997934d8e4f9d9aa91d2c5020aeb5b Mon Sep 17 00:00:00 2001 From: David Wright Date: Thu, 15 Oct 2020 00:01:15 -0400 Subject: [PATCH 3/3] Added a hash table problem --- .../Contents.swift | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Practice-Problems/CSPracticeProblems.playground/Contents.swift b/Practice-Problems/CSPracticeProblems.playground/Contents.swift index 1819d953..bd7f7037 100644 --- a/Practice-Problems/CSPracticeProblems.playground/Contents.swift +++ b/Practice-Problems/CSPracticeProblems.playground/Contents.swift @@ -43,3 +43,31 @@ print(squareRoot(1)) // 1 print(squareRoot(0)) // 0 //--------------------------------------------------------------------------------------------- + +// MARK: - Hash Table Problems +// Key Words: two arrays, intersection, count + +//--------------------------------------------------------------------------------------------- + +// Intersection of Two Arrays II +// Given two arrays, write a function to compute their intersection. + +func intersect(_ nums1: [Int], _ nums2: [Int]) -> [Int] { + var res: [Int] = [] + var dict = [Int: Int]() + + for n1 in nums1 { + dict[n1, default: 0] += 1 + } + + for n2 in nums2 { + if dict[n2, default: 0] > 0 { + res.append(n2) + dict[n2]! -= 1 + } + } + + return res +} + +//---------------------------------------------------------------------------------------------