-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.cpp
More file actions
196 lines (174 loc) · 4.09 KB
/
Copy pathRecursion.cpp
File metadata and controls
196 lines (174 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* LECTRUE04.cpp
*
* Created on: Aug 31, 2021
* Author: choiwonhong
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std::string_literals;
// given : n>=1
// Time Complexity : O(n)
// Space Complexity : O(n)
void printNto1(int n){
if(n<1){
return;
}
std::cout << n << " ";
printNto1(n-1);
}
// Time Complexity : O(n)
// Space Complexity : O(n)
void print1ToN(int n){
if(n<1){
return;
}
print1ToN(n-1);
std::cout << n << " ";
}
// factorial
int fact(int n){
if(n==0)
return 1;
return n*fact(n-1);
}
// fibonacci
int fib(int n){
if(n<=1)
return n;
return fib(n-1)+fib(n-2);
}
// sum of natural number using recursion
// when given n, find the output which is 1+2+...+n
// 1-line solution
int getSum(int n){
return n*(n+1)/2;
}
// Time Complexity : O(n), because T(n)=T(n-1)+O(1)
// Space Complexity : O(n)
int getSum1(int n){
if(n==0)
return 0;
return n+getSum1(n-1);
}
// palindrome check using recursion
// Time Complexity : O(n), T(n)=T(n-2)+O(1)
// Space Complexity : O(n), exactly O(n/2)
bool isPalindrome(const std::string& str){
if(str.size()<=1){
return true;
}
return str[0]==str[str.size()-1] && isPalindrome(str.substr(1,str.size()-2));
}
// initially start=0, end=n-1
// Time Complexity : O(n)
// Space Complexity : O(n), exactly O(n/2)
bool isPalindrome(const std::string& str, int start, int end){
if(start>=end){
return true;
}
return str[start]==str[end] && isPalindrome(str,start+1,end-1);
}
// sum of digits using recursion
// Time Complexity : O(logn)
// Space Complexity : O(logn)
int sumOfDigits(int n){
if(n==0)
return 0;
return (n%10)+sumOfDigits(n/10);
}
// more optimized
int sumOfDigits1(int n){
if(n<=9)
return n;
return (n%10)+sumOfDigits1(n/10);
}
// iteratively
// Time Complexity : O(logn)
// Space Complexity : O(1)
int sumOfDigits2(int n){
int sum{0};
while(n>0){
sum+=n%10;
n/=10;
}
return sum;
}
// rope cutting problem
// Time Complexity : O(3^n)
int maxOfThree(int a, int b, int c){
if(a>=b&&a>=c)
return a;
else if(b>=a&&b>=c)
return b;
else
return c;
}
// initially edges=0
int ropeCutting(int n, int a, int b, int c, int edges){
if(n<0)
return -1;
if(n==0)
return edges;
return maxOfThree(ropeCutting(n-a,a,b,c,edges+1),ropeCutting(n-b,a,b,c,edges+1),ropeCutting(n-c,a,b,c,edges+1));
}
int ropeCutting1(int n, int a, int b, int c){
if(n==0)
return 0;
if(n<0)
return -1;
int result{maxOfThree(ropeCutting1(n-a,a,b,c),ropeCutting1(n-b,a,b,c),ropeCutting1(n-c,a,b,c))};
if(result==-1)
return -1;
return result+1;
}
// subset of a string
void subset(const std::string& str, std::string cur=""s,int i=0){
if(i==str.size()){
std::cout << cur << " ";
return;
}
subset(str,cur,i+1);
subset(str,cur+str[i],i+1);
}
void towerOfHanoi(int n, char from='A',char tmp='B',char to='C'){
if(n==0)
return;
towerOfHanoi(n-1,from,to,tmp);
std::cout << "Move disc " << n << " from " << from << " to " << to << std::endl;
towerOfHanoi(n-1,tmp,from,to);
}
// I don't understand it a lttle bit
// josephus problem
// when there is a circular table and there are n peple, kill the k-th person
// then, from the died person, next k-th person is killed
// Time Complexity : O(n), T(n)=T(n-1)+O(1)
int josephus(int n, int k){
if(n==1)
return 0;
return (josephus(n-1,k)+k)%n;
}
// subset sum
// return the number of subset which the sum of subset is equal to given sum
// Time complexity : O(2^n)
int subsetSum(const std::vector<int>& vec, int size, int sum){
if(size==0)
return (sum==0)?1:0;
return subsetSum(vec,size-1,sum)+subsetSum(vec,size-1,sum-vec[size-1]);
}
// should do
void printAllPermutation(const std::string& str){
}
int main(){
// printNto1(5);
// std::cout << std::endl;
// print1ToN(5);
// std::cout << getSum1(4) << std::endl;
// std::cout << std::boolalpha << isPalindrome("ac"s);
// std::cout << sumOfDigits(873);
// std::cout << ropeCutting(9,2,2,2,0);
// subset("ABC");
towerOfHanoi(3);
return 0;
}