← Home ← Back to /g/

Thread 106308762

47 posts 12 images /g/
dogesh No.106308762 >>106309013 >>106309029 >>106309319 >>106309338 >>106310869
/lg/ leetcode general
its over, its brutally fucking over. i genuinly want to kill myself. why the fuck did i think of arthimetic progression when i could simply count the zeros??? how the fuck will I get into faang with my retarded 30 iq pajeet brain
Anonymous No.106308819
This is kind of a funky question even by leetcode standards. Must be even more oversaturated than I thought.
Anonymous No.106308822
Don't be so hard on yourself.
Anonymous No.106308967 >>106309478
This is pretty easy if you notice that for a run of n zeroes the total number of subarrays is the triangular number of n. So even a trivial implementation only needs to be linear time.
Anonymous No.106309013
>>106308762 (OP)
That is an arithmetic progression though.
Anonymous No.106309029 >>106309072 >>106309073
>>106308762 (OP)
That's an absolutely trivial problem. You can be "fancy" and do the triangular numbers (arithmetic progression), or just count them like a tard. Who cares?
Anonymous No.106309072
>>106309029
Also useless.
The generalized version with maximized arrays is used in RLE compression which we used in the 80s.
Anonymous No.106309073 >>106309099 >>106309212 >>106309569 >>106311979
>>106309029
its not that trivial, its designed to trick you into thinking its trivial.
you have to find the combination of arrays, not the zeroes.
so if you had [1,3,0,0,0,0,2,0,0,0,0,4]
then it would be
There are 8 occurrences of [0] as a subarray.
There are 6 occurences of [0,0] as a subarray
There are 4 occurences of [0,0,0] as a subarray
There are 2 occurences of [0,0,0,0] as a subarray
Therefore, we return 20
Anonymous No.106309099
>>106309073
>There are 8 occurrences of [0] as a subarray.
>There are 6 occurences of [0,0] as a subarray
>There are 4 occurences of [0,0,0] as a subarray
>There are 2 occurences of [0,0,0,0] as a subarray
>Therefore, we return 20
so two times triangular number of four
wow that wasn't trivial at all
Anonymous No.106309125 >>106309159 >>106309394
What is a subarray? [1, 2, [0,1], 3, 4]. So [0,1] would be a subarray to me and only that kek. The problem gives a series of numbers without anything explicitly separated by [], so I'm assuming that by that definition there of "non-contiguousness", wouldn't [1, 3], [2] and [4] be the only subarrays, since they are separated by empty sequences? Not a programmer, of course
Anonymous No.106309159
>>106309125
Yeah, this is a math/combinatorics question.
It wouldn’t make much sense to a programmer.
Or a lisp programmer.
Anonymous No.106309212
>>106309073
Yes, I know. That's still trivial.
Anonymous No.106309319 >>106309394 >>106309401
>>106308762 (OP)
300 IQ solution
Anonymous No.106309338 >>106309618 >>106309618
>>106308762 (OP)
How about you start making something instead of doing mathfag question #129129129 in the hopes it will make you a better programmer. Try making a Chip8 emulator, a ray tracer, a little programming language, a little platformer etc etc. You'll learn more in day than you would a year doing that shit.
Anonymous No.106309394 >>106309478
>>106309319
I'm (>>106309125) getting 5, but I barely know what I'm doing. Here's the code:
let lst = [1, 3, 0, 0, 2, 0, 0, 4];

function separateByDelimiter(arr, delimiter) {
return arr.reduce((result, num) => {
if (num === delimiter) {
result.push([]);
} else {
result[result.length - 1].push(num);
}
return result;
}, [[]]).filter(subArr => subArr.length > 0);
}

let separatedlst = separateByDelimiter(lst, 0);

let totalSubarrays = separatedlst.reduce((acc, curr) => {
const n = curr.length;
const numSubarrays = (n * (n + 1)) / 2;
return acc + numSubarrays;
}, 0);

I think is pretty self-explanatory, separateByDelimiter gives the subarrays, then I use the formula ((n(n+1))/2) to get me all the possible combinations after checking for each subarray length. Got the formula from ChatGPT, dunno if that shit is hallucinated.
Anonymous No.106309401
>>106309319
jewfram only looks impressive when you do a one liner with one of the shit tons of math functions
Anonymous No.106309478 >>106309510
>>106309394
The best solution is from >>106308967, it reduces the problem to simply counting the length of each run, taking the triangle number of that length and adding them all up.
Anonymous No.106309510
>>106309478
Oh yeah, thanks, just got an explanation. Really trivial desu
Anonymous No.106309553 >>106309575 >>106309628 >>106310315 >>106311607
fn from_distance(distance: usize) -> usize {
if distance != 0 {
(1..=distance).fold(0usize, |sum, distance| distance + sum)
} else {
0
}
}

fn count_zero_filled_subarrays(numbers: &[usize]) -> usize {
let (count, distance) = numbers.iter().copied().fold((0usize, 0usize), |(count, distance), number| {
if number == 0 {
(count, distance + 1)
} else {
(count + from_distance(distance), 0)
}
});
count + from_distance(distance)
}

fn main() {
assert_eq!(count_zero_filled_subarrays(&[1, 3, 0, 0, 2, 0, 0, 4]), 6);
assert_eq!(count_zero_filled_subarrays(&[1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0]), 20);
}
Anonymous No.106309557
just ask qwen3-coder or deepseek using your local llm. AI being able to destroy leetcode in 3 seconds is a blessing.
Anonymous No.106309569 >>106309597
>>106309073
Question; why is it so fucking hard for the stack overflow types to just answer like this instead of engaging in the multilayered bullshit dance of obfuscation and bitter rage?
Anonymous No.106309575 >>106309609
>>106309553
perl?
Anonymous No.106309597
>>106309569
because it's boring and only helps pajeets
Anonymous No.106309609 >>106309622
>>106309575
Rust
Anonymous No.106309618 >>106309686
>>106309338
>>106309338
>implying brain twisters and logic puzzles bad
>implying that's the only programming that's done at the time
npc take. i'm afraid that it's still correct, since op wants a faang job
Anonymous No.106309622
>>106309609
is it supposed to look like shit or that's just your incompetence?
Anonymous No.106309628
>>106309553
I'm too brainlet for math, but replace the fold in `from_distance` with `(distance * (distance + 1)) / 2` and you have the ideal linear time solution
Anonymous No.106309686 >>106309742
>>106309618
>Wants me to prove a negative
>Ad hominem
Anonymous No.106309742 >>106309787
>>106309686
i don't want to prove anything. not everyone's here to argue, you know. i've merely pointed out that your take would be full of shit for anyone who's not dreaming to be a corpo wagecuck. that's it.
Anonymous No.106309787 >>106309885 >>106309975 >>106310155
>>106309742
>Dude just do these shitty little toy programs that do nothing!
Making something useful is superior to your no-coder-copium puzzles made by a fucking jeet (for the jeet) for every single possible outcome (except ones that result in you wasting time). There is little difference between gayming and jeetcode. Maybe I shouldn't be so harsh, maybe making something beyond 100loc is beyond your capabilities where you currently are at in life.
Anonymous No.106309885 >>106309951
>>106309787
Holy midwit. Bro crashed out fr fr, yo. There's plenty of benefits you can glean from problem solving, which both "jeetcode" and gaming can provide. This idea that everything has to be focused on 'muh productivity' all of the time is extremely brown-coded.
Anonymous No.106309951 >>106310025
>>106309885
cap
Anonymous No.106309975 >>106310017
>>106309787
this post reeks of insecurity and jealousy
Anonymous No.106310017
>>106309975
Cope and sneed.
Anonymous No.106310025 >>106310035
>>106309951
Shouldn't you be doordashing, Vivek?
Anonymous No.106310035
>>106310025
Ranjeesh, please needfully the leetcodings.
Anonymous No.106310155
>>106309787
>Making something useful is superior to your no-carver-copium toys made from a wood collected by a fucking nigger (for nigger factories) for every single possible outcome (except when useful things like furniture turn out to be a waste of time). There is little difference between gayming and carving little trinkets. Maybe I shouldn't be harsh, maybe something beyond a wooden cube is beyond your capabilities where you currently are at life
t. your post.
also, competence is bred by practice, yes - but the practice should be varied. otherwise you're a biobot one trick pony who doesnt get the principles of doing a thing. fooling around and doing as much as possible gives knowledge laying foundation to your general skill as a programmer, so by that logic leetcode (web, automation, assembly, lambda calculus, kernel, dbs, language models...) should not be avoided, but actually given a try. you don't like it, you still get what it's about. you like it, see no usecase - you still do it to take off the edge of focusing on a different task, thus increasing your productivity. you like it and there's a usecase - there's no problem, but try a different thing, like making a mobile image editor.
Anonymous No.106310315
>>106309553
Rust lambdas are hideous and you made a mess of using the streaming/functional paradigm: calling `from_distance` when `distance` is to be reset unnecessarily reiterates over items and also requires a final call to `from_distance` outside of your logic. Do the calc inline and both issues go away (you could abstract the aggregating function if you wish)

ints.Aggregate(
(subarrays: 0, nextSubarrayCount: 1),
( acc, i ) =>
0 == i
? ( acc.subarrays + acc.nextSubarrayCount, acc.nextSubarrayCount + 1 )
: ( acc.subarrays, 1 ),
acc => acc.subarrays );


But these muh funcitonal solutions are suboptimal from a performance perspective and overly complicated (always develop like an absolute retard will maintain the code in the future b/c they probably will). For an interview question I would ask what type of solution they're looking for (simplest/easiest to read or clever and/or highest performing); if quick enough I would provide both. In this case, though, the simpler procedural solution has the clear performance and readability advantages (and considerably less typing)

var subarrays = 0;
var seqLen = 0;

foreach(var i in ints)
{
if(0 == i)
{
subarrays += ++seqLen;
}
else
{
seqLen = 0;
}
}
Anonymous No.106310869
>>106308762 (OP)
This shit is fucking awful. Are they still doing this in the age of AI? Please god say it's killing leetcode. Shit is fucking retarded.
Anonymous No.106311607
>>106309553
fn count(v: &[u8]) -> u64 {
fn sum(n: u64) -> u64 {
(1..=n).sum()
}

let (acc, curr) = v.iter()
.copied()
.fold((0, 0), |(acc, curr), v| {
if v == 0 {
(acc, curr + 1)
} else {
(acc + sum(curr), 0)
}
});

acc + sum(curr)
}
Anonymous No.106311945 >>106311947
in Rust this is just
pub fn zero_filled_subarray(nums: Vec) -> i64 {
nums.chunk_by(|a, b| a == b)
.filter(|s| s[0] == 0)
.map(|s| (s.len() * (s.len() + 1) / 2) as i64)
.sum()
}
Anonymous No.106311947 >>106311981 >>106312085
>>106311945
>just
This is less readable than the resulting assembly.
Anonymous No.106311979
>>106309073
So you go along, and when you find a 0 you count how many there are.
Then you have your counter array and you start from the length of the zero fill and +1 that, then +2 the len-1, then +3 the len-2 etc until you get to the 1 length value.
Then you find the next zero filled sub array.
Anonymous No.106311981 >>106312073
>>106311947
Yeah I have a question on what filters does. Does it make an array of subarrays filled with zeros? Why use s[0] == 0, i.e. match only the first element?
Anonymous No.106312073 >>106312101
>>106311981
>Yeah I have a question on what filters does.
https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.filter
>Creates an iterator which uses a closure to determine if an element should be yielded.

>Why use s[0] == 0, i.e. match only the first element?
https://doc.rust-lang.org/stable/std/primitive.slice.html#method.chunk_by
>Returns an iterator over the slice producing non-overlapping runs of elements using the predicate to separate them.
Anonymous No.106312085
>>106311947
I believe it was Scott Meyers who said it's incredible how much more readable assembly language is than the C++ that produces it. Too bad he retired right as Rust was getting popular.
Anonymous No.106312101
>>106312073
Ok, fine, I'll rtfm. Thanks for spoonfeeding this retard.