Skip to content

Commit e7f39d6

Browse files
committed
1437. Check If All 1's Are at Least Length K Places Away
0 parents  commit e7f39d6

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.mvn/
2+
/target/

pom.xml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.rkabanov</groupId>
7+
<artifactId>leetcode</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<name>leetcode</name>
11+
<!-- FIXME change it to the project's website -->
12+
<url>http://www.example.com</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.release>17</maven.compiler.release>
17+
</properties>
18+
19+
<dependencyManagement>
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.junit</groupId>
23+
<artifactId>junit-bom</artifactId>
24+
<version>5.11.0</version>
25+
<type>pom</type>
26+
<scope>import</scope>
27+
</dependency>
28+
</dependencies>
29+
</dependencyManagement>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter-api</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
<!-- Optionally: parameterized tests support -->
38+
<dependency>
39+
<groupId>org.junit.jupiter</groupId>
40+
<artifactId>junit-jupiter-params</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
47+
<plugins>
48+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
49+
<plugin>
50+
<artifactId>maven-clean-plugin</artifactId>
51+
<version>3.4.0</version>
52+
</plugin>
53+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
54+
<plugin>
55+
<artifactId>maven-resources-plugin</artifactId>
56+
<version>3.3.1</version>
57+
</plugin>
58+
<plugin>
59+
<artifactId>maven-compiler-plugin</artifactId>
60+
<version>3.13.0</version>
61+
</plugin>
62+
<plugin>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<version>3.3.0</version>
65+
</plugin>
66+
<plugin>
67+
<artifactId>maven-jar-plugin</artifactId>
68+
<version>3.4.2</version>
69+
</plugin>
70+
<plugin>
71+
<artifactId>maven-install-plugin</artifactId>
72+
<version>3.1.2</version>
73+
</plugin>
74+
<plugin>
75+
<artifactId>maven-deploy-plugin</artifactId>
76+
<version>3.1.2</version>
77+
</plugin>
78+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
79+
<plugin>
80+
<artifactId>maven-site-plugin</artifactId>
81+
<version>3.12.1</version>
82+
</plugin>
83+
<plugin>
84+
<artifactId>maven-project-info-reports-plugin</artifactId>
85+
<version>3.6.1</version>
86+
</plugin>
87+
</plugins>
88+
</pluginManagement>
89+
</build>
90+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.rkabanov.leetcode.p1437;
2+
3+
class Solution {
4+
public boolean kLengthApart(int[] nums, int k) {
5+
if (k == 0) {
6+
// special case, who knew
7+
return true;
8+
}
9+
10+
// fast-forward to first 1
11+
int pos = 0;
12+
while (0 == nums[pos]) {
13+
pos++;
14+
15+
if (pos >= nums.length) {
16+
// went through whole list but no single 1 encountered. they think it's good
17+
return true;
18+
}
19+
}
20+
21+
// current pos is known to hold 1, so let's move on to next
22+
pos++;
23+
if (pos >= nums.length) {
24+
// list is over, and no other 1 encountered - we're good
25+
return true;
26+
}
27+
28+
int distance = 0;
29+
30+
// now we're looking at next number after 1
31+
for (; pos < nums.length; pos++) {
32+
if (1 == nums[pos]) {
33+
// meet next 1 - measure distance
34+
if (distance < k) {
35+
return false;
36+
}
37+
38+
// good, long enough, but now let's start count over
39+
distance = 0;
40+
} else {
41+
distance++;
42+
}
43+
}
44+
45+
return true;
46+
}
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.rkabanov.leetcode.p1437;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void shouldHandleKZero() {
11+
var solution = new Solution();
12+
boolean result = solution.kLengthApart(new int[]{1, 1, 1, 1, 1}, 0);
13+
assertTrue(result);
14+
}
15+
16+
@Test
17+
public void shouldHandleAllZeros() {
18+
var solution = new Solution();
19+
// special case, who knew
20+
boolean result = solution.kLengthApart(new int[]{0, 0, 0}, 2);
21+
assertTrue(result);
22+
}
23+
24+
@Test
25+
public void shouldHandleEndingZero() {
26+
var solution = new Solution();
27+
// 1 stands tight to 1, that's 0 distance, too close
28+
boolean result = solution.kLengthApart(new int[]{1, 1, 1, 0}, 3);
29+
assertFalse(result);
30+
}
31+
32+
@Test
33+
public void shouldCase36() {
34+
var solution = new Solution();
35+
// no other 1 closer than 1. actually no other 1 at all
36+
boolean result = solution.kLengthApart(new int[]{1, 0, 0, 0}, 1);
37+
assertTrue(result);
38+
}
39+
}

0 commit comments

Comments
 (0)