sum

Maximum sum of two elements in an array minus the distance between them

ⅰ亾dé卋堺 提交于 2021-02-19 23:19:17
问题 I am trying to find the maximum sum of two elements in an array minus the distance between them. Specifically I am trying to calculate max{ a[i]+a[j]-|i-j| } I am currently stuck. I have obviously considered the naive approach (O(n^2)). However ,I am pretty sure there is a better ,more efficient approach (O(nlogn)) or even O(n). Can someone please help me on how to approach the problem. I would be grateful if anyone threw some hints or a simple idea to have something to start from. Sorting

Maximum sum of two elements in an array minus the distance between them

ぐ巨炮叔叔 提交于 2021-02-19 23:09:14
问题 I am trying to find the maximum sum of two elements in an array minus the distance between them. Specifically I am trying to calculate max{ a[i]+a[j]-|i-j| } I am currently stuck. I have obviously considered the naive approach (O(n^2)). However ,I am pretty sure there is a better ,more efficient approach (O(nlogn)) or even O(n). Can someone please help me on how to approach the problem. I would be grateful if anyone threw some hints or a simple idea to have something to start from. Sorting

Bind a TextBox to the Sum of a DataTable Column

谁说胖子不能爱 提交于 2021-02-19 08:15:08
问题 Context I would like to have a footer under my grid to show some stats about the datas. Such as Sum of some columns and average of some other. Half solutions I've found I've found two things intersting to help me do this, binding the databox to a bindingSource, BUT it's only the selected line that is shown into the textBox... myTextBox.DataBindings.add("Text", myGrid.DataSource,"Weight") And getting the Sum of a column in the grid, BUT it doesn't update if I change the grid :S myTextBox.Text

SQL: How to select rows that sum up to certain value

寵の児 提交于 2021-02-19 03:43:07
问题 I want to select rows that sum up to a certain value. My SQL (SQL Fiddle): id user_id storage 1 1 1983349 2 1 42552 3 1 367225 4 1 1357899 37 1 9314493 It should calculate the sum up to 410000 and get the rows. Meanwhile it should get something like this: id user_id storage 2 1 42552 3 1 367225 As you can see, 42552 + 367225 = 409777. It selected two rows that are nearly 410000. I have tried everything but it didn't work :( Sorry for my language, I am German. 回答1: You can use a correlated

Summing a 2D array by “group”

为君一笑 提交于 2021-02-17 07:20:10
问题 I have an array in the form [ ['16-24', 192081], ['16-24', 94452], ['16-24', 1055], ['25-34', 192081], ['25-34', 94452], ['25-34', 1055], ... ] I have many items in the array that I'd like to reduce down to the unique left-hand entries. This is an example of what I'd like to reduce down to [ ['16-24', 287588], ['25-34', 287588], ... ] Obviously the numbers here are a contrived example. I've looked into javascript's array reduce but I'm unsure how to use it to achieve this effect 回答1: Use

Summing a 2D array by “group”

本小妞迷上赌 提交于 2021-02-17 07:20:06
问题 I have an array in the form [ ['16-24', 192081], ['16-24', 94452], ['16-24', 1055], ['25-34', 192081], ['25-34', 94452], ['25-34', 1055], ... ] I have many items in the array that I'd like to reduce down to the unique left-hand entries. This is an example of what I'd like to reduce down to [ ['16-24', 287588], ['25-34', 287588], ... ] Obviously the numbers here are a contrived example. I've looked into javascript's array reduce but I'm unsure how to use it to achieve this effect 回答1: Use

Dividing 2 numbers returns 0

不羁岁月 提交于 2021-02-17 05:57:28
问题 I'm trying to divide 2 counts in order to return a percentage. The following query is returning 0 : select ( (select COUNT(*) from saxref..AuthCycle where endOfUse is null and addDate >= '1/1/2014') / (select COUNT(*) from saxref..AuthCycle where addDate >= '1/1/2014') ) as Percentage Should I be applying a cast? 回答1: I would do it differently, using two sum s: select sum ( case when endOfUse is null and addDate >= '1/1/2014' then 1 else 0 end ) * 100.0 -- if you want the usual 0..100 range

The sum() function seems to be messing map objects

让人想犯罪 __ 提交于 2021-02-17 05:56:09
问题 I'm doing this code excercise for trying out functional programming in python, and I ran into problems with sum(), list(), and map objects. I don't understand what I'm doing wrong, but the list() function seems to be screwing with my map object. This is the code I have: people = [{'name': 'Mary', 'height': 160}, {'name': 'Isla', 'height': 80}, {'name': 'Sam'}] heights = map(lambda x: x['height'], filter(lambda x: 'height' in x, people)) print(len(list(heights))) print(sum(list(heights)))

TypeError: unsupported operand type(s) for +: 'int' and 'str' when using str(sum(list))

血红的双手。 提交于 2021-02-17 05:24:42
问题 I'm going through the Python tutorial and have no idea why my code isn't working. I know I need to tell Python to print an integer manually but I have already put str(sum(ages)) . Can anyone tell me why this is happening? ages = ['24','34','51','36','57','21','28'] print('The oldest in the group is ' + str(max(ages)) + '.') print('The youngest in the group is ' + str(min(ages)) + '.') print('The combined age of all in the list is ' + str(sum(ages)) + '.') Error: File "list2.py", line 4, in

Sum column up to the current row in SQL?

不想你离开。 提交于 2021-02-17 03:40:08
问题 I'm trying to sum a column up to the current row (in SQL Server). How do I do this? select t1.CounterTime, t1.StartTime, t1.EndTime, isNull(t1.value, 0) as value1, -- How do I make Total1 the sum of t1.value over all previous rows? sum( isNull(t1.value, 0) ) over (partition by t1.CounterTime order by t1.CounterTime) as Total1 from SomeTable t1 order by t1.CounterTime But I got the partition by wrong... ╔═══╦═════════════════════════╦═════════════════════════╦═════════════════════════╦════════