Skip to content

Morzeta #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions easy/1/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "bits/stdc++.h"
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cassert>

using namespace std;


class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int residue;
vector<int> output;
std::vector<int>::iterator pos;
for (int i = 0; i < nums.size(); i++) {
residue = target - nums[i];
pos = std::find(nums.begin() + i + 1, nums.end(), residue);
if (pos != nums.begin() && pos != nums.end()) {
output.push_back(i);
output.push_back(pos - nums.begin());
return output;
}
}
return output;
}
};
45 changes: 45 additions & 0 deletions easy/1128/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## 1128. Number of Equivalent Domino Pairs


Given a list of `dominoes`, `dominoes[i] = [a, b]` is **equivalent to** `dominoes[j] = [c, d]` if and only if either (`a == c` and `b == d`), or (`a == d` and `b == c`) - that is, one domino can be rotated to be equal to another domino.


Return *the number of pairs* `(i, j)` *for which* `0 <= i < j < dominoes.length`*, and* `dominoes[i]` *is **equivalent to*** `dominoes[j]`.





**Example 1:**



```

**Input:** dominoes = [[1,2],[2,1],[3,4],[5,6]]
**Output:** 1

```

**Example 2:**



```

**Input:** dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]]
**Output:** 3

```




**Constraints:**


* `1 <= dominoes.length <= 4 * 104`
* `dominoes[i].length == 2`
* `1 <= dominoes[i][j] <= 9`


34 changes: 34 additions & 0 deletions easy/1128/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from testing.solution_test import BaseSolutionTest
from typing import List

def check_equivalent(domino_1: List[int], domino_2: List[int]) -> bool:
if (domino_1[0] == domino_2[0] and domino_1[1] == domino_2[1]) or (domino_1[0] == domino_2[1] and domino_1[1] == domino_2[0]):
return True
else:
return False

def domino2str(domino: List[int]):
return str(min(domino[0], domino[1])) + "_" + str(max(domino[0], domino[1]))


class Solution:
def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
count = 0
seen = {domino2str(dominoes[0]): [dominoes[0]]}

for d in dominoes[1:]:
if domino2str(d) in seen.keys():
count += len(seen[domino2str(d)])
seen[domino2str(d)].append(d)
else:
seen[domino2str(d)] = [d]

return count

class TestableSolution(Solution):
def __init__(self):
super().__init__()
self.main = self.numEquivDominoPairs


BaseSolutionTest(TestableSolution, )
1 change: 1 addition & 0 deletions easy/1128/test_cases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"question_1": {"input": {"dominoes": [[1, 2], [2, 1], [3, 4], [5, 6]]}, "output": 1}, "question_2": {"input": {"dominoes": [[1, 2], [1, 2], [1, 1], [1, 2], [2, 2]]}, "output": 3}}
75 changes: 75 additions & 0 deletions easy/175/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

## 175. Combine Two Tables

Table: `Person`

```

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| personId | int |
| lastName | varchar |
| firstName | varchar |
+-------------+---------+
personId is the primary key (column with unique values) for this table.
This table contains information about the ID of some persons and their first and last names.

```

Table: `Address`

```

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| addressId | int |
| personId | int |
| city | varchar |
| state | varchar |
+-------------+---------+
addressId is the primary key (column with unique values) for this table.
Each row of this table contains information about the city and state of one person with ID = PersonId.

```

Write a solution to report the first name, last name, city, and state of each person in the `Person` table. If the address of a `personId` is not present in the `Address` table, report `null` instead.

Return the result table in **any order**.

The result format is in the following example.

**Example 1:**

```

Input:
Person table:
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+
Address table:
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
+-----------+----------+---------------+------------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California |
+-----------+----------+---------------+------------+
Output:
+-----------+----------+---------------+----------+
| firstName | lastName | city | state |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+
Explanation:
There is no address in the address table for the personId = 1 so we return null in their city and state.
addressId = 1 contains information about the address of personId = 2.

```

['database']
20 changes: 20 additions & 0 deletions easy/175/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from testing.solution_test import BaseSolutionTest


import pandas as pd

class Solution:
def combine_two_tables(self, person: pd.DataFrame, address: pd.DataFrame) -> pd.DataFrame:
result = person.merge(address, on="personId", how="outer")
result = result.drop(columns=["personId", "addressId"]).dropna(subset=["lastName"])

return result


class TestableSolution(Solution):
def __init__(self):
super().__init__()
self.main = self.combine_two_tables


BaseSolutionTest(TestableSolution, )
1 change: 1 addition & 0 deletions easy/175/test_cases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"question_1":{"input":{"person":"{\"personId\":{\"0\":1,\"1\":2},\"lastName\":{\"0\":\"Wang\",\"1\":\"Alice\"},\"firstName\":{\"0\":\"Allen\",\"1\":\"Bob\"}}","address":"{\"addressId\":{\"0\":1,\"1\":2},\"personId\":{\"0\":2,\"1\":3},\"city\":{\"0\":\"New York City\",\"1\":\"Leetcode\"},\"state\":{\"0\":\"New York\",\"1\":\"California\"}}"},"output":"{\"firstName\":{\"0\":\"Allen\",\"1\":\"Bob\"},\"lastName\":{\"0\":\"Wang\",\"1\":\"Alice\"},\"city\":{\"0\":null,\"1\":\"New York City\"},\"state\":{\"0\":null,\"1\":\"New York\"}}"}}
81 changes: 81 additions & 0 deletions easy/27/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
## 27. Remove Element


Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm). The order of the elements may be changed. Then return *the number of elements in* `nums` *which are not equal to* `val`.


Consider the number of elements in `nums` which are not equal to `val` be `k`, to get accepted, you need to do the following things:


* Change the array `nums` such that the first `k` elements of `nums` contain the elements which are not equal to `val`. The remaining elements of `nums` are not important as well as the size of `nums`.
* Return `k`.


**Custom Judge:**


The judge will test your solution with the following code:



```

int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
// It is sorted with no values equaling val.

int k = removeElement(nums, val); // Calls your implementation

assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i < actualLength; i++) {
assert nums[i] == expectedNums[i];
}

```

If all assertions pass, then your solution will be **accepted**.





**Example 1:**



```

**Input:** nums = [3,2,2,3], val = 3
**Output:** 2, nums = [2,2,_,_]
**Explanation:** Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

```

**Example 2:**



```

**Input:** nums = [0,1,2,2,3,0,4,2], val = 2
**Output:** 5, nums = [0,1,4,0,3,_,_,_]
**Explanation:** Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).

```




**Constraints:**


* `0 <= nums.length <= 100`
* `0 <= nums[i] <= 50`
* `0 <= val <= 100`


18 changes: 18 additions & 0 deletions easy/27/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import List
from testing.solution_test import BaseSolutionTest


class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
for i in range(nums.count(val)):
nums.pop(nums.index(val))
return len(nums),


class TestableSolution(Solution):
def __init__(self):
super().__init__()
self.main = self.removeElement


BaseSolutionTest(TestableSolution, )
1 change: 1 addition & 0 deletions easy/27/test_cases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"question_1": {"input": {"nums": [3, 2, 2, 3], "val": 3}, "output": "2, nums = [2,2,_,_]"}, "question_2": {"input": {"nums": [0, 1, 2, 2, 3, 0, 4, 2], "val": 2}, "output": "5, nums = [0,1,4,0,3,_,_,_]"}}
73 changes: 73 additions & 0 deletions easy/2894/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## 2894. Divisible and Non-divisible Sums Difference


You are given positive integers `n` and `m`.


Define two integers as follows:


* `num1`: The sum of all integers in the range `[1, n]` (both **inclusive**) that are **not divisible** by `m`.
* `num2`: The sum of all integers in the range `[1, n]` (both **inclusive**) that are **divisible** by `m`.


Return *the integer* `num1 - num2`.





**Example 1:**



```

**Input:** n = 10, m = 3
**Output:** 19
**Explanation:** In the given example:
- Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37.
- Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18.
We return 37 - 18 = 19 as the answer.

```

**Example 2:**



```

**Input:** n = 5, m = 6
**Output:** 15
**Explanation:** In the given example:
- Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15.
- Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0.
We return 15 - 0 = 15 as the answer.

```

**Example 3:**



```

**Input:** n = 5, m = 1
**Output:** -15
**Explanation:** In the given example:
- Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0.
- Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15.
We return 0 - 15 = -15 as the answer.

```




**Constraints:**


* `1 <= n, m <= 1000`


Loading