Post

leetcode(리트코드)3월23일 challenge923-3Sum With Multiplicity

leetcode March 23일 - 3Sum With Multiplicity 문제입니다.

1. 문제

https://leetcode.com/problems/3sum-with-multiplicity/


2. Input , Output


3. 분류 및 난이도

Medium 난이도입니다.
3월 23일자 챌린지 문제입니다.


4. 문제 해석

  • 숫자 세개를 더해서 target을 만들 수 있는 경우의 수를 모두 찾아 10^+7로 나눈 나머지를 리턴합니다.

5. code

c++

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
32
class Solution {
public:
    int threeSumMulti(vector<int>& arr, int target) {
        unordered_map<int,long long> um;
        for(size_t i = 0;i<arr.size();++i)
            um[arr[i]]++;
        
        long long result=0;
        
        for(auto it : um)
        {
            for(auto it2: um)
            {
                int first =it.first;
                int second = it2.first;
                int third = target-first-second;
                if(!um.count(third))continue;
                
                if(first == second && second== third)
                    result += um[first]* (um[first]-1) * (um[first]-2) / 6;
                else if(first==second && second!=third)
                    result += (um[first] * (um[first]-1))/2 * um[third];
                else if(first<second && second<third)
                    result += um[first] * um[second] * um[third];
                
            }
        }
        return (result %(int)(1e9+7));
    }
};



6. 결과 및 후기, 개선점

c++ 84%

This post is licensed under CC BY 4.0 by the author.