짬뽕얼큰하게의 맨땅에 헤딩 :: '2025/05/06 글 목록

'2025/05/06'에 해당되는 글 1건

Leetcode Problem:

Summary

  • Given a zero-based permutation, build an array where ans[i] = nums[nums[i]] for each 0 <= i < nums.length and return it.

Approach

  • This solution uses a simple iterative approach where it iterates over the input array and uses the value at each index as the index for the next value in the output array.

Complexity

  • O(n) where n is the length of the input array

Explanation

  • The solution uses a vector to store the output array and iterates over the input array
  • For each index in the input array, it uses the value at that index as the index for the next value in the output array
  • This approach is simple and efficient, with a time complexity of O(n) where n is the length of the input array.

Solution Code:


class Solution {
public:
    vector ans;
    vector buildArray(vector& nums) {
        for(int i = 0 ; i < nums.size(); i++){
            ans.push_back(nums[nums[i]]);
        }
        return ans;
    }
};

'알고리즘' 카테고리의 다른 글

Find Minimum Time to Reach Last Room II  (1) 2025.05.08
Find Minimum Time to Reach Last Room I  (0) 2025.05.07
Domino and Tromino Tiling  (1) 2025.05.05
Number of Equivalent Domino Pairs  (0) 2025.05.04
Minimum Domino Rotations For Equal Row  (1) 2025.05.03
블로그 이미지

짬뽕얼큰하게

,