Berkeley CSUA MOTD:Entry 36222
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2024/12/24 [General] UID:1000 Activity:popular
12/24   

2005/2/17 [Computer/SW/Languages] UID:36222 Activity:insanely high
2/17    Quiz: What is an optimal algorithm for finding a contiguous sequence
        in an array that when added will yield the greatest sum, where the
        array contains both positive and negative numbers?
                \_ -5 1 -2 8 -5 1 -1 100 -3 where the greatest sequence is
                   8-5+1-1+100
                   \_ Do you mean the greatest sequence of five numbers?
                      The greatest sequence is just all the positive numbers
                      if I understand your concept of "sequence"
                      correctly.
                      \_ The numbers must be adjacent, and the length of the
                         sequence is arbitrary. -!op
                         \_ The algorithm to solve this is still linear time.
                            Email me if you want the solution.  Hint:
                            partition the array into 3 sums. -- ilyas
              I ask again, do you partition the array into lumps of -/
              positive and negative blocks, and go from there? Mine I think
              works but requires you to do so first.
              \_ No.  I ask that you email me for a complete solution, because
                 I want to make sure this isn't homework. -- ilyas
        \_ This is trivially linear time. -- ilyas
           \_ No it's not.  Assuming you're looking for the contiguous subset
              of the input which has the largest sum, there are around N*N/2
              possibilities, and a simple 2-pass linear search is not
              gauranteed to find the right answer.
          \_ does your algorithm first repartition the array into a sequence
             of alternating positive and negative integers?
             \_ Look, maybe you didnt state your problem correctly, but as
                stated, the problem is easily solved by going through the array
                once, and putting all positive numbers into the sequence.
                  -- ilyas
                \_ don't be a moron, obviously he means a contiguous sequence
                   in the original array.  -tom
                   \_ Maybe he meant 'greatest increasing subsequence.'
                      Sequences have nothing to do with adjacency.
                      Obviously indeed.  You seem really smart, tom. -- ilyas
                      \_ It *is* obvious that op is not asking "what is
                         the set of numbers which adds up to the greatest
                         sum, given a set of positive and negative numbers"?
                         You seem really stupid, ilyas.  -tom
                         \_ That much is clear from my reply: 'you probably
                            didn't state the problem correctly', etc.  What
                            you seem to be missing is that there are multiple
                            interesting problems he may in fact be asking that
                            all involve arrays and sequences.  I am not sure
                            why I am wasting my time explaining this to you.
                              -- ilyas
                            \_ Wow, I didn't realize you meant that in
                               "This is trivially linear time" -!tom
                \_ -5 1 -2 8 -5 1 -1 100 -3 where the greatest sequence is
                   8-5+1-1+100
                   \_ Do you mean the greatest sequence of five numbers?
                      The greatest sequence is just all the positive numbers
                      if I understand your concept of "sequence"
                      correctly.
                      \_ The numbers must be adjacent, and the length of the
                         sequence is arbitrary. -!op
                         \_ The algorithm to solve this is still linear time.
                            Email me if you want the solution.  Hint:
                            partition the array into 3 sums. -- ilyas
        \_ Add all the positive numbers in the array and return that.
           In other words, maybe you want to define sequence, or give us
           more information.
        \_ What is the optimal algorithm for finding the hottest, best match
           for a long-term monogamous heterosexual relationship?
        \_ Not sure this is right.  But take a running total.  Mark the array
           index when the total is the lowest.  Mark the array index when the
           total is the highest.  The sequence starts with the number after
           the first array index, and ends with the number at the second array
           index.
           \_ This greedy algorithm fails. -- ilyas
              \_ Usually you provide an example.
                 \_ For instance what happens when the lowest cumulative sum
                    is after the highest cumulative sum?  If you constrain the
                    former to appear before the latter, it's not a global
                    max/min anymore, etc.  Lazy bitch. -- ilyas
                    \_ Let's add one more feature.  Track the highest single
                       positive value.  If the highest single positive value
                       is greater than the sum of the sequence, then the
                       final answer is just the single value.
                       "Lazy bitch"?  Dude, what's wrong with you?
                       \_ This still doesn't work.  You didn't address the
                          problem of min occuring after max. -- ilyas
                          \_ Yeah.  I was a little eager to post originally.
                             Sigh ... but if the global minimum running
                             total occurred befored the global maximum running
                             total, I'd be schweet.  Again, though:
                             "Lazy bitch"?  Dude, what's wrong with you?
                             \_ It's the price you pay for not thinking of a
                                counterexample yourself.  Lazy bitch. -- ilyas
                                \_ Dude, you need to stop with the anti-social
                                   behavior.
                                   \_ And you need to stop being lazy.  We
                                      all could use improvement.  I thought
                                      I was being eminently reasonable in both
                                      providing the requested counterexample,
                                      and gently chiding the sin of sloth,
                                      which, as our Christian friends will tell
                                      us, is deadly. -- ilyas
                                      \_ You must play a Silverfang. Probably a
                                         a theurge, I'd guess.  Nowhere else
                                         would you see a combination of cryptic
                                         utterances, arrogant stubbornnes,
                                         and haughty condescending
                                         intellectualism all wrapped around a
                                         core of inflexible superiority bound
                                         together by a completely unapologetic
                                         nigh impregnable psyche.  Bravo -- I'm
                                         impressed.  Now, the question is, are
                                         you this way in real life, or are you
                                         just giving motd a non-stop
                                         demonstration of your rp abilities?
                                         I'd guess the latter, but I'm sure
                                         there are motd denizens that would
                                         disagree with me.    --!pp
                                         \_ Paolo says I am Tzimisce. -- ilyas
                                            \_ In this context, that's quite
                                               a compliment -- though perhaps
                                               somewhat backhanded iir all the
                                               details....
        \_ Seems like you can just keep a running count. If your next number is
           bigger than your count would be by adding it, then you remember your
           previous sequence if it's the biggest so far and start a new one.
           You'd also have to keep the high point of the current sequence.
        \_ Okay, I think this works.  It's a two-pass solution.
           Pass 1:  Take the running total solution.  Find the array index
           where the minimum running total occurs.  Find the index for the
           max running total.  If the min occurs before max, the sequence is
           between the two.
           Pass 2:  If the min occurs after the max, then find the index of
           the max after the min.  The sequence will occur between the min
           and and the new max.
           Edge cases should be straightforward.
           \_ This doesn't work either.  The point of 170 homework is that
              you prove the thing works. -- ilyas
2024/12/24 [General] UID:1000 Activity:popular
12/24   

You may also be interested in these entries...
2011/3/7-4/20 [Computer/SW/Languages/C_Cplusplus] UID:54056 Activity:nil
3/7     I have a C question.  I have the following source code in two identical
        files t.c and t.cpp:
                #include <stdlib.h>
                int main(int argc, char *argv[]) {
                  const char * const * p1;
                  const char * * p2;
	...
2009/4/30-5/6 [Computer/Theory] UID:52923 Activity:nil
4/30    Sorting question!  I have n sorted arrays of doubles.  What's the
        fastest way to sort them into 1 big sorted array?
        \_ as mentioned below: you are describing one half of mergesort
        \_ You really have to ask this question?
           \_ You don't know either, huh?
        \_ If three are n sorted arrays of m doubles each, I think the fastest
	...
2009/4/5-5/3 [Computer/SW/Languages, Computer/HW/Drives] UID:52801 Activity:nil
4/5     Tuesday at 10:00PM I will be taking down Soda temporarily to migrate
        disk arrays.  This is an expected part of our move to the new server.
        The downtime should be minimal - I'll be remounting /home read-only
        for an hour or so while I run a final mirror over to the new array
        and then will reboot Soda onto the new array. The final migration
        will come at a slightly later time - this is in preparation for
	...
2009/1/20-26 [Computer/SW/Languages/Java, Computer/SW/Languages/Misc] UID:52425 Activity:nil
1/20    I've been using tcsh as shell program tool (i know, bad shell to
        do scripting).  One thing I've noticed when I extract xml file
        is that the variable type automatically change from
        integer/string to... almost an array-like data structure when
        the output of the xml key/value is more than one (it's more
        like a string separated by space, but I was very impressed as
	...
2008/12/4-10 [Computer/HW/CPU, Computer/HW/Drives] UID:52163 Activity:nil
12/4    A question to you old crufy alumni: So lately we've suggested
        VMs, and been asked why it's necessary. We've suggested top-of-the-line
        hardware and been told we don't need that much power. So I'd like to
        ask -- what exactly do you think the CSUA is supposed to _be_?
        \_ Noone said VMs weren't needed.  They suggested you use the
        \_ No one said VMs weren't needed.  They suggested you use the
	...
2008/11/29-12/6 [Computer/SW/OS/FreeBSD, Computer/SW/OS/VM] UID:52129 Activity:moderate
11/29   I'm experimenting with virtualization, and as a poor college student
        I'm wondering what the best alternatives for virtualization are, and
        how best to cut my teeth on messing with non-linux platforms (or I
        guess interesting stuff on Linux would work too). Right now I've got
        FreeBSD7 running on KVM on my home computer (on a Core 2 Quad), and am
        somewhat at a loss as to how to use it. (More details: bridged
	...
2008/11/24-29 [Computer/SW/Languages/C_Cplusplus] UID:52093 Activity:nil
11/23   C++ question: If I have "char *p = new char[10];", is there any real
        difference between "delete p;" and "delete[] p;" after compilation?
        The Standard C free() function only has one form and works for freeing
        both a single character and a character array.  Why are there separate
        forms in C++?  Thx.
        \_ For an array of char, there's no difference.  If you have an array
	...
2008/6/9-12 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Security] UID:50194 Activity:nil
6/8     CSUA code guru please help. I need to see my random number
        generator with a good seed (I just need random 18 bit
        identifiers). The usual time(NULL) is OK, except my program
        might be invoked faster than once a second, and seeding using
        time() produced the same result. I tried clock() but it seems
        to return 0. My program needs to be run in Linux/DOS (Watcom
	...
2008/4/29-5/5 [Computer/SW/Languages/Perl, Computer/SW/Languages/Python] UID:49852 Activity:moderate
4/29    Scaling your web app in the real world:
        http://teddziuba.com/2008/04/im-going-to-scale-my-foot-up-y.html
        \_ This article is crap.  While yes, 99.9% of all websites don't
           need any serious scalability plans, if any of them become worth
           anything they will need to scale.  If you write a web application
           without careing about scalability you are writing a webapp that can
	...
2007/11/13-16 [Computer/HW, Computer/HW/Drives] UID:48631 Activity:nil
11/12   What is diff between SAN and NAS?
         \_ NAS=Network Attached Storage.  Appliance which provides disk
             to servers/clients via file-based protocols (NFS, CIFS, iSCSI).
            SAN=Storage Area Netwoork.  Provides direct fiber connections from
             multiple servers to a single storage array.  -tom
            \_ Here is another way to explain It: They're very similar in that
	...