Sort by

recency

|

1253 Discussions

|

  • + 0 comments

    Using bitwise "|" and Binary is way more better to find max subject

    max_subject = 0
    max_team = 0
    
    for first_i in range(len(people)):
        for second_i in range(first_i + 1, len(people)):
            first_subject = people[first_i]
            second_subject = people[second_i]
    
            i_k = int(first_subject, 2)
            i_j = int(second_subject, 2)
    
            current_max_sub = bin(i_k | i_j)[2:].count("1")
    
            if  current_max_sub > max_subject:
                max_subject = current_max_sub
                max_team = 1
            elif current_max_sub == max_subject:
                max_team += 1
    
    return [max_subject, max_team]
    

    *

  • + 0 comments

    My C Solution

    int* acmTeam(int topic_count, char** topic, int* result_count) {
        int count = 0, team = 0,c = 0,d = 1,max = 0,_total = (topic_count*(topic_count - 1))/2;
        for (int i = 0;i < _total;i++) {
            for (int j = 0;j < strlen(topic[0]);j++) {
                if ((topic[c][j] == '1' && topic[d][j] == '1') || (topic[c][j] == '1' && topic[d][j] == '0') || (topic[c][j] == '0' && topic[d][j] == '1')) {
                    count++;
                }
            }
            if (d == topic_count-1) {
                c++;
                d = c + 1;
            }else {
                d++;
            }
            if (max < count) {
                max = count;
                team = 1;
                count = 0;
            }
            if (max == count) {
                team++;
            }
            count = 0;
    
        }
        *result_count = 2;
        int* result = (int*)malloc((*result_count)*sizeof(int));
        result[0] = max;
        result[1] = team;
        return result;
    
    }
    
  • + 0 comments

    This is so over the top, but I refuse to do it otherwise

    class BinaryString {
    public:
        std::string value;
    
        BinaryString(const std::string& bin_str) : value(bin_str) {}
    
        // Overload the + operator
        BinaryString operator+(const BinaryString& other) const {
            std::string result = "";
            
            for (int i = 0; i<value.size(); i++) {
                if(value[i]=='1' || other.value[i]=='1'){
                    result+="1";
                }
            }
            std::reverse(result.begin(), result.end());
            return BinaryString(result);
        }
    };
    
    
    
    vector<int> acmTeam(vector<string> topic) {
        int subjects = 0;
        int knowitalls = 0;
        std::vector<BinaryString> teams;
        std::transform(topic.begin(), topic.end(), std::back_inserter(teams), [](const std::string& s) { return BinaryString(s); });
        
        for(int i = 0; i<teams.size(); i++){
            for(int j = i+1; j<teams.size(); j++){
                 BinaryString team_sum = teams[i] + teams[j];
                 
                 if(team_sum.value.size()>subjects){
                    subjects = team_sum.value.size();
                    knowitalls = 0;
                 }
                 
                if(team_sum.value.size()==subjects){
                    knowitalls++;
                 }
            }
        }
        
        
        return {subjects, knowitalls};
    }
    
  • + 0 comments

    def acmTeam(topic): # Write your code here combs = [ sum([ 1 if i=='1' or j=='1' else 0 for i, j in zip(s1, s2)]) for s1, s2 in combinations(topic, 2)] key_value = sorted(Counter(combs).items())[-1] return list(key_value)

  • + 0 comments

    my kotlin solution

    fun knownTopics(topic1 : String,topic2 : String) : Int{
        var res = 0
        for(t in 0..topic1.length-1){
            if(topic1.get(t) == '1' || topic2.get(t) == '1')
                res++
        }
        return res
    }
    /*
     * Complete the 'acmTeam' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts STRING_ARRAY topic as parameter.
     */
    
    fun acmTeam(topics: Array<String>): Array<Int> {
        // Write your code here
        var scores = mutableListOf<Int>()
        for(i in 0..topics.size-2){
            var p1 = topics.get(i)
            for(j in i+1..topics.size-1){
                var p2 = topics.get(j)
                scores.add(knownTopics(p1, p2))
            }
        }
        scores.sortDescending()
        var res = Array<Int>(2){0;0}
        res[0]=scores.get(0)
        res[1]=scores.count{
            it == res[0]
        }
        return res
    }