Ибраги́м
It’s some sort of array , that I know
std::vector is a sequence container that encapsulates dynamic size arrays.
Ибраги́м
Or do you have two pairs of eyes, one for each direcrion?
That's the difference between RTX ON & RTX OFF
MᏫᎻᎯᎷᎷᎬᎠ
.
Ankush
I want to learn C but idk how should i do that, i checked some youtube videos but that's not working i mean i want to learn as simpler as i can......ik idk how to express but pls think deeper😪
joel
type every code u see in the book
Ankush
I used that😐 i used to read ANSI C
S.
there are n possible begining points. For every begining point, it's a maxSumOFSubarray problem, hence O(n * n).
Had no time yesterday and have tried this morning ... Hmm you're right, the start point method is O(n^2) because finding the max subarray of circle {2, -1, 3} is not equivalent to that of consecutive array {2, -1, 3, 2, -1} 😂 It took me one and a half hour to figure out the trick ... definitely would fail in an interview 😑 #include <stdio.h> #include <limits.h> int findMaxSumOfCircle(const int *array, int len) { int currentMax = INT_MIN; int currentNeg = INT_MIN; int currentMaxSum = 0; int currentNegSum = 0; int allSum = 0; int i; for (i = 0; i < len; ++i) { allSum += array[i]; currentMaxSum += array[i]; currentNegSum -= array[i]; if (currentMax < currentMaxSum) { currentMax = currentMaxSum; } if (currentNeg < currentNegSum) { currentNeg = currentNegSum; } if (currentMaxSum < 0) { currentMaxSum = 0; } if (currentNegSum < 0) { currentNegSum = 0; } } // Cancel the "most negative" subarray to calculate // the sum of the remaining parts allSum += currentNeg; if (allSum == 0) { return currentMax; } else { return (currentMax > allSum)? currentMax : allSum; } } int main() { // int source[] = {1, 2, -1, 3, -1, 4}; // int source[] = {1, 2, 3}; int source[] = {-2, -1, -3}; int res = findMaxSumOfCircle(source, sizeof(source)/sizeof(source[0])); printf("Result: %d\n", res); return 0; }
Liam
Had no time yesterday and have tried this morning ... Hmm you're right, the start point method is O(n^2) because finding the max subarray of circle {2, -1, 3} is not equivalent to that of consecutive array {2, -1, 3, 2, -1} 😂 It took me one and a half hour to figure out the trick ... definitely would fail in an interview 😑 #include <stdio.h> #include <limits.h> int findMaxSumOfCircle(const int *array, int len) { int currentMax = INT_MIN; int currentNeg = INT_MIN; int currentMaxSum = 0; int currentNegSum = 0; int allSum = 0; int i; for (i = 0; i < len; ++i) { allSum += array[i]; currentMaxSum += array[i]; currentNegSum -= array[i]; if (currentMax < currentMaxSum) { currentMax = currentMaxSum; } if (currentNeg < currentNegSum) { currentNeg = currentNegSum; } if (currentMaxSum < 0) { currentMaxSum = 0; } if (currentNegSum < 0) { currentNegSum = 0; } } // Cancel the "most negative" subarray to calculate // the sum of the remaining parts allSum += currentNeg; if (allSum == 0) { return currentMax; } else { return (currentMax > allSum)? currentMax : allSum; } } int main() { // int source[] = {1, 2, -1, 3, -1, 4}; // int source[] = {1, 2, 3}; int source[] = {-2, -1, -3}; int res = findMaxSumOfCircle(source, sizeof(source)/sizeof(source[0])); printf("Result: %d\n", res); return 0; }
lol Actually, you could transfer exist knowledge into this new issue. * Known: how to find max sum of subarray in a non-circle array. New issue: how to find max sum of subarray in a circle array; which is equal to: * find the max of the following two sums * max sum of subarray in a non-circle array. * sum of the whole array minus min sum of subarray in a non-circle array.
Liam
lol Actually, you could transfer exist knowledge into this new issue. * Known: how to find max sum of subarray in a non-circle array. New issue: how to find max sum of subarray in a circle array; which is equal to: * find the max of the following two sums * max sum of subarray in a non-circle array. * sum of the whole array minus min sum of subarray in a non-circle array.
#include <iostream> #include <vector> #include <algorithm> #include <numeric> using citer_t = std::vector<int>::const_iterator; template <bool is_max> bool findExtSumOFSubarray(citer_t first, citer_t last, int* res) { constexpr auto update = [](int a, int b) { return is_max ? std::max(a, b) : std::min(a, b); }; if (0 == std::distance(first, last)) { return false; } else /* left blank intentionally */; int tmp = *first; *res = tmp; for (++first; first != last; ++first) { tmp = update(tmp + *first, *first); *res = update(*res, tmp); } return true; } bool findMaxSumOFSubarray(citer_t first, citer_t last, int* res) { return findExtSumOFSubarray<true>(first, last, res); } bool findMinSumOFSubarray(citer_t first, citer_t last, int* res) { return findExtSumOFSubarray<false>(first, last, res); } bool findMaxSumOFSubarrayINCircle(citer_t first, citer_t last, int* res) { if (std::all_of(first, last, [](int i){ return i < 0; })) { return findMaxSumOFSubarray(first, last, res); } else { const int sum_of_source = std::accumulate(first, last, 0); int max, min; if (findMaxSumOFSubarray(first, last, &max) and findMinSumOFSubarray(first, last, &min)) { *res = std::max(max, sum_of_source - min); return true; } else { return false; } } } int main() { const std::vector<int> source = {-1, -2, -3}; int res; if (findMaxSumOFSubarrayINCircle(source.begin(), source.end(), &res)) { std::cout << "Result: " << res << "." << std::endl; // Result: 31. } else /* left blank intentionally */; return 0; }
S.
#include <iostream> #include <vector> #include <algorithm> #include <numeric> using citer_t = std::vector<int>::const_iterator; template <bool is_max> bool findExtSumOFSubarray(citer_t first, citer_t last, int* res) { constexpr auto update = [](int a, int b) { return is_max ? std::max(a, b) : std::min(a, b); }; if (0 == std::distance(first, last)) { return false; } else /* left blank intentionally */; int tmp = *first; *res = tmp; for (++first; first != last; ++first) { tmp = update(tmp + *first, *first); *res = update(*res, tmp); } return true; } bool findMaxSumOFSubarray(citer_t first, citer_t last, int* res) { return findExtSumOFSubarray<true>(first, last, res); } bool findMinSumOFSubarray(citer_t first, citer_t last, int* res) { return findExtSumOFSubarray<false>(first, last, res); } bool findMaxSumOFSubarrayINCircle(citer_t first, citer_t last, int* res) { if (std::all_of(first, last, [](int i){ return i < 0; })) { return findMaxSumOFSubarray(first, last, res); } else { const int sum_of_source = std::accumulate(first, last, 0); int max, min; if (findMaxSumOFSubarray(first, last, &max) and findMinSumOFSubarray(first, last, &min)) { *res = std::max(max, sum_of_source - min); return true; } else { return false; } } } int main() { const std::vector<int> source = {-1, -2, -3}; int res; if (findMaxSumOFSubarrayINCircle(source.begin(), source.end(), &res)) { std::cout << "Result: " << res << "." << std::endl; // Result: 31. } else /* left blank intentionally */; return 0; }
Oh this code has a bug
S.
try {-1, -2, -3}
S.
output should be -1
Liam
hmmm
Liam
interesting.
S.
🙄it took me several minutes to consider the test cases too
Liam
well, fixed.
S.
well, fixed.
Nice C++ features ( still reading
Anonymous
Who has a good book to study C++?
Anonymous
google C++ of book
S.
This group doesn't share pirated resources... you can google for what you need
Stefan
https://stackoverflow.com/a/47461
Ибраги́м
thanks😊
Welcome, you're
Ибраги́м
@OxFFFFFFFF https://pastebin.com/xd6fm9Pu
https://pastebin.com/GgNrEM7Z Look at this and note the changes.
olli
https://stackoverflow.com/a/47461
So? You're referring to one of the weaker answers in the thread
Anonymous
hi
Anonymous
oky
MᏫᎻᎯᎷᎷᎬᎠ
Hi
BinaryByter
https://stackoverflow.com/a/47461
Well, first of all, goto is bad becaude it wont clear your stack
BinaryByter
IMHO thats enough justification not to use it
BinaryByter
Then there is the problem of it being kinda unpredictable
BinaryByter
+ I dont see where gotos are easier to use than... functions
Jussi
Explain please
Mihail
+ I dont see where gotos are easier to use than... functions
Escaping a nested for maybe, but that's about it
Mihail
I think in C++ it does
Mihail
Also calls destructors
Mihail
I think
Jussi
I think in C++ it does
Of course it does
Jussi
And it does clear the stack in C too as soon as you leave the acope
Mihail
But in C it doesn't
Jussi
Scope*
Jussi
Just like any if-else
Mihail
And it does clear the stack in C too as soon as you leave the acope
I don't think so, but maybe you're rights I don't know C that well
Jussi
Also, switch-case is actually the aame thing as a bunch of gotos
Jussi
I don't think so, but maybe you're rights I don't know C that well
Ofc you can break your code by calling goto to somewhere outside the current scope
Mihail
That's what we're talking about, or?
Mihail
Escaping a scope with goto (at least in C++) will clean up variables
Jussi
I just saw that one message that stated that "goto does not clear your stack" and got triggered
Jussi
It depends on how you use it, you can fill your stack by recursion too, and it is not considered bad practise
BinaryByter
BinaryByter
???????
say you have a function with some variables and you jump into another function => memory leak since f1's variables wont be cleared up
Jussi
Why not?
BinaryByter
since the routines to destroy the variables are skipped by goto
BinaryByter
raii
Nope
Jussi
So normally you lose your variables after you call another function?
BinaryByter
Nope
RAII is only a thing in compiler land
Jussi
int a=7; anotherfunction(); /* i lost a? */
BinaryByter
No
BinaryByter
read what I said
Jussi
Explain please
BinaryByter
i said GOTO
BinaryByter
you just CALL the functions
Jussi
Yeah? So I lose the variables with goto?
BinaryByter
yes
Jussi
Why so?