Problem:
Say that a "clump" in an array is a series of 2 or more adjacent elements of the same value. Return the number of clumps in the given array.
countClumps({1, 2, 2, 3, 4, 4}) → 2
countClumps({1, 1, 2, 1, 1}) → 2
countClumps({1, 1, 1, 1, 1}) → 1
Solution:
public int countClumps(int[] nums) { boolean match = false; int count = 0; for (int i = 0; i < nums.length-1; i++) { if (nums[i] == nums[i+1] && !match) { match = true; count++; } else if (nums[i] != nums[i+1]) { match = false; } } return count; }
public int countClumps(int[] nums) {
ReplyDeleteint count = 0;
for(int i = 0;i<nums.length-1;i++){
if(nums[i] == nums[i+1]&&
(i==0||nums[i-1]!=nums[i])){
count++;
}
}
return count;
}
Your solution is excellent, but I do not understand it. Can you explain how you are using the boolean variable match? I do not understand its function.
ReplyDeleteThanks again for your solution.
DeleteThanks again for your solution.
DeleteThe match variable prevents the program from counting the same clump twice. For example, if the clump is of length 3 and we didn't check the match variable, count would be incremented twice for the same clump because nums[i] == nums[i+1] would be true for 2 iterations of the loop. The match variable tells the program that this matching clump has already been accounted for.
DeleteKyle great solution
DeleteThanks for the explanation, Shawn. Actually, I understand the purpose of the match variable. However, I do not know how to interpret !match. What does this mean if the variable is originally set at match = false? Does it mean if match == true?
ReplyDelete!match is same as match==false
DeleteHere is a better solution
ReplyDeletepublic int countClumps(int[] nums) {
int count = 0;
for(int i = 0; i < nums.length - 1; i++){
if(nums[i] == nums[i+1])
count++;
while(i < nums.length-1 && nums[i] == nums[i+1])
i++;
}
return count;
}
// Love Alpha
public int countClumps(int[] nums) {
ReplyDeleteint count = 0;
boolean match = Arrays.stream(nums).allMatch(s -> s==(nums[0]));
if (nums.length == 0)
return 0;
else if (match)
return 1;
for (int i = 0; i < nums.length - 1; i++) {
while (nums[i] == nums[i + 1]) {
i++;
count++;
break;
}
}
return count;
}
Simplest code according to me....
ReplyDeletepublic int countClumps(int[] nums) {
int count = 0;
boolean chk=false;
for (int i = 0; i < nums.length; i++) {
if (i + 1 < nums.length) {
if (nums[i] == nums[i+1] && !chk ) {
count++;
chk=true;
}
else if(nums[i]!=nums[i+1]){
chk=false;
}
}
}
return count;
}
public int countClumps(int[] nums) {
ReplyDeleteint counter = 0;
for(int i = 0; i< nums.length;i++){
if(i < nums.length-1 && nums[i]==nums[i+1]){
if(i==0){
counter++;
} else {
if(nums[i-1]!=nums[i]){
counter++;
}
}
}
}
return counter;
}
Hello.
ReplyDeletepublic int countClumps(int[] nums) {
ReplyDeleteint count=0;
for(int i=0 ; i0 && (nums[i]==nums[i+1])&& (nums[i]!=nums[i-1]) ) { count++;}
}
return count;
}
public int countClumps(int[] nums) {
ReplyDeleteint count=0;
for(int i=0 ; i0 && (nums[i]==nums[i+1])&& (nums[i]!=nums[i-1]) ) { count++;}
}
return count;
}
public int countClumps(int[] nums) { int count=0; for(int i=0 ; i0 && (nums[i]==nums[i+1])&& (nums[i]!=nums[i-1]) ) { count++;} } return count; }
ReplyDeleteint count=0;
ReplyDeletefor(int i=0 ; i0 && (nums[i]==nums[i+1])&& (nums[i]!=nums[i-1]) ) { count++;}
} return count; }
public int countClumps(int[] nums) {
ReplyDeleteint len = 1;
int count = 0;
for (int i = 1; i < nums.length; i++) {
len = nums[i] == nums[i - 1] ? len + 1 : 1;
count = len == 2 ? count + 1 : count;
}
return count;
}
public int countClumps(int[] nums) {
ReplyDeleteint count = 0;
for (int i = 0; i < nums.length - 1; i++)
{
if (nums[i] == nums[i + 1])
{
i++;
count++;
while (i < nums.length - 1 && nums[i] == nums[i + 1])
{
i++;
}
}
}
return count;
}
public int countClumps(int[] nums) {
ReplyDeleteint count = 0;
int len = nums.length;
for(int i = 0; i<len-1; i++){
if(i==0 && nums[i]==nums[i+1]){
count++;
i++;
}
if(nums[i]==nums[i+1] && nums[i] != nums[i-1]){
count++;
i++;
}
}
return count;
}
int count = 1;
ReplyDeleteint k = 0;
for(int i = 0; i< nums.length-1; i++) {
if(nums[i] == nums[i+1]) {
k +=2;
} else {
if(k>=2) count++;
k = 0;
}
}
if(k == 0) return 0;
return count;
public int countClumps(int[] nums) {
ReplyDeleteint count=0;
boolean readytocount=false;
for(int i=0;i<nums.length-1;i++){
if( nums[i]==nums[i+1]){
if(readytocount || count==0){
count++;
readytocount=false;
}
}else{
readytocount=true;
}
}
return count;
}