chore: add C++ permutation and add Go contains duplicate (#441)
Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>pull/445/head
parent
2c8c9c011c
commit
2835574020
|
@ -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)
|
||||
|
||||
|
|
|
@ -114,3 +114,4 @@
|
|||
|
||||
1. [Tower of Hanoi](Recursion/towerofHanoi.cpp)
|
||||
2. [Factorial](Recursion/factorial.cpp)
|
||||
3. [Permutation](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 <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
vector<vector<int>> permute(vector<int> &nums);
|
||||
void helper(vector<int> newNum, vector<int> current, vector<vector<int>> &result);
|
||||
|
||||
//Input will be similar to the example above, free feel to test with any kind of input
|
||||
int main(){
|
||||
vector<int> nums = {1, 2, 3};
|
||||
vector<vector<int>> 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<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
vector<vector<int>> permute(vector<int>& nums) {
|
||||
vector<vector<int>> result;
|
||||
helper(nums, {}, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
void helper(vector<int> newNum, vector<int> current, vector<vector<int>> &result){
|
||||
if(newNum.size() == 0 && current.size() > 0){
|
||||
result.push_back(current);
|
||||
}else{
|
||||
for(int i = 0; i < newNum.size(); i++){
|
||||
vector<int> newArray;
|
||||
newArray.insert(newArray.end(), newNum.begin(), newNum.begin() + i);
|
||||
newArray.insert(newArray.end(), newNum.begin()+i+1, newNum.end());
|
||||
vector<int> newPerms = current;
|
||||
newPerms.push_back(newNum[i]);
|
||||
helper(newArray, newPerms, result);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
|
@ -18,4 +21,3 @@
|
|||
|
||||
## Recursion
|
||||
1. [Fibonacci](recursion/fibonacci.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)
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue