!cxPCiPlsXnajakSrqd:matrix.org

Array languages

884 Members
General discussion about APL-like array languages. See #array:matrix.org for other rooms23 Servers

Load older messages


SenderMessageTime
22 Jan 2025
@_discord_671689100331319316:t2bot.iobrian_e cool, goodluck! 11:47:05
@bear8642:matrix.orgsilas The APLcultivations cover this If you want an array oriented solution 13:25:39
@_discord_292750210755461121:t2bot.iohd3625 thank you 13:26:26
@_discord_292750210755461121:t2bot.iohd3625 ill lok at this 13:26:36
@_discord_292750210755461121:t2bot.iohd3625 * ill look at this 13:26:46
23 Jan 2025
@loke:dhsdevelopments.comlokeI haven't read the entire book. and I suspect it ties into what was discussed earlier. But as a standalone example of how to solve it, it looks a bit overly complicated?02:30:32
@_discord_636104124957458433:t2bot.ioandover77. such a great resource 02:51:57
@_discord_636104124957458433:t2bot.ioandover77. 02:51:57
@_discord_651823008347979793:t2bot.ioa.brudz Announcement: [BAA Vector Webinar](https://aplwiki.com/wiki/BAA#Webinar) in 5 mins. 15:54:09
@_discord_125549206139174912:t2bot.iorak1507#1964 changed their profile picture.18:30:27
@_discord_718551566336000090:t2bot.iojam3943598 How to do this in array style? https://leetcode.com/problems/daily-temperatures/description/ I have a solution but it's too complicated. 20:13:05
@_discord_160673472530350080:t2bot.ioludvikgalois I'm struggling to even come up with an O(n) solution that doesn't require mutation, let alone being in an array style. Although, I can do O(n log n), which probably meets the timing requirements (still not in an array style though). 20:53:37
@_discord_1162491071339712644:t2bot.iopolylokh_39446 Probably the O(n) solution is to solve it right-to-left. 20:58:44
@dzaima:matrix.orgdzaima 30 <= temperatures[i] <= 100 means there's a simple O(70×n) option, but that's largely lame 21:00:44
@_discord_718551566336000090:t2bot.iojam3943598 Best I've got: scan from the right into a list of lists of indices of decreasing values, then for each input, binary search the indices. nlogn, I think.

dailytemps_array(xs) = let
    dec_seqs = accumulate(reverse(collect(enumerate(xs))); init=Int[]) do acc, (i, x)
        j = binary_findlast(j -> xs[j] > x, acc) 
        push(acc[begin:j], i)
    end |> reverse
    ixs = map(xs, dec_seqs) do x, seq
        get(seq, binary_findlast(j -> xs[j] > x, seq), 0)
    end
    max.(0, ixs .- eachindex(xs))
end
21:02:56
@mlochbaum:matrix.orgMarshall Have also done this in the BQN channel, searching the URL should find it? 21:04:58
@_discord_1162491071339712644:t2bot.iopolylokh_39446 yeah that's the only other hit, a 2022 discussion 21:05:53
@dzaima:matrix.orgdzaima (matrix / discord) 21:06:14
@_discord_718551566336000090:t2bot.iojam3943598 Is Bins the binary-search primitive? 21:15:55
@dzaima:matrix.orgdzaima yeah 21:16:09
@_discord_718551566336000090:t2bot.iojam3943598 I feel like there's supposed to be a solution involving grade and bins 21:17:05
@dzaima:matrix.orgdzaima I for some reason think a single Bins won't be enough 21:17:37
@_discord_304605065715384321:t2bot.iofiuzeri I think I've got O(n log n) time and space in a pretty clean array style, but haven't written it down in any language yet.
the basic idea is:
generate the array
a[k][i] = max(input[i+1:i+2^k])
with each row being done in O(n) from two entries of the previous.
use this to generate:
b[k][i] = max(n s.t. max(input[i+1:i+n*2^k] < input[i])
c[k][i] = max(input[i+1:i+b[k][i]])
21:18:22
@dzaima:matrix.orgdzaima yeah, been thinking that you'd need a manual log(n) loop 21:19:28
@_discord_870115701279584326:t2bot.iodiscodoug It’s worth noting that array approaches can often have different big O complexity than traditional approaches but still perform better due to things like more efficient use of memory. “So array idiomatic” and hitting specific big O targets can be at odds. 21:19:39
@_discord_304605065715384321:t2bot.iofiuzeri each row of b is computed pointwise from the corresponding of a and the previous of c, each of c is from the corresponding of b. 21:19:51
@_discord_304605065715384321:t2bot.iofiuzeri the last of b is the result, plus or minus the zeroing out not-found elements 21:23:23
@mlochbaum:matrix.orgMarshall You mean a row of b comes from the next of c (larger k)? So first you decide whether to add the maximum 2^k to the index, then the next-highest, and so on. 21:27:24
@_discord_718551566336000090:t2bot.iojam3943598 Yeah "container with most water" also is linear with pointers and nlogn with arrays, afaik. 21:29:22
@_discord_160673472530350080:t2bot.ioludvikgalois The "expected" solution is something like
class Solution:
    def dailyTemperatures(self, temperatures):
        seen = []
        results = [0] * len(temperatures)
        for ix, t in enumerate(temperatures):
            while len(seen) > 0 and temperatures[seen[-1]] < t:
                x = seen.pop()
                results[x] = ix - x
            seen.append(ix)
        return results

which doesn't have much "noise" (if not written in Python), so in this particular case I think you're only likely to have a speed up here if your array solution can make substantial use of parallelism.
21:29:58

Show newer messages


Back to Room ListRoom Version: 6