time-complexity

Aggregate duplicate records by maintaining the order and also include duplicate records

ぐ巨炮叔叔 提交于 2020-02-04 03:57:53
问题 I am trying to solve an interesting problem, it's easy to just do a groupBy for aggregation like sum, count etc. But this problem is slightly different. Let me explain: This is my list of tuples: val repeatSmokers: List[(String, String, String, String, String, String)] = List( ("ID76182", "sachin", "kita MR.", "56308", "1990", "300"), ("ID76182", "KOUN", "Jana MR.", "56714", "1990", "100"), ("ID76182", "GANGS", "SKILL", "27539", "1990", "255"), ("ID76182", "GANGS", "SKILL", "27539", "1990",

What is the time complexity of zip() in Python?

坚强是说给别人听的谎言 提交于 2020-02-01 12:03:27
问题 How can I calculate the time complexity of zip()? testList = [[1,2,3]for _ in range(5)] zip(*testList) 回答1: Assume you zip N iterables. In python 3.x , the zip function itself runs in O(1) time, as it just allocates a special iterable (called the zip object), and assigns the parameter array to an internal field. The function invocation itself (before control reaches in zip) is O(N) , as the interpreter must convert the parameters to an array. Every subsequent next call on the iterator also

What is the time complexity of zip() in Python?

南楼画角 提交于 2020-02-01 12:03:11
问题 How can I calculate the time complexity of zip()? testList = [[1,2,3]for _ in range(5)] zip(*testList) 回答1: Assume you zip N iterables. In python 3.x , the zip function itself runs in O(1) time, as it just allocates a special iterable (called the zip object), and assigns the parameter array to an internal field. The function invocation itself (before control reaches in zip) is O(N) , as the interpreter must convert the parameters to an array. Every subsequent next call on the iterator also

Big O of Nested Loop (int j = 0; j < i * i; ++j)

风流意气都作罢 提交于 2020-02-01 09:20:40
问题 Question 1 for (i = 0; i < n; i++) { for (j = 0; j < i * i ; j++){ } } Answer: O(n^3) At first glance, O(n^3) made sense to me, but I remember a previous problem I did: Question 2 for (int i = n; i > 0; i /= 2) { for (int j = 0; j < i; j++) { //statement } } Answer: O(n) For Question 2, the outer loop is O(log n) and the inner loop is O(2n / log n) resulting in O(n). The inner loop is O(2n / log n) because - see explanation here: Big O of Nested Loop (int j = 0; j < i; j++) Why we don't do

Number of distinct palindromic substrings

亡梦爱人 提交于 2020-01-31 18:01:52
问题 Given a string, I know how to find the number of palindromic substrings in linear time using Manacher's algorithm. But now I need to find the number of distinct/unique palindromic substrings. Now, this might lead to an O(n + n^2) algorithm - one 'n' for finding all such substrings, and n^2 for comparing each of these substrings with the ones already found, to check if it is unique. I am sure there is an algorithm with better complexity. I was thinking of maybe trying my luck with suffix trees

C++ 3sum complexity

南楼画角 提交于 2020-01-30 09:23:08
问题 I was trying to solve the 3 sum problem in cpp. Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { int size = nums.size(); vector<vector<int>> result; for (int i = 0; i < size - 2; ++i) { for (int j = i + 1; j < size - 1; ++j) { for (int k = j + 1; k < size; ++k) { if (sumToZero(i, j, k, nums)) { vector<int> newComb

Big O notation for Ruby methods?

為{幸葍}努か 提交于 2020-01-30 06:08:29
问题 How can I find the complexity of a Ruby method? For example length? If I look at the source code, I see this: static VALUE rb_ary_length(VALUE ary) { long len = RARRAY_LEN(ary); return LONG2NUM(len); } But I don't know how to read that in order to find the Big O notation. 回答1: There is no maintained list of theoretical complexities of Ruby methods. Of interest to you is minitest/benchmark , which is used like this: require 'minitest/autorun' require 'minitest/benchmark' class TestFoobar <

Big O notation for Ruby methods?

∥☆過路亽.° 提交于 2020-01-30 06:07:46
问题 How can I find the complexity of a Ruby method? For example length? If I look at the source code, I see this: static VALUE rb_ary_length(VALUE ary) { long len = RARRAY_LEN(ary); return LONG2NUM(len); } But I don't know how to read that in order to find the Big O notation. 回答1: There is no maintained list of theoretical complexities of Ruby methods. Of interest to you is minitest/benchmark , which is used like this: require 'minitest/autorun' require 'minitest/benchmark' class TestFoobar <

Big O notation for Ruby methods?

半城伤御伤魂 提交于 2020-01-30 06:07:05
问题 How can I find the complexity of a Ruby method? For example length? If I look at the source code, I see this: static VALUE rb_ary_length(VALUE ary) { long len = RARRAY_LEN(ary); return LONG2NUM(len); } But I don't know how to read that in order to find the Big O notation. 回答1: There is no maintained list of theoretical complexities of Ruby methods. Of interest to you is minitest/benchmark , which is used like this: require 'minitest/autorun' require 'minitest/benchmark' class TestFoobar <

How to calculate the complexity of a given code

大憨熊 提交于 2020-01-25 10:01:10
问题 Function(int n) if(n<=2) return 1; for(i=n ; i>n/8 ; i-=n/2) for(j=n ; j>2 ; j=j/2) syso(); return Function(n/2); In order to calculate I have done the following : T(n) = T(n/2) + O(1) + 2logn T(n/2): the recursive call to the function. O(1) : the if statement. 2logn: the first "for" will run only 2 times * (the second "for") that will run logn times. ** I have assumed that the second for loop will divide the j by 2 meaning that I will have j/2^k times iterations = logn. With the same logic