Skip to content

Commit

Permalink
feat: add a solution for the daily problem of S0633
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonySchuijlenburg committed Jun 17, 2024
1 parent 54ae935 commit c94c210
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Contributions are welcome! If you have a more efficient solution or want to add
| 344 | Reverse String | 0 ms | 48.4 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0344/Problem.md) |
| 409 | Longest Palindrome | 6 ms | 42.4 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0409/Problem.md) |
| 506 | Relative Ranks <- Needs work! | 27 ms | 45.4 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0506/Problem.md) |
| 633 | Sum of Square Numbers | 5 ms | 40.8 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0633/Problem.md) |
| 648 | Replace Words | 686 ms | 53 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0648/Problem.md) |
| 786 | K-th Smallest Prime Fraction <- Needs work! | 672 ms | 106.5 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0786/Problem.md) |
| 846 | Hand of Straights | 59 ms | 45.5 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0846/Problem.md) |
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/anthonyschuijlenburg/S0633/Problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 633. Sum of Square Numbers

Topics: Math, Two Pointers, Binary Search

Given a non-negative integer c, decide whether there are two integers a and b such that a2 + b2 = c.

### Example 1:

Input: c = 5
Output: true
Explanation: 1 * 1 + 2 * 2 = 5

### Example 2:

Input: c = 3
Output: false

### Constraints:

0 <= c <= 2^31 - 1

18 changes: 18 additions & 0 deletions src/main/java/com/anthonyschuijlenburg/S0633/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.anthonyschuijlenburg.S0633;

public class Solution {
public boolean judgeSquareSum(int c) {
double max = Math.sqrt(c);

for (int i = 0; i <= max; i++) {
double result = Math.sqrt(c - (i * i));

// Check if result is a whole number
if (result == (int) result) {
return true;
}
}

return false;
}
}
56 changes: 56 additions & 0 deletions src/test/java/com/anthonyschuijlenburg/S0633/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.anthonyschuijlenburg.S0633;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SolutionTest {
@Test
void judgeSquareSum_testCase0() {
Solution solutionGenerator = new Solution();

boolean result = solutionGenerator.judgeSquareSum(0);
assertTrue(result);
}

@Test
void judgeSquareSum_testCase1() {
Solution solutionGenerator = new Solution();

boolean result = solutionGenerator.judgeSquareSum(3);
assertFalse(result);
}

@Test
void judgeSquareSum_testCase2() {
Solution solutionGenerator = new Solution();

boolean result = solutionGenerator.judgeSquareSum(5);
assertTrue(result);
}

@Test
void judgeSquareSum_testCase3() {
Solution solutionGenerator = new Solution();

boolean result = solutionGenerator.judgeSquareSum(4);
assertTrue(result);
}

@Test
void judgeSquareSum_testCase5() {
Solution solutionGenerator = new Solution();

boolean result = solutionGenerator.judgeSquareSum(1);
assertTrue(result);
}

@Test
void judgeSquareSum_testCase6() {
Solution solutionGenerator = new Solution();

// Largest available number (constraints)
boolean result = solutionGenerator.judgeSquareSum(Integer.MAX_VALUE - 1);
assertFalse(result);
}
}

0 comments on commit c94c210

Please sign in to comment.