Skip to content

feat: update solutions to lc problems: No.633~635 #2481

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

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions solution/0600-0699/0633.Sum of Square Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@

## 解法

### 方法一
### 方法一:数学 + 双指针

我们可以使用双指针的方法来解决这个问题,定义两个指针 $a$ 和 $b$,分别指向 $0$ 和 $\sqrt{c}$。在每一步中,我们会计算 $s = a^2 + b^2$ 的值,然后比较 $s$ 与 $c$ 的大小。如果 $s = c$,我们就找到了两个整数 $a$ 和 $b$,使得 $a^2 + b^2 = c$。如果 $s < c$,我们将 $a$ 的值增加 $1$,如果 $s > c$,我们将 $b$ 的值减小 $1$。我们不断进行这个过程直到找到答案,或者 $a$ 的值大于 $b$ 的值,返回 `false`。

时间复杂度 $O(\sqrt{c})$,其中 $c$ 是给定的非负整数。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -80,14 +84,17 @@ class Solution {
class Solution {
public:
bool judgeSquareSum(int c) {
long a = 0, b = (long) sqrt(c);
long long a = 0, b = sqrt(c);
while (a <= b) {
long s = a * a + b * b;
if (s == c) return true;
if (s < c)
long long s = a * a + b * b;
if (s == c) {
return true;
}
if (s < c) {
++a;
else
} else {
--b;
}
}
return false;
}
Expand All @@ -114,12 +121,13 @@ func judgeSquareSum(c int) bool {

```ts
function judgeSquareSum(c: number): boolean {
let a = 0,
b = Math.floor(Math.sqrt(c));
let [a, b] = [0, Math.floor(Math.sqrt(c))];
while (a <= b) {
let sum = a ** 2 + b ** 2;
if (sum == c) return true;
if (sum < c) {
const s = a * a + b * b;
if (s === c) {
return true;
}
if (s < c) {
++a;
} else {
--b;
Expand All @@ -131,22 +139,22 @@ function judgeSquareSum(c: number): boolean {

```rust
use std::cmp::Ordering;

impl Solution {
pub fn judge_square_sum(c: i32) -> bool {
let c = c as i64;
let mut left = 0;
let mut right = (c as f64).sqrt() as i64;
while left <= right {
let num = left * left + right * right;
match num.cmp(&c) {
let mut a: i64 = 0;
let mut b: i64 = (c as f64).sqrt() as i64;
while a <= b {
let s = a * a + b * b;
match s.cmp(&(c as i64)) {
Ordering::Equal => {
return true;
}
Ordering::Less => {
left += 1;
a += 1;
}
Ordering::Greater => {
right -= 1;
}
Ordering::Equal => {
return true;
b -= 1;
}
}
}
Expand Down
52 changes: 30 additions & 22 deletions solution/0600-0699/0633.Sum of Square Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@

## Solutions

### Solution 1
### Solution 1: Mathematics + Two Pointers

We can use the two-pointer method to solve this problem. Define two pointers $a$ and $b$, pointing to $0$ and $\sqrt{c}$ respectively. In each step, we calculate the value of $s = a^2 + b^2$, and then compare the size of $s$ and $c$. If $s = c$, we have found two integers $a$ and $b$ such that $a^2 + b^2 = c$. If $s < c$, we increase the value of $a$ by $1$. If $s > c$, we decrease the value of $b$ by $1$. We continue this process until we find the answer, or the value of $a$ is greater than the value of $b$, and return `false`.

The time complexity is $O(\sqrt{c})$, where $c$ is the given non-negative integer. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -76,14 +80,17 @@ class Solution {
class Solution {
public:
bool judgeSquareSum(int c) {
long a = 0, b = (long) sqrt(c);
long long a = 0, b = sqrt(c);
while (a <= b) {
long s = a * a + b * b;
if (s == c) return true;
if (s < c)
long long s = a * a + b * b;
if (s == c) {
return true;
}
if (s < c) {
++a;
else
} else {
--b;
}
}
return false;
}
Expand All @@ -110,12 +117,13 @@ func judgeSquareSum(c int) bool {

```ts
function judgeSquareSum(c: number): boolean {
let a = 0,
b = Math.floor(Math.sqrt(c));
let [a, b] = [0, Math.floor(Math.sqrt(c))];
while (a <= b) {
let sum = a ** 2 + b ** 2;
if (sum == c) return true;
if (sum < c) {
const s = a * a + b * b;
if (s === c) {
return true;
}
if (s < c) {
++a;
} else {
--b;
Expand All @@ -127,22 +135,22 @@ function judgeSquareSum(c: number): boolean {

```rust
use std::cmp::Ordering;

impl Solution {
pub fn judge_square_sum(c: i32) -> bool {
let c = c as i64;
let mut left = 0;
let mut right = (c as f64).sqrt() as i64;
while left <= right {
let num = left * left + right * right;
match num.cmp(&c) {
let mut a: i64 = 0;
let mut b: i64 = (c as f64).sqrt() as i64;
while a <= b {
let s = a * a + b * b;
match s.cmp(&(c as i64)) {
Ordering::Equal => {
return true;
}
Ordering::Less => {
left += 1;
a += 1;
}
Ordering::Greater => {
right -= 1;
}
Ordering::Equal => {
return true;
b -= 1;
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions solution/0600-0699/0633.Sum of Square Numbers/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
class Solution {
public:
bool judgeSquareSum(int c) {
long a = 0, b = (long) sqrt(c);
long long a = 0, b = sqrt(c);
while (a <= b) {
long s = a * a + b * b;
if (s == c) return true;
if (s < c)
long long s = a * a + b * b;
if (s == c) {
return true;
}
if (s < c) {
++a;
else
} else {
--b;
}
}
return false;
}
Expand Down
22 changes: 11 additions & 11 deletions solution/0600-0699/0633.Sum of Square Numbers/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::cmp::Ordering;

impl Solution {
pub fn judge_square_sum(c: i32) -> bool {
let c = c as i64;
let mut left = 0;
let mut right = (c as f64).sqrt() as i64;
while left <= right {
let num = left * left + right * right;
match num.cmp(&c) {
let mut a: i64 = 0;
let mut b: i64 = (c as f64).sqrt() as i64;
while a <= b {
let s = a * a + b * b;
match s.cmp(&(c as i64)) {
Ordering::Equal => {
return true;
}
Ordering::Less => {
left += 1;
a += 1;
}
Ordering::Greater => {
right -= 1;
}
Ordering::Equal => {
return true;
b -= 1;
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions solution/0600-0699/0633.Sum of Square Numbers/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
function judgeSquareSum(c: number): boolean {
let a = 0,
b = Math.floor(Math.sqrt(c));
let [a, b] = [0, Math.floor(Math.sqrt(c))];
while (a <= b) {
let sum = a ** 2 + b ** 2;
if (sum == c) return true;
if (sum < c) {
const s = a * a + b * b;
if (s === c) {
return true;
}
if (s < c) {
++a;
} else {
--b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ $$

最终答案即为 $f[n]$。注意答案的取模操作。

我们发现,状态转移方程中只与 $f[i - 1]$ 和 $f[i - 2]$ 有关,因此我们可以使用两个变量 $a$ 和 $b$ 来分别表示 $f[i - 1]$ 和 $f[i - 2]$,从而将空间复杂度降低到 $O(1)$。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -116,7 +114,9 @@ func findDerangement(n int) int {

<!-- tabs:end -->

### 方法二
### 方法二:动态规划(空间优化)

我们发现,状态转移方程中只与 $f[i - 1]$ 和 $f[i - 2]$ 有关,因此我们可以使用两个变量 $a$ 和 $b$ 来分别表示 $f[i - 1]$ 和 $f[i - 2]$,从而将空间复杂度降低到 $O(1)$。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,24 @@

## Solutions

### Solution 1
### Solution 1: Dynamic Programming

We define $f[i]$ as the number of derangement of an array of length $i$. Initially, $f[0] = 1$, $f[1] = 0$. The answer is $f[n]$.

For an array of length $i$, we consider where to place the number $1$. Suppose it is placed in the $j$-th position, where there are $i-1$ choices. Then, the number $j$ has two choices:

- Placed in the first position, then the remaining $i - 2$ positions have $f[i - 2]$ derangements, so there are a total of $(i - 1) \times f[i - 2]$ derangements;
- Not placed in the first position, which is equivalent to the derangement of an array of length $i - 1$, so there are a total of $(i - 1) \times f[i - 1]$ derangements.

In summary, we have the following state transition equation:

$$
f[i] = (i - 1) \times (f[i - 1] + f[i - 2])
$$

The final answer is $f[n]$. Note the modulo operation in the answer.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -93,7 +110,9 @@ func findDerangement(n int) int {

<!-- tabs:end -->

### Solution 2
### Solution 2: Dynamic Programming (Space Optimization)

We notice that the state transition equation only relates to $f[i - 1]$ and $f[i - 2]$. Therefore, we can use two variables $a$ and $b$ to represent $f[i - 1]$ and $f[i - 2]$ respectively, thereby reducing the space complexity to $O(1)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ logSystem.retrieve(&quot;2016:01:01:01:01:01&quot;, &quot;2017:01:01:23:00:00&qu

## Solutions

### Solution 1
### Solution 1: String Comparison

Store the `id` and `timestamp` of the logs as tuples in an array. Then in the `retrieve()` method, truncate the corresponding parts of `start` and `end` based on `granularity`, and traverse the array, adding the `id` that meets the conditions to the result array.

In terms of time complexity, the time complexity of the `put()` method is $O(1)$, and the time complexity of the `retrieve()` method is $O(n)$, where $n$ is the length of the array.

<!-- tabs:start -->

Expand Down