본문 바로가기

알고리즘 문제 풀이207

1431. Kids With the Greatest Number of Candies candies 각 배열의 요소에 extraCandy를 더했을 때 그 값이 candies 수 중의 최대 값이 되면 true 아니면 false입니다. [처음 푼 방법] ''' java import java.util.ArrayList; import java.util.List; class Solution { public static List kidsWithCandies(int[] candies, int extraCandies) { List answer = new ArrayList(candies.length); int maxCandyCnt = 0; for (int candy : candies) { if (candy > maxCandyCnt) { maxCandyCnt = candy; } } f.. 2021. 6. 10.
1827. Minimum Operations to Make the Array Increasing 배열의 각 숫자를 1씩 더할 수 있을 때, 배열이 오름차가 되기 위해선 몇 번 1을 더해야 하는지 구하시오. [처음 푼 코드] class Solution { public int minOperations(int[] nums) { int answer = 0; if (nums.length = nums[secondPointer]) { int temp = nums[firstPointer] - nums[secondPointer] + 1; nums[secondPointer] += temp; answer += temp; } } return answer; } } 그냥 투포인터를 사용해서 앞에 요소랑 뒤에 요소를 비교하는 방법을 사용했습니다. 배열의 앞쪽의 값이 뒷쪽의 값보다 크거나 같을 경우 그 차만큼 뒤에 요소에 값을.. 2021. 6. 8.
(아직안끝남)1351. Count Negative Numbers in a Sorted Matrix 이중 배열에서 0보다 작은 수들의 갯수를 구하면 됩니다. 문제에서 내림차순으로 숫자가 감소하기 때문에 처음 -가 나오면 나머지는 전부 -입니다. class Solution { public int countNegatives(int[][] grid) { int answer = 0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[i].length; j++) { if (grid[i][j] < 0) { answer += grid[i].length - j; } } } return answer; } }해설 중에 이진탐색 사용한 거 있어서 그거 한번 짜보기로함. 2021. 6. 2.
1528. Shuffle String 문제출처 [처음 짠 코드] 처음 생각한 코드는 인덱스는 중복이 안 되고 문자와 세트이므로 map으로 넣은 후에 key를 정렬해서 받아오는 방법을 생각함. import java.util.HashMap; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; class Solution { public String restoreString(String s, int[] indices) { Map map = new HashMap(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < indices.length; i++) { map.put(indices[i], s.charAt(i)).. 2021. 5. 25.
1281. Subtract the Product and Sum of Digits of an Integer 숫자 각 자릿수에 곱과 합을 빼서 리턴해주는 문제. 10으로 나눈 나머지가 1의 자릿수의 값이 됨. 0이 될 때까지 계속 반복하면 모든 자릿수를 거쳐갈 수 있음. [내가 짠 코드] class Solution { public int subtractProductAndSum(int n) { int number = n; int multilSum = 1; int sum = 0; while (number > 0) { int temp = number % 10; sum += temp; multilSum *= temp; number /= 10; } return multilSum- sum; } } 2021. 5. 25.
1561. Maximum Number of Coins You Can Get 숫자가 적혀있는 동전을 3개씩 뽑아서 숫자의 크기가 제일 큰 애는 Alice한테 주고 Bob한테는 제일 작은 값 주고, 가운데 값은 자기가 가진다. 동전이 없어질 때까지 반복한다. 자기가 가진 동전의 숫자의 합의 최댓값은? 문제를 보고 제일 먼저 생각난 건 3n이면 반복할 횟수는 n번. 뽑을 때마다 최댓값을 가진 동전 다음으로 큰 동전을 뽑아야함. 1,2,2,4,7,8 x,x,o,x,o,x 2,4,5 x,o,x 1,2,3,4,5,6,7,8,9 x,x,x,o,x,o,x,o,x import java.util.Arrays; class Solution { //1,2,2,4,7,8 //x,x,o,x,o,x //2,4,5 //x,o,x //1,2,3,4,5,6,7,8,9 //x,x,x,o,x,o,x,o,x publ.. 2021. 5. 13.