엔지니어 게시판
LeetCode 솔루션 분류

923. 3Sum With Multiplicity

컨텐츠 정보

본문

[ LeetCode 시즌 3 ] 2022년 4월 6일 문제 입니다.

[Medium] 923. 3Sum With Multiplicity

Given an integer array arr, and an integer target, return the number of tuples i, j, k such that i < j < k and arr[i] + arr[j] + arr[k] == target.

As the answer can be very large, return it modulo 109 + 7.

 

Example 1:

Input: arr = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Explanation: 
Enumerating by the values (arr[i], arr[j], arr[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.

Example 2:

Input: arr = [1,1,2,2,2,2], target = 5
Output: 12
Explanation: 
arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.

 

Constraints:

  • 3 <= arr.length <= 3000
  • 0 <= arr[i] <= 100
  • 0 <= target <= 300

관련자료

댓글 1

bobkim님의 댓글

  • 익명
  • 작성일
class Solution {
public:
    int threeSumMulti(vector<int>& arr, int target) {
        int vectorSize=arr.size();
        int mod=1e9+7;
        unsigned long int counter=0;

        std::map<int,unsigned long int> m;
        std::map<int,unsigned long int> sum;
        
        for(int i=0;i<vectorSize;i++){
            m[arr[i]]++;
        };
        
        for(std::map<int,unsigned long int>::iterator itr1=m.begin();itr1!=m.end();itr1++){
            for(std::map<int,unsigned long int>::iterator itr2=itr1;itr2!=m.end();itr2++){
                for(std::map<int,unsigned long int>::iterator itr3=itr2;itr3!=m.end();itr3++){
                    if((itr1->first+itr2->first+itr3->first)==target){
                        if(itr1->first != itr2->first){
                            if(itr2->first != itr3->first){
                                //std::cout<<"case1"<<std::endl;
                            counter=counter+(itr1->second * itr2->second * itr3->second);
                            }else{
                                if(itr2->second >= 2){
                                    //std::cout<<"case2"<<std::endl;
                                    counter=counter+(itr1->second * (itr2->second * (itr2->second-1))/2);
                                };
                            };
                        }else{
                            if(itr2->first != itr3->first){
                                if(itr1->second >= 2){
                                    //std::cout<<"case3"<<std::endl;
                                    counter=counter+((itr1->second * (itr1->second-1))/2 * itr3->second);
                                };
                            }else{
                                if(itr1->second >= 3){
                                    //std::cout<<"case4"<<std::endl;
                                    counter=counter+(((itr1->second)*(itr1->second-1) * (itr1->second-2) / 6 ))%mod;
                                };
                            };
                        };
                    };
                };
            };
        };

        return counter%mod;
    }
};
전체 29 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 592 명
  • 오늘 방문자 1,626 명
  • 어제 방문자 7,414 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,543 명
알림 0