Skip to content

Commit

Permalink
LeetCode1004. Max Consecutive Ones III
Browse files Browse the repository at this point in the history
알고리즘: 투포인터
복잡도: O(n)
사고과정
1. 왜 못풀었을까? 0의 갯수를 카운팅해서 k와 비교하겠다는 생각을 안했기 때문에.. 계속 올라가는 것이 rt 인데, lt를 while로 안에서 조종하면 되겠다는 생각을 안했기 때문에
  • Loading branch information
jinkshower committed Sep 3, 2024
1 parent df752c0 commit 2389ae2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main/java/review/leetcode1004.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package review;

public class leetcode1004 {
public int longestOnes(int[] nums, int k) {
int lt = 0;
int rt = 0;
int max = -1;
int zeros = 0;
while (rt < nums.length) {
if (nums[rt] == 0) {
zeros++;
}
while (zeros > k) {
if (nums[lt] == 0) {
zeros--;
}
lt++;
}
max = Math.max(max, rt - lt + 1);
rt++;
}
return max;
}
}

0 comments on commit 2389ae2

Please sign in to comment.