본문 바로가기

코딩 테스트/leetcode23

Count Pairs Whose Sum is Less than Target 첫번째 코드 인덱스 순서대로 더한 후에 target보다 작은 경우 answer에 1씩 더하는 방법으로 결과를 내면 된다! class Solution:     def countPairs(self, nums: List[int], target: int) -> int:         answer = 0         for i in range(len(nums)):             for j in nums[i+1:]:                 if nums[i] + j                     answer += 1         return answer 통과 :)링크https://github.com/ornni/leetcode/tree/main/2824-count-pairs-whose-sum-i.. 2024. 6. 29.
Neither Minimum nor Maximum 첫번쨰 코드 가장 큰 값, 가장 작은 값이 아니면 그냥 return 해버리고,다 돌았는데 아무것도 이루어지지 않으면 -1을 return 해버리면 된다!! class Solution:     def findNonMinOrMax(self, nums: List[int]) -> int:         Max = max(nums)         Min = min(nums)         for i in nums:             if i != Max and i != Min:                 return i                          return -1  통과!링크https://github.com/ornni/leetcode/tree/main/2733-neither-minimum-n.. 2024. 6. 28.
Final Prices With a Special Discount in a Shop 첫번째 코드 인덱스를 통해서 다음 값 중 작은 값이 있다면 현재 값에서 빼고그렇지 않으면 그냥 현재 값을 정답 리스트에 추가한다. class Solution:     def finalPrices(self, prices: List[int]) -> List[int]:         answer = []         output = 0         for i in range(len(prices)):             output = prices[i]             for j in range(i+1, len(prices)):                 if prices[i] >= prices[j]:                     output = prices[i] - prices[j]    .. 2024. 6. 27.
Removing Stars From a String 첫번째 코드 스택을 이용하면 되겠다는 생각이 들었다.스택은 나중에 들어온 값이 가장 먼저 나가므로*이 나오면 가장 나중에 들어온 값을 제거하는 방법으로 진행하면 되겠구나! class Solution:     def removeStars(self, s: str) -> str:         answer = []         for i in s:             if i == '*':                 answer.pop()             else:                 answer.append(i)         return ''.join(answer)          통과링크https://github.com/ornni/leetcode/tree/main/2390-removi.. 2024. 6. 26.
Shuffle the Array 첫번째 코드 시작 인덱스에 대해서 이해하고 있으면 순서대로 넣으면 된다! class Solution:     def shuffle(self, nums: List[int], n: int) -> List[int]:         index1 = 0         index2 = n         answer = []                  for i in range(len(nums) // 2):             answer.append(nums[index1])             answer.append(nums[index2])             index1 += 1             index2 += 1                  return answer 통과링크https://gi.. 2024. 6. 25.
Divisor Game 첫번째 코드 생각해보면 짝수인 경우는 Alice의 승리, 홀수인 경우에는 Bob의 승리이다. class Solution:     def divisorGame(self, n: int) -> bool:         if n % 2 == 0:             return True         else:             return False 통과!링크https://github.com/ornni/leetcode/tree/main/1025-divisor-game leetcode/1025-divisor-game at main · ornni/leetcodeCollection of LeetCode questions to ace the coding interview! - Created using [Leet.. 2024. 6. 24.
728x90