algorithm

How to find the longest substring that consists of the same char?

江枫思渺然 提交于 2021-02-20 02:40:55
问题 Now I try to solve task about finding length the longest substring that consists of the same char. For example, I have a string yyuuufhvksoooo , then I will get result 4 . I wrote this code, here it is: function longRepeat(line) { if (line.length > 0) { var count = 1; var max = 1; } for (let i = 0; i < line.length - 1; i++) { if (line[i] == line[i + 1]) { count++; if (count > max) max = count; } else { count = 1; } } return max; } It is work. But when I test this code with big string,

How to find the longest substring that consists of the same char?

时光怂恿深爱的人放手 提交于 2021-02-20 02:39:08
问题 Now I try to solve task about finding length the longest substring that consists of the same char. For example, I have a string yyuuufhvksoooo , then I will get result 4 . I wrote this code, here it is: function longRepeat(line) { if (line.length > 0) { var count = 1; var max = 1; } for (let i = 0; i < line.length - 1; i++) { if (line[i] == line[i + 1]) { count++; if (count > max) max = count; } else { count = 1; } } return max; } It is work. But when I test this code with big string,

How to find the longest substring that consists of the same char?

浪尽此生 提交于 2021-02-20 02:37:57
问题 Now I try to solve task about finding length the longest substring that consists of the same char. For example, I have a string yyuuufhvksoooo , then I will get result 4 . I wrote this code, here it is: function longRepeat(line) { if (line.length > 0) { var count = 1; var max = 1; } for (let i = 0; i < line.length - 1; i++) { if (line[i] == line[i + 1]) { count++; if (count > max) max = count; } else { count = 1; } } return max; } It is work. But when I test this code with big string,

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

find all possible paths in a tree in python

馋奶兔 提交于 2021-02-19 08:33:57
问题 I am trying to create a list with all possible paths in a tree. I have following structure given (subset from DB): text = """ 1,Product1,INVOICE_FEE, 3,Product3,INVOICE_FEE, 7,Product7,DEFAULT, 2,Product2,DEFAULT,7 4,Product4,DEFAULT,7 5,Product5,DEFAULT,2 """ where the columns are: ID, product-name, invoice-type, reference-to-parent-ID. I would like to create list with all possible paths, like in the example: [[Product1],[Product3],[Product7,Product2,Product5],[Product7,Product4]] I do

find all possible paths in a tree in python

ε祈祈猫儿з 提交于 2021-02-19 08:33:25
问题 I am trying to create a list with all possible paths in a tree. I have following structure given (subset from DB): text = """ 1,Product1,INVOICE_FEE, 3,Product3,INVOICE_FEE, 7,Product7,DEFAULT, 2,Product2,DEFAULT,7 4,Product4,DEFAULT,7 5,Product5,DEFAULT,2 """ where the columns are: ID, product-name, invoice-type, reference-to-parent-ID. I would like to create list with all possible paths, like in the example: [[Product1],[Product3],[Product7,Product2,Product5],[Product7,Product4]] I do

Word Frequency Statistics in C (not C++)

末鹿安然 提交于 2021-02-19 08:22:27
问题 Given a string consists of words separated by a single white space, print out the words in descending order sorted by the number of times they appear in the string. For example an input string of “ab bc bc” would generate the following output: bc : 2 ab : 1 The problem would be easily resolved if C++ data structures, like a map, is used. But if the problem could only be solved in plain old C, it looks much harder. What kind of data structures and algorithms shall I use here? Please be as

Velocity verlet algorithm not conserving energy

筅森魡賤 提交于 2021-02-19 07:46:42
问题 I was under the impression that the algorithm should conserve energy if the system being modelled does. I'm modelling the solar system, which should conserve energy. The program conserves angular momentum and does produce stable orbits, but the total energy (kinetic + gravitational potential) oscillates around some baseline. The oscillations are significant. Are there common reasons why this might happen? Model assumes planets are point masses, circular orbits (I've also tried elliptical

Python blurred image creation, to create a beautifully colored painting from black to white

扶醉桌前 提交于 2021-02-19 07:43:44
问题 on Pyhton I wanted to create a picture that goes from black to white, and I wrote the following code. But I think I'm doing a very small mistake, and that's the result. I actually wanted to create a similar image. Can you see where I made a mistake? import numpy as np from PIL import Image width = 100 height = 100 img = np.zeros((height, width), dtype=np.uint8) xx, yy=np.mgrid[:height, :width] circle = (xx - 50)**2 + (yy- 50)**2 for x in range (img.shape[0]): for y in range (img.shape[1]):