Skip to content

#20920 영단어 암기는 괴로워 (시간 초과) #4

@Kang-bh

Description

@Kang-bh
bool compare(const pair<string, int> &firstWord, const pair<string, int> &secondWord) {
    if (firstWord.second > secondWord.second) {
        return true;
    } else if (firstWord.second < secondWord.second) {
        return false;
    }

    if (firstWord.first.length() > secondWord.first.length()) {
        return true;
    } else if (firstWord.first.length() > secondWord.first.length()) {
        return false;
    }

    return (firstWord < secondWord);
}

```

위 코드를 아래 코드로 변경 후 시간초과 해결하였습니다.
```
bool compare(const pair<string, int> &firstWord, const pair<string, int> &secondWord) {
    if (firstWord.second == secondWord.second) {
        if (firstWord.first.length() == secondWord.first.length()) {
            return firstWord.first <  secondWord.first;
        }
        return firstWord.first.length() > secondWord.first.length();
    } 
    return firstWord.second > secondWord.second;
}
```

두 코드의 차이점 파악이 필요합니다.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions