It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.

Apparently, many engineers take measures on the site called LetCode.

It is a site that trains the algorithmic power that can withstand the coding test that is being done in the early story, and it is an inevitable path for those who want to build a career at an overseas tech company.

I wrote it in a big way, but I have no plans to have such an interview at the moment.

However, as an IT engineer, it would be better to have the same level of algorithm power as a person, so I would like to solve the problem irregularly and write down the method I thought at that time as a memo.

I'm solving it with Python3.

Leet Code Table of Contents Starting from Zero

Last time Leet Code Day86 "33. Search in Rotated Sorted Array" starting from zero

Twitter I'm doing it.

** Technical Blog Started! !! ** ** I think the technology will write about LetCode, Django, Nuxt, and so on. ** This is faster to update **, so please bookmark it!

1512. Number of Good Pairs The difficulty level is Easy.

The problem is given an array of integers `nums`

.

If `nums [i] == nums [j]`

and ʻi <j`, enable pairs`

(i, j) `and design an algorithm that returns the number of valid pairs.

Example 1:

Input: nums = [1,2,3,1,1,3] Output: 4 Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.

Example 2:

Input: nums = [1,1,1,1] Output: 6 Explanation: Each pair in the array are good.

Example 3:

Input: nums = [1,2,3] Output: 0

```
class Solution:
def numIdenticalPairs(self, nums: List[int]) -> int:
ans,dic = 0,{}
for i,j in enumerate(nums):
if j in dic:
ans += dic[j]
dic[j] += 1
else:
dic[j] = 1
return ans
# Runtime: 32 ms, faster than 100.00% of Python3 online submissions for Number of Good Pairs.
# Memory Usage: 13.6 MB, less than 100.00% of Python3 online submissions for Number of Good Pairs.
```

Manage your elements with `dic`

and you're done!
I think it was a pretty good one in terms of speed and capacity (self-proclaimed).
I wrote it in a hurry, so I can't go into a deep explanation, but I think you can also use `Counter`

or two` pointer`

s.
If you want the answer, please post it on disucuss.

So that's it for this time. Thank you for your hard work.

Recommended Posts