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 | 31 |
Tags
- 나쫌
- 버즈2프로
- 메일우유
- 뜨아거
- DIY
- javascript
- 미앤아이
- 3d퍼즐
- 코딩테스트
- 맛집
- 송리단
- 하스스톤
- 노노그램
- 서울제빵소
- 잠실새내
- 발더스게이트
- 눈알빠지겠네
- 발더스3
- 밥먹고
- 메탈퍼즐
- 코테
- 게임
- 바질토마토뭐시기
- LeetCode
- 알고리즘테스트
- 취미
- 천등
- 발더스모드
- 누룽지소금빵
- 토이프로젝트
Archives
- Today
- Total
.Zzumbong
[leetCode/JS] 446. Arithmetic Slices II - Subsequence 본문
문제 설명
Given an integer array nums
, return the number of all the arithmetic subsequences of nums
.
A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
- For example,
[1, 3, 5, 7, 9]
,[7, 7, 7, 7]
, and[3, -1, -5, -9]
are arithmetic sequences. - For example,
[1, 1, 2, 5, 7]
is not an arithmetic sequence.
A subsequence of an array is a sequence that can be formed by removing some elements (possibly none) of the array.
- For example,
[2,5,10]
is a subsequence of[1,2,1,2,4,1,5,10]
.
The test cases are generated so that the answer fits in 32-bit integer.
입출력 예
Example 1:
Input: nums = [2,4,6,8,10]
Output: 7
Explanation: All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]
Example 2:
Input: nums = [7,7,7,7,7]
Output: 16
Explanation: Any subsequence of this array is arithmetic.
Constraints
1 <= nums.length <= 1000
-231 <= nums[i] <= 231 - 1
내 솔루션
/**
* @param {number[]} nums
* @return {number}
*/
var numberOfArithmeticSlices = function(nums) {
const cache = Array.from(Array(nums.length), () => new Map())
let answer = 0;
for(let i = 0; i < nums.length; i++) {
for(let j = 0; j < i; j++) {
let cache_i = cache[i].get(nums[i]-nums[j]) || 0;
let cache_j = cache[j].get(nums[i]-nums[j]) || 0;
cache[i].set(nums[i]-nums[j], cache_i + cache_j + 1)
answer += cache_j;
}
}
return answer
};
--------------
nums[1] - nums[0] 4 2 2
cache_i: 0, cache_j: 0, i+j+1: 1
--------------
nums[2] - nums[0] 6 2 4
cache_i: 0, cache_j: 0, i+j+1: 1
nums[2] - nums[1] 6 4 2
cache_i: 0, cache_j: 1, i+j+1: 2
--------------
nums[3] - nums[0] 8 2 6
cache_i: 0, cache_j: 0, i+j+1: 1
nums[3] - nums[1] 8 4 4
cache_i: 0, cache_j: 0, i+j+1: 1
nums[3] - nums[2] 8 6 2
cache_i: 0, cache_j: 2, i+j+1: 3
--------------
nums[4] - nums[0] 10 2 8
cache_i: 0, cache_j: 0, i+j+1: 1
nums[4] - nums[1] 10 4 6
cache_i: 0, cache_j: 0, i+j+1: 1
nums[4] - nums[2] 10 6 4
cache_i: 0, cache_j: 1, i+j+1: 2
nums[4] - nums[3] 10 8 2
cache_i: 0, cache_j: 3, i+j+1: 4
--------------
// cache
[
Map(0) {},
Map(1) { 2 => 1 },
Map(2) { 4 => 1, 2 => 2 },
Map(3) { 6 => 1, 4 => 1, 2 => 3 },
Map(4) { 8 => 1, 6 => 1, 4 => 2, 2 => 4 }
]
감상평
- 아니 너무 한거 아니요?
- 일요일 한시간 그냥 날렸네
'coding test > leetCode' 카테고리의 다른 글
[leetCode/JS] 380. Insert Delete GetRandom O(1) (0) | 2022.11.29 |
---|---|
[leetCode/JS] 2225. Find Players With Zero or One Losses (0) | 2022.11.28 |
[leetCocd/JS] 907. Sum of Subarray Minimums (0) | 2022.11.25 |
[leetCode/JS] 79. Word Search (0) | 2022.11.24 |
[leetCode/JS] 784. Letter Case Permutation (0) | 2022.11.24 |
Comments