Software Development and Programming Careers (Official Discussion Thread)

null

...
Joined
Nov 12, 2014
Messages
29,185
Reputation
4,881
Daps
46,386
Reppin
UK, DE, GY, DMV

yeah you need to sort them and then pair-wise compare them. you can shortcut by discarding anything shorter than your running longest-common-value or anything where sub-strings of that length are not equal. i have an accepted solution on leet and i'll post it here in a bit. in c++. i don't know rust yet.
 

TheAnointedOne

Superstar
Joined
Jul 30, 2012
Messages
7,784
Reputation
666
Daps
30,669
yeah you need to sort them and then pair-wise compare them. you can shortcut by discarding anything shorter than your running longest-common-value or anything where sub-strings of that length are not equal. i have an accepted solution on leet and i'll post it here in a bit. in c++. i don't know rust yet.

Revised rust solution

Code:
fn longest_common_prefix(strs: Vec<String>) -> String {
    if strs.is_empty() {
        return "".to_string() ;
    }
    let mut strs = strs.clone() ;
    strs.sort() ;
    let (first, last) = ( strs.first().unwrap(), strs.last().unwrap() ) ;    
    let min = first.len().min( last.len() ) ;
    let mut count = 0 ;
    for itr in 0..min {
        if first.as_bytes()[ itr ] != last.as_bytes()[ itr ] {
            count = itr ;
            break ;            
        }
        count += 1 ;
    }
    first.chars().take( count ).collect()    
}
 

FreshFromATL

Self Made
Joined
May 1, 2012
Messages
19,637
Reputation
2,631
Daps
43,649
Reppin
ATL
About to start studying for the Microsoft Azure Data Engineer certification (DP-203). Company paying me $1000 to get it. Looking to take the exam by the end of January which should be more than enough time to solidify my knowledge on ADF, Synapse, Databricks, etc.
 

TheAnointedOne

Superstar
Joined
Jul 30, 2012
Messages
7,784
Reputation
666
Daps
30,669
It's not enough to simply solve the problems though. The whole point of using something like rust is to write performant and safe code.

My solution for 'Plus One' (easy)

Code:
impl Solution {
    pub fn plus_one(digits: Vec<i32>) -> Vec<i32> {
        let mut ret = digits.clone() ;
        ret.reverse() ;

        for itr in ret.iter_mut() {

            println!( "{}", itr ) ;        

            if *itr < 9 {
                *itr += 1 ;
                break ;
            }

            *itr = 0 ;
        }

        if ret.last().unwrap() == &0 {
            ret.push(1) ;
        }
        
        ret.reverse() ;
        ret
    }
}

runtime 1ms beats 61.37%; memory 2.3mb beats 7.58%

So my shyt uses too much memory.


---------------------- Edit -------------------------------

New solution uses 2.1mb beats 74.1%

Code:
impl Solution {
    pub fn plus_one(digits: Vec<i32>) -> Vec<i32> {

        let mut digits = digits ;

        for itr in digits.iter_mut().rev() {
            if *itr < 9 {
                *itr += 1 ;
                return digits ;
            }

            *itr = 0 ;
        }

        digits.insert( 0, 1 ) ;
        digits    
    }
}
 
Last edited:

null

...
Joined
Nov 12, 2014
Messages
29,185
Reputation
4,881
Daps
46,386
Reppin
UK, DE, GY, DMV

here is a c++ based solution. i did not submit it because ...

the idea is

1. sort the list - do this through pointers as cheaper
2. find the shortest common length - this could be done as part of the sort
3. then compare first and last in the list i.e. two elements (ONLY) for the longest common prefix of the two and return that

Code:
static std::string longestCommonPrefix(const std::vector<std::string>& strs) {
    if (strs.size() < 1) {
      return "";
    }
    if (strs.size() < 2) {
      return strs[0];
    }
    
    uint16_t max_common_length(std::min_element(strs.begin(), strs.end(), [&max_common_length](std::string_view s1, std::string_view s2) { return s1.length() < s2.length(); })->length());
    std::vector<const std::string*> sorted;
    sorted.reserve(strs.size());
    std::transform(strs.begin(), strs.end(), std::back_inserter(sorted), [](const std::string& s) { return &s; } );
    std::sort(sorted.begin(), sorted.end(), [](const std::string*& s1, const std::string*& s2) { return *s1 < *s2; });

    for (uint16_t i = 0; i<max_common_length; ++i) {
      if (sorted[0]->operator[](i) != sorted[sorted.size()-1]->operator[](i)) {
        return strs[0].substr(0, i);
      }
    }
    
    return strs[0].substr(0, max_common_length);
  }
 

TheAnointedOne

Superstar
Joined
Jul 30, 2012
Messages
7,784
Reputation
666
Daps
30,669
here is a c++ based solution. i did not submit it because ...

the idea is

1. sort the list - do this through pointers as cheaper
2. find the shortest common length - this could be done as part of the sort
3. then compare first and last in the list i.e. two elements (ONLY) for the longest common prefix of the two and return that

Code:
static std::string longestCommonPrefix(const std::vector<std::string>& strs) {
    if (strs.size() < 1) {
      return "";
    }
    if (strs.size() < 2) {
      return strs[0];
    }
   
    uint16_t max_common_length(std::min_element(strs.begin(), strs.end(), [&max_common_length](std::string_view s1, std::string_view s2) { return s1.length() < s2.length(); })->length());
    std::vector<const std::string*> sorted;
    sorted.reserve(strs.size());
    std::transform(strs.begin(), strs.end(), std::back_inserter(sorted), [](const std::string& s) { return &s; } );
    std::sort(sorted.begin(), sorted.end(), [](const std::string*& s1, const std::string*& s2) { return *s1 < *s2; });

    for (uint16_t i = 0; i<max_common_length; ++i) {
      if (sorted[0]->operator[](i) != sorted[sorted.size()-1]->operator[](i)) {
        return strs[0].substr(0, i);
      }
    }
   
    return strs[0].substr(0, max_common_length);
  }
1) How long did this take you?

2) You really should submit your answer to see the speed/memory usage.
 

null

...
Joined
Nov 12, 2014
Messages
29,185
Reputation
4,881
Daps
46,386
Reppin
UK, DE, GY, DMV
1) How long did this take you?

2) You really should submit your answer to see the speed/memory usage.

I already solved the problem before so know how to do it.

The version above is one that I rewrote from one of my earlier solutions on my local laptop (Xcode) and then I ran some unit tests. my first solution didn't use transform and compared the entire list rather than just the first and last.

It took about 30 mins to rewrite (while I was watching the football and on thecoli)
 

MyApps

All Star
Joined
Jan 22, 2016
Messages
2,133
Reputation
51
Daps
5,303
Reppin
Oakland
If I needed to find someone to develop an app for me, where should I start?

Any help is greatly appreciated.
 

MyApps

All Star
Joined
Jan 22, 2016
Messages
2,133
Reputation
51
Daps
5,303
Reppin
Oakland
Ok that's a start. I can work on a true outline.

But for starters, it would be an app similar in style and function as LYFT but for a different service.

Bear with me. I'm green at this.
 
Top