algorithm

Triangle: Determine if an array includes a triangular triplet (Codility)

情到浓时终转凉″ 提交于 2021-02-08 21:12:01
问题 This is the Triangle problem from Codility: A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and: A[P] + A[Q] > A[R], A[Q] + A[R] > A[P], A[R] + A[P] > A[Q]. Write a function: int solution(vector<int> &A); that, given a zero-indexed array A consisting of N integers, returns 1 if there exists a triangular triplet for this array and returns 0 otherwise. For example, given array A such that: A[0] = 10, A[1] = 2, A[2] = 5, A[3] = 1,

Why is <algorithm> not needed for std::copy or std::swap?

两盒软妹~` 提交于 2021-02-08 20:37:07
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why is <algorithm> not needed for std::copy or std::swap?

故事扮演 提交于 2021-02-08 20:37:04
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why is <algorithm> not needed for std::copy or std::swap?

我的未来我决定 提交于 2021-02-08 20:36:56
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why is <algorithm> not needed for std::copy or std::swap?

你离开我真会死。 提交于 2021-02-08 20:36:43
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why is <algorithm> not needed for std::copy or std::swap?

故事扮演 提交于 2021-02-08 20:36:40
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Why is <algorithm> not needed for std::copy or std::swap?

半城伤御伤魂 提交于 2021-02-08 20:36:21
问题 According to this cplusplus.com page, std::copy is in the <algorithm> header, as is std::swap and yet this works: #include <iostream> // std::cout #include <vector> // std::vector #include <iterator> // std::ostream_iterator() #include <cstdlib> // rand(), srand() // NOT including <algorithm> int main() { srand(time(NULL)); const int SIZE = 10; std::vector<int> vec; for(int i = 0; i < SIZE; ++i) { vec.push_back(rand() % 256); } copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout

Binary insertion sort and complexity

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 17:37:45
问题 I have a simple question about using binary search in the insertion sort algorithm. More precisely, at each step of the usual insertion sort, instead of linearly comparing the element with all the elements in the previous (sorted) subarray, we just use binary search in that sorted subarray to find the place where the element belongs. I know that this reduced the number of comparisons that the algorithm makes (O(log n) instead of O(n)), but the number of swaps needed at each step still

Python list.pop(i) time complexity?

☆樱花仙子☆ 提交于 2021-02-08 14:43:40
问题 I look up online and know that list.pop() has O(1) time complexity but list.pop(i) has O(n) time complexity. While I am writing leetcode, many people use pop(i) in a for loop and they say it is O(n) time complexity and in fact it is faster than my code, which only uses one loop but many lines in that loop. I wonder why this would happen, and should I use pop(i) instead of many lines to avoid it? Example: Leetcode 26. Remove Duplicates from Sorted Array My code: (faster than 75%) class

Generating an optimal binary search tree (Cormen)

我怕爱的太早我们不能终老 提交于 2021-02-08 13:53:15
问题 I'm reading Cormen et al., Introduction to Algorithms (3rd ed.) (PDF), section 15.4 on optimal binary search trees, but am having some trouble implementing the pseudocode for the optimal_bst function in Python. Here is the example I'm trying to apply the optimal BST to: Let us define e[i,j] as the expected cost of searching an optimal binary search tree containing the keys labeled from i to j . Ultimately, we wish to compute e[1, n] , where n is the number of keys (5 in this example). The