Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add LeetCode problem 434 #1252

Merged
merged 4 commits into from
Apr 27, 2023

Conversation

PalAditya
Copy link
Contributor

Adds solution of problem 434 for leetcode. Beats 100% Time, 97.18% space

Description of Change

References

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • I acknowledge that all my contributions will be made under the project's license.

PalAditya and others added 3 commits April 27, 2023 23:17
Adds solution of problem 434 for leetcode. Beats 100% Time, 97.18% space
docs: updating `leetcode/DIRECTORY.md`
@github-actions github-actions bot added the Leetcode folder changes Changes to Leetcode folder. Known CI issues. label Apr 27, 2023
@PalAditya
Copy link
Contributor Author

TimeSpace

Proof of it being optimal 😄

Panquesito7
Panquesito7 previously approved these changes Apr 27, 2023
Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 🚀

Copy link
Member

@alexpantyukhin alexpantyukhin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if to simplify the solution with only one loop? Something like that:

int countSegments(char * s){
    int sLen = strlen(s);
    int prevSpace = 1;
    int result = 0;
    char currChar;

    for (int i = 0; i < sLen; i++){
        currChar = s[i];
        if (s[i] != ' ' && prevSpace){
            result++;
        }

        prevSpace = (currChar == ' ');
    }

    return result;
}

It beats 100% also :)

leetcode/src/434.c Outdated Show resolved Hide resolved
@PalAditya
Copy link
Contributor Author

@alexpantyukhin - Your version is simply superior, so I can't think of a good reason to not use it directly 😄
I have added one explanatory comment which maybe unnecessary, but hopefully still explains the correctness of the logic as I see it.
P.S: Not sure if this PR is still valid, since I have pretty much copy-pasted your version apart from the comment. Please let me know

Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this PR is still valid, since I have pretty much copy-pasted your version apart from the comment. Please let me know

Both your contribution and @alexpantyukhin's contribution will be mentioned and kept, so I don't think there's much to worry about :) Thanks.

@Panquesito7 Panquesito7 merged commit 2b88510 into TheAlgorithms:master Apr 27, 2023
10 checks passed
@PalAditya PalAditya deleted the leetcode_434 branch October 10, 2023 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Leetcode folder changes Changes to Leetcode folder. Known CI issues.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants