From 283557402001d81b83a77db59f5527b78bc5fff8 Mon Sep 17 00:00:00 2001 From: Can Huynh <58443871+canhuynh1998@users.noreply.github.com> Date: Wed, 1 Sep 2021 05:51:50 -0700 Subject: [PATCH] chore: add C++ permutation and add Go contains duplicate (#441) Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com> --- algorithms/C/README.md | 1 + algorithms/CPlusPlus/README.md | 1 + .../CPlusPlus/Recursion/permutation.cpp | 48 +++++++++++++++++++ algorithms/Go/README.md | 6 ++- algorithms/Go/arrays/contains_duplicate.go | 33 +++++++++++++ algorithms/Go/arrays/majority_element.go | 33 +++++++++++++ algorithms/Go/recursion/fibonacci.go | 43 +++++++++++++++++ .../Go/searching/rotated-array-search.go | 45 +++++++++++++++++ 8 files changed, 208 insertions(+), 2 deletions(-) create mode 100644 algorithms/CPlusPlus/Recursion/permutation.cpp create mode 100644 algorithms/Go/arrays/contains_duplicate.go create mode 100644 algorithms/Go/arrays/majority_element.go create mode 100644 algorithms/Go/recursion/fibonacci.go create mode 100644 algorithms/Go/searching/rotated-array-search.go diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 25601f5f..29958e52 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -9,6 +9,7 @@ - [Multiply](bit-manipulation/multiply-bitwise.c) - [Divide bitwise](bit-manipulation/divide-bitwise.c) + ## Graphs - [Prim's Algorithm](graphs/Prim's-algorithm.c) diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index f28fe6a7..6344f48d 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -114,3 +114,4 @@ 1. [Tower of Hanoi](Recursion/towerofHanoi.cpp) 2. [Factorial](Recursion/factorial.cpp) +3. [Permutation](Recursion/permutation.cpp) diff --git a/algorithms/CPlusPlus/Recursion/permutation.cpp b/algorithms/CPlusPlus/Recursion/permutation.cpp new file mode 100644 index 00000000..8d421438 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/permutation.cpp @@ -0,0 +1,48 @@ +/* +Given an array of distinct numbers, nums +find all the possible permutations. You can return the answer in any order. + +Example: + Input: nums = [1,2,3] + Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] +*/ + +#include +#include +using namespace std; + +vector> permute(vector &nums); +void helper(vector newNum, vector current, vector> &result); + +//Input will be similar to the example above, free feel to test with any kind of input +int main(){ + vector nums = {1, 2, 3}; + vector> permutations = permute(nums); + for(int i = 0 ; i < permutations.size(); i++){ + for(int j = 0; j < permutations[i].size();j++){ + cout<< permutations[i][j]<<" "; + } + cout<> permute(vector& nums) { + vector> result; + helper(nums, {}, result); + return result; +} + +void helper(vector newNum, vector current, vector> &result){ + if(newNum.size() == 0 && current.size() > 0){ + result.push_back(current); + }else{ + for(int i = 0; i < newNum.size(); i++){ + vector newArray; + newArray.insert(newArray.end(), newNum.begin(), newNum.begin() + i); + newArray.insert(newArray.end(), newNum.begin()+i+1, newNum.end()); + vector newPerms = current; + newPerms.push_back(newNum[i]); + helper(newArray, newPerms, result); + } + } +} \ No newline at end of file diff --git a/algorithms/Go/README.md b/algorithms/Go/README.md index b54aefa6..62968bd4 100644 --- a/algorithms/Go/README.md +++ b/algorithms/Go/README.md @@ -3,6 +3,8 @@ ## Arrays 1. [Maximum subarray sum (Kadane's Algorithm)](arrays/maximum-subarray-sum.go) 2. [Two Sum](arrays/two-sum.go) +3. [Majority Element](arrays/majority_element.go) +4. [Contains Duplicate](arrays/contains_duplicate.go) ## Scheduling 1. [Interval Scheduling](scheduling/interval-scheduling.go) @@ -10,6 +12,7 @@ ## Searching 1. [Binary Search](searching/binary-search.go) 2. [Linear Search](searching/linear-search.go) +3. [Find Minimum in Rotated Sorted Array](searching/rotated-array-search.go) ## Sorting 1. [Bubble Sort](sorting/bubble-sort.go) @@ -17,5 +20,4 @@ 3. [Quicksort](sorting/quicksort.go) ## Recursion -1. [Fibonacci](recursion/fibonacci.go) - +1. [Fibonacci](recursion/fibonacci.go) \ No newline at end of file diff --git a/algorithms/Go/arrays/contains_duplicate.go b/algorithms/Go/arrays/contains_duplicate.go new file mode 100644 index 00000000..04497dd1 --- /dev/null +++ b/algorithms/Go/arrays/contains_duplicate.go @@ -0,0 +1,33 @@ +package arrays +import("fmt") +/* +Given an integer array nums, +return true if any value appears at least twice in the array, and return false if every element is distinct. + +Example: + Input: nums = [1, 2, 3, 1] + Output: true + +Time: O(n) +Space: O(n) + +*/ + +func containsDuplicate(nums []int) bool { + dictionary := make(map[int]int) + + for _, num := range(nums){ + if _, found := dictionary[num]; found{ + return true; + }else{ + dictionary[num] = 1 + } + } + return false; +} + +func runContainDuplicate(){ + nums := []int{1,2,3,1} + hasDuplicate := containsDuplicate(nums) + fmt.Printf("Contain Duplicate %t\n", hasDuplicate) +} \ No newline at end of file diff --git a/algorithms/Go/arrays/majority_element.go b/algorithms/Go/arrays/majority_element.go new file mode 100644 index 00000000..6eb07ae2 --- /dev/null +++ b/algorithms/Go/arrays/majority_element.go @@ -0,0 +1,33 @@ +package arrays +import ("fmt") +/* +Given an array nums of size n, return the majority element. +The majority element appears more than n/2 times + +Time: O(n) +Space: O(1) +*/ + +func majorityElement(nums []int) int { + count := 1 + candidate := nums[0] + + for i:= 1; i < len(nums); i++{ + num := nums[i] + if count == 0{ + candidate = nums[i] + count = 1 + }else if num == candidate{ + count++ + }else if num != candidate{ + count-- + } + } + return candidate +} + +func runmajorityElement(){ + nums1 := []int{2,2,1,1,1,2,2} + target := majorityElement(nums1) + fmt.Printf("Majority Element is %d\n", target) +} \ No newline at end of file diff --git a/algorithms/Go/recursion/fibonacci.go b/algorithms/Go/recursion/fibonacci.go new file mode 100644 index 00000000..f3dd7f67 --- /dev/null +++ b/algorithms/Go/recursion/fibonacci.go @@ -0,0 +1,43 @@ +package recursion +import "fmt" +/* +Time: O(2^n) +Space: O(n) - call stack +*/ +func fibonacci_using_recursion(n int) int { + if n == 2{ + return 1 + } + if n == 1{ + return 0; + } + return fibonacci_using_recursion(n-1) + fibonacci_using_recursion(n-2) +} + +/* +Time: O(N) +Space: O(1) +*/ +func fibonacci_using_constant_space(n int) int{ + first:= 0 + second:= 1 + + for i := 3; i<= n; i++{ + next:= first + second + first = second + second = next + } + + if n > 1{ + return second + } + return first // this handles n == 1 or n == 0 case +} + +func run_fibonacci(){ + n := 10 + recursionValue := fibonacci_using_recursion(n) + nonrecursionValue := fibonacci_using_constant_space(n) + fmt.Printf("Recursion value: %d\n", recursionValue) + fmt.Printf("Non-recursion value: %d\n", nonrecursionValue) +} \ No newline at end of file diff --git a/algorithms/Go/searching/rotated-array-search.go b/algorithms/Go/searching/rotated-array-search.go new file mode 100644 index 00000000..6694a398 --- /dev/null +++ b/algorithms/Go/searching/rotated-array-search.go @@ -0,0 +1,45 @@ +package searching + +import "fmt" + +/* + +Find the minimum element in the rotated sorted array(ascending order). +An array [a[0], a[1], a[2], ..., a[n-1]] after 1 rotation will be [a[n-1], a[0], a[1], a[2], ..., a[n-2]] + +All elements are unique + +*/ +func findMin(nums []int) int { + if len(nums) == 1 || nums[0] < nums[len(nums)-1]{ + return nums[0] + } + + front, back := 0, len(nums) -1 + for front <= back { + mid := (front+back) / 2 + if nums[mid] > nums[mid+1]{ + return nums[mid+1] + }else if nums[mid] < nums[mid-1]{ + return nums[mid] + } + + if nums[mid] > nums[0]{ + front = mid + 1 + }else if nums[mid] < nums[0]{ + back = mid - 1 + } + } + return 0 +} + +//I will hard-code the input for now but feel free to modify +func runFindMin(){ + nums1 := []int{4,5,6,7,8,0,1,2} + result1 := findMin(nums1) + fmt.Println(result1) // should print 0 + + nums2 := []int{1,2,3,4,5,6,7,8} + result2 := findMin(nums2) + fmt.Println(result2) // should print 1 +} \ No newline at end of file