Leetcode Algorithm

박영호·2021년 3월 29일

Today(03/29) Algorithm

1550. Three Consecutive Odds

Example 1:

Input: arr = [2,6,4,1]
Output: false
Explanation: There are no three consecutive odds.

code

 for(let i = 0; i < arr.length; i++) {
   if(arr[i] % 2 === 1 && 
      arr[i +1] % 2 === 1 && 
      arr[i +2 ] % 2 === 1 ){
     return true;
   }
  }
  return false;
};

520. Detect Capital

Example 1:

Input: "USA"
Output: True

Example 2:

Input: "FlaG"
Output: False

code

var detectCapitalUse = function(word) {
  let result = "";
  let words = word.split("");
 
  if(word === word.toUpperCase() ||
     word === word.toLowerCase()){
    return true;
  }
  
  if(word[0] === word.toUpperCase()[0]){
    for(let i=0; i < words.length; i++){
       if(words[i] === words[i].toUpperCase()){
        result = result + words[i];
      }     
    }
    return result.length >1 ? false : true;
  }
  return false;
};

1672. Richest Customer Wealth

Example 1:

Input: accounts = [[1,2,3],[3,2,1]]
Output: 6
Explanation:
1st customer has wealth = 1 + 2 + 3 = 6
2nd customer has wealth = 3 + 2 + 1 = 6
Both customers are considered the richest with a wealth of 6 each, so return 6.

Example 2:

Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
Explanation:
1st customer has wealth = 6
2nd customer has wealth = 10
3rd customer has wealth = 8
The 2nd customer is the richest with a wealth of 10.

Example 3:

Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
Output: 17

code

var maximumWealth = function(accounts) {
  const result = [];
  
    for (let i = 0; i <accounts.length; i++){
      result.push(accounts[i].reduce((a,b) => a + b));  
    }

  return Math.max(...result);
};

1470. Shuffle the Array

Example 1:

Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].

Example 2:

Input: nums = [1,2,3,4,4,3,2,1], n = 4
Output: [1,4,2,3,3,2,4,1]

Example 3:

Input: nums = [1,1,2,2], n = 2
Output: [1,2,1,2]

code

var shuffle = function(nums, n) {
  let result = [];
  let x =nums.slice(0, n);
  let y = nums.slice(n);

  for(let i = 0; i < n; i++){
    result.push(x[i], y[i]);
  }
  return result;
};
profile
무언가에 호기심이 생기면 적극적이고 재밌게 그걸 해결해내고 싶어하는 프론트 엔드 개발자 입니다 .

0개의 댓글