Skip to content

Instantly share code, notes, and snippets.

@fishkingsin
Last active November 10, 2021 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fishkingsin/e6fdc38cc40738444e29e408d31e2920 to your computer and use it in GitHub Desktop.
Save fishkingsin/e6fdc38cc40738444e29e408d31e2920 to your computer and use it in GitHub Desktop.
sample
import 'dart:collection';
void main() {
// Q1
bool ret = fx(2, 20, 200, 301);
print(" ${ ret ? "meet" : "not meet" }");
ret = fx(2, 20, 4, 400);
print(" ${ ret ? "meet" : "not meet" }");
print("O(?) step $step");
// Q2
fx2([]);
fx2([1, 2, 3, 4, 5]);
fx2([6, 7, 8, 9, 3, 4, 10, 11, 12]);
fx2([]);
print("Q2 O(?) step $step");
}
/*
consider input 2 integer a and b and the goal c and d
implement a function to examinate will (a+b , b) or (a, a+b) will meet c and d
e.g
a = 1 , b = 4, c = 9, d =4
(1 + 4, 4) = (5, 4)
(4 + 5, 4) = (9, 4)
return true
*/
int step = 0;
bool fx(int a, int b, int c, int d) {
step++;
if (a == c && b == d) { // step met
return true;
}
if (a > c || b > d) {
return false;
}
return fx(a + b, b, c, d) || fx(a, a + b, c, d);
}
/*
consider input array of integer in following sequence
t1 = []
t2 = [1,2,3,4,5]
t3 = [6,7,8,9,10,3,4,11,12]
t4 = []
outputs:
t2 add [1,2,3,4,5] remove []
t2 add [6,7,8,9,10,11,12] remove [1,2,5]
t2 add [] remove [6,7,8,9,10,11,12]
write a function to output the above result
*/
Set<int> cacheSet = {};
void fx2(List<int> input) {
Set<int> inputSet = HashSet.from(input);
Set<int> removeSet = {};
Set<int> addSet = {};
removeSet = cacheSet.difference(inputSet);
print("remove $removeSet");
addSet = inputSet.difference(cacheSet);
print("add $addSet");
cacheSet = inputSet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment