Interesting task for programmers

tommy-gung
Site user since 22.11.2006
Offline
258
#61

ahahahah

sorry

Здесь не могла быть ваша реклама
T7
Site user since 19.09.2018
Offline
31
#62

First, in the wrong steppe array_combine. Works, but not necessarily the first example gets c [1,2,3,3,3,3,3, 6].

Then it came. The point values as an index.

https://pastebin.com/NcuYkQbe

Option in the "forehead" slightly better memory, but slower

 class Solution { 
private $ n, $ t, $ l;
public function twoSum (array $ nums, int $ target) {
$ This-> n = $ nums;
$ This-> t = $ target;
$ This-> l = count ($ nums);
$ C = $ this-> l-1;
for ($ i = 0; $ i <$ c; $ i ++) {
$ R = $ this-> calc ($ this-> n [$ i], $ i + 1);
if (false == $ r!) return [$ i, $ r];
}
return [];
}

private function calc ($ val, $ k) {
for ($ i = $ k; $ i <$ this-> l; $ i ++) {
if ($ this-> t == $ val + $ this-> n [$ i]) return $ i;
}
return false;
}
}
Gerga
Site user since 02.08.2015
Offline
89
#63

a linear version https://pastebin.com/QJvnxc1A , but still far from the best solutions to php ..

png 1.png
D
Site user since 18.12.2015
Offline
142
#64

Gerga, very strange that the way the rough sorting overtaken 49% of all variants)), it is interesting that there are other ponapisyvali then) surely count in the cycle.

On pyhe I like this happened, but once he realized the algorithm becomes more not interested



Разработка и поддержка высоконагруженных проектов.
S3
Site user since 29.03.2012
Offline
212
#65

I made their algorithm to pass tests, but still woefully bad. But this is still too much, though, and on heshtab, While I can not be cut in the algorithm (


Runtime: 5964 ms, faster than 8.52% of Python3 online submissions for Two Sum.
Memory Usage: 16 MB, less than 5.77% of Python3 online submissions for Two Sum.


---------- Posted 18.06.2020 at 17:47 ----------

Danforth:
http://imgur.com/28teQGyl.png

It's like this for 0 seconds? 🤪

T7
Site user since 19.09.2018
Offline
31
#66
Danforth:
On pyhe I like this happened

Very similar results

timo-71:
The point values as an index.
https://pastebin.com/NcuYkQbe
D
Site user since 18.12.2015
Offline
142
#67

timo-71, I have the same final decision. sotochku thought how to squeeze?

spoiler:

I thought at first that it is necessary preallotsirovat array (in Go and Rust I did), but this can not be done pyhe (SplFixedArray is still an array rather than Mapa or hash table).

T7
Site user since 19.09.2018
Offline
31
#68

https://pastebin.com/HgtXG8d2

In Python with the same algorithm :(

---------- Posted 18.06.2020 at 19:06 ----------

Danforth:
sotochku thought how to squeeze?

That the idea is no more. :(

VoV@
Site user since 22.09.2007
Offline
196
#69

Here is a C # https://dotnetfiddle.net/rX0UbZ

It seems to work correctly :)

Compile: 0.014s

Execute: 0.05s

Memory: 0b

CPU: 0.064s

⭐ Разработка Андроид-приложений (Xamarin C#). ⭐ Разработка ASP.NET (WebForms, MVC, WebAPI, Core). ⭐ Цой жив!

To post a new comment, please log in or register