Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Data Structure
- array
- string
- implement
- 자바
- greedy
- Method
- Stack
- hash table
- Counting
- bit manipulation
- geometry
- java
- Tree
- 코테
- 구현
- two pointers
- Binary Tree
- dynamic programming
- 파이썬
- simulation
- 코딩테스트
- Number Theory
- Class
- Binary Search
- Math
- Matrix
- database
- SQL
- sorting
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2614. Prime In Diagonal 본문
1. Input
1) int[][] nums
2. Output
1) nums의 두 대각선 위에 있는 소수 중 가장 큰 것을 반환
- 소수: 약수의 개수가 2개인 자연수
2) 대각선에 소수가 없다면 0을 반환
3. Constraint
1) 1 <= nums.length <= 300
2) nums.length == nums[i[.length
3) 1 <= nums[i][j] <= 4*10^6
4) 대각선은 nums[i][i]나 nums[i][nums.length - i - 1] 형태로 나타낼 수 있다.
4. Example
Input: nums = [[1,2,3],[5,6,7],[9,10,11]] -> Output: 11
설명: 대각선 위의 수는 1,3,6,9,11이고, 이 중 가장 큰 소수는 11이다.
5. Code
1) 첫 코드(2023/05/08)
class Solution {
public int diagonalPrime(int[][] nums) {
int answer = 0;
for(int i=0 ; i<nums.length ; i++){
if(isPrime(nums[i][i]))
answer = Math.max(answer, nums[i][i]);
if(isPrime(nums[i][nums[i].length-1-i]))
answer = Math.max(answer, nums[i][nums[i].length-1-i]);
}
return answer;
}
static boolean isPrime(int n){
boolean answer = n>1;
for(int i=2 ; i<=Math.sqrt(n) && answer ; i++)
if(n%i==0)
answer = false;
return answer;
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2643. Row With Maximum Ones (0) | 2023.05.08 |
---|---|
[LeetCode/Easy] 2639. Find the Width of Columns of a Grid (0) | 2023.05.08 |
[LeetCode/Easy] 2605. Form Smallest Number From Two Digit Arrays (0) | 2023.05.06 |
[LeetCode/Easy] 2582. Pass the Pillow (0) | 2023.05.06 |
[LeetCode/Easy] 2570. Merge Two 2D Arrays by Summing Values (0) | 2023.05.06 |