Skip to main content

Find Target Indices After Sorting Array

Find Target Indices After Sorting Array

 You are given a 0-indexed integer array nums and a target element target.


A target index is an index i such that nums[i] == target.


Return a list of the target indices of nums after sorting nums in non-decreasing order. If there are no target indices, return an empty list. The returned list must be sorted in increasing order.


Example 1:


Input: nums = [1,2,5,2,3], target = 2

Output: [1,2]

Explanation: After sorting, nums is [1,2,2,3,5].

The indices where nums[i] == 2 are 1 and 2.

Example 2:


Input: nums = [1,2,5,2,3], target = 3

Output: [3]

Explanation: After sorting, nums is [1,2,2,3,5].

The index where nums[i] == 3 is 3.

Example 3:


Input: nums = [1,2,5,2,3], target = 5

Output: [4]

Explanation: After sorting, nums is [1,2,2,3,5].

The index where nums[i] == 5 is 4.

Example 4:


Input: nums = [1,2,5,2,3], target = 4

Output: []

Explanation: There are no elements in nums with value 4.

 


Constraints:


1 <= nums.length <= 100

1 <= nums[i], target <= 100


Solutions:


C++ :


class Solution {
public:
    vector<int> targetIndices(vector<int>& A, int target) {
        sort(begin(A), end(A));
        vector<int> ans;
        for (int i = 0; i < A.size(); ++i) {
            if (A[i] == target) ans.push_back(i);
        }
        return ans;
    } 

}; 

class Solution {
public:
    vector<int> targetIndices(vector<int>& A, int target) {
        int cnt = 0, rank = 0; // `cnt` is the frequency of `target`, `rank` is the sum of frequencies of all numbers < target
        for (int n : A) {
            cnt += n == target;
            rank += n < target;
        }
        vector<int> ans;
        while (cnt--) ans.push_back(rank++);
        return ans;
    }
};


Python


def targetIndices(self, nums: List[int], target: int) -> List[int]:
    nums.sort()
    res = []
    for i, char in enumerate(nums):
        if char == target:
            res.append(i)
    return res

Comments

Popular posts from this blog

May-6 2020 Challenge

  6.   Majority Element Given an array of size  n , find the majority element. The majority element is the element that appears  more than   ⌊ n/2 ⌋  times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: Input: [3,2,3] Output: 3 Example 2: Input: [2,2,1,1,1,2,2] Output: 2 Solution in Java  class Solution {     public int majorityElement(int[] num) {         int m = num[0], cnt= 1;     for (int i = 1; i < num.length; i++) {         if (cnt == 0) {             m= num[i];             cnt = 1;         } else if (num[i] == m) {             cnt++;         } else              cnt--;    }      return m;...

Leetcode 190. Reverse Bits. Python. Bit Manipulation

  190 .  Reverse Bits Reverse bits of a given 32 bits unsigned integer. Note: Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using  2's complement notation . Therefore, in  Example 2  above, the input represents the signed integer  -3  and the output represents the signed integer  -1073741825 .   Example 1: Input: n = 00000010100101000001111010011100 Output: 964176192 (00111001011110000010100101000000) Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000 . Example 2: Input: n = 11111111111111111111...

Leetcode 58. Meeting Rooms II. Python (Easiest Explaination)

  58.  Meeting Rooms II Description Given an array of meeting time intervals consisting of start and end times  [[s1,e1],[s2,e2],...] (si < ei) , find the minimum number of conference rooms required.) (0,8),(8,10) is not conflict at 8 Example Example1 Input : intervals = [(0,30),(5,10),(15,20)] Output : 2 Explanation : We need two meeting rooms room1 : ( 0 , 30 ) room2 : ( 5 , 10 ),( 15 , 20 ) Example2 Input: intervals = [( 2 , 7 )] Output: 1 Explanation: Only need one meeting room Solution : """ Definition of Interval: class Interval(object):     def __init__(self, start, end):         self.start = start         self.end = end """ class Solution:     """     @param intervals: an array of meeting time intervals     @return: the minimum number of conference rooms required     """     def min_meeting_rooms( self , intervals: List[Interval]) -> int :   ...