Skip to content

Commit ce03e21

Browse files
authored
feature/march leetcode challenge (#1)
* testing code coverage part 3 * fix yml file and git-ignore
1 parent 2fe251b commit ce03e21

File tree

15 files changed

+4939
-0
lines changed

15 files changed

+4939
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
node_modules
2+
3+
# env tokens for code-cov
4+
codecov.yml
5+
6+
# dotenv environment variables file
7+
.env
8+
9+
# Coverage directory used by tools like istanbul
10+
coverage

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- node
4+
env:
5+
- CODECOV_TOKEN='565c9b90-78cd-4ead-9a89-aaddae5adbf6'
6+
after_success:
7+
- bash <(curl -s https://codecov.io/bash)
8+
script:
9+
- jest --coverage && codecov

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
# leetcode
2+
3+
[![Build Status](https://travis-ci.com/Mvrs/leetcode.svg?token=Ek5Qzx5pczABPWP2y28e&branch=feature/march-leetcode-challenge)](https://travis-ci.com/Mvrs/leetcode)
4+
[![codecov](https://codecov.io/gh/Mvrs/leetcode/branch/main/graph/badge.svg?token=G1R16KIQTF)](https://codecov.io/gh/Mvrs/leetcode)
5+
6+
My Solutions for [LeetCode problems](https://leetcode.com/problemset/all/)

jest.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
// An array of glob patterns indicating a set of files for which coverage information should be collected
3+
collectCoverageFrom: ['problems/**/*.js'],
4+
5+
// The directory where Jest should output its coverage files
6+
coverageDirectory: 'coverage',
7+
8+
// The test environment that will be used for testing
9+
testEnvironment: 'node',
10+
}

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"devDependencies": {
3+
"@types/jest": "^26.0.20",
4+
"codecov": "^3.8.1",
5+
"eslint": "^7.22.0",
6+
"eslint-config-standard": "^16.0.2",
7+
"eslint-plugin-import": "^2.22.1",
8+
"eslint-plugin-jest": "^24.2.1",
9+
"eslint-plugin-node": "^11.1.0",
10+
"eslint-plugin-promise": "^4.3.1",
11+
"eslint-plugin-standard": "^5.0.0",
12+
"jest": "^26.6.3"
13+
},
14+
"name": "leetcode",
15+
"description": "My solutions for Leetcode problems",
16+
"version": "1.0.0",
17+
"main": "index.js",
18+
"scripts": {
19+
"test": "jest",
20+
"test:watch": "jest --watchAll",
21+
"test:coverage": "jest --coverage"
22+
},
23+
"repository": {
24+
"type": "git",
25+
"url": "git+https://github.com/Mvrs/leetcode.git"
26+
},
27+
"keywords": [
28+
"leetcode"
29+
],
30+
"author": "Marlon Johnson",
31+
"license": "MIT",
32+
"bugs": {
33+
"url": "https://github.com/Mvrs/leetcode/issues"
34+
},
35+
"homepage": "https://github.com/Mvrs/leetcode#readme"
36+
}

problems/missing-number/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Missing Numbers
2+
3+
Leetcode: [#268](https://leetcode.com/problems/missing-number/)
4+
5+
Difficulty: Easy
6+
7+
Topic: `Arrays` `Math` `Bit Manipulation`
8+
9+
## Problem
10+
11+
Given an array `nums` containing `n` distanct numbers in the range `[0, n]`, return the only number in the range that is missing from _the array_
12+
13+
**Follow up:** Could you implement a solution using only `O(1)` extra space complexity and `O(n)` runtime complexity?
14+
15+
Example 1:
16+
17+
```
18+
Input: nums = [3,0,1]
19+
Output: 2
20+
Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
21+
```
22+
23+
Example 2:
24+
25+
```
26+
Input: nums = [0,1]
27+
Output: 2
28+
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
29+
```
30+
31+
Example 3:
32+
33+
```
34+
Input: nums = [0,1]
35+
Output: 2
36+
Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
37+
```
38+
39+
Example 4:
40+
41+
```
42+
Input: nums = [0]
43+
Output: 1
44+
Explanation: n = 1 since there is 1 number, so all numbers are in the range [0,1]. 1 is the missing number in the range since it does not appear in nums.
45+
```
46+
47+
Constraints:
48+
49+
- `n == nums.length`
50+
- `1 <=n <= 10^4`
51+
- `0 <= nums[i] <= n`
52+
- All the numbers of `nums` are **unique**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var missingNumber = function (nums) {
6+
const N = nums.length
7+
let total = 0
8+
9+
for (let i = 0; i < N; i++) {
10+
total += nums[i]
11+
}
12+
13+
return (N * (N + 1)) / 2 - total
14+
}
15+
16+
module.exports = missingNumber
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const missingNumber = require('./solution')
2+
3+
test('Example 1', () => {
4+
const nums = [3, 0, 1]
5+
const result = missingNumber(nums)
6+
7+
expect(result).toBe(2)
8+
})
9+
10+
test('Example 2', () => {
11+
const nums = [0, 1]
12+
const result = missingNumber(nums)
13+
14+
expect(result).toBe(2)
15+
})
16+
17+
test('Example 3', () => {
18+
const nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]
19+
const result = missingNumber(nums)
20+
21+
expect(result).toBe(8)
22+
})
23+
24+
test('Example 4', () => {
25+
const nums = [0]
26+
const result = missingNumber(nums)
27+
28+
expect(result).toBe(1)
29+
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Definition for singly-linked list.
2+
function ListNode(val, next) {
3+
this.val = val === undefined ? 0 : val
4+
this.next = next === undefined ? null : next
5+
}
6+
7+
// const ListNode = require('../../utils/singly-linked-lists')
8+
9+
/**
10+
* @param {ListNode} head
11+
* @param {number} k
12+
* @return {ListNode}
13+
*/
14+
var swapNodes = function (head, k) {
15+
// lets grab the length of the list
16+
// to determine the distanct from the Kth node (firstNode) to endNode\
17+
18+
let listLength = 0
19+
let currentHead = head
20+
21+
// find the length of the linkedlist
22+
while (currentHead !== null) {
23+
listLength++
24+
currentHead = currentHead.next
25+
}
26+
27+
let frontNode = head
28+
for (let i = 1; i < k; i++) {
29+
frontNode = frontNode.next
30+
}
31+
32+
let endNode = head
33+
for (let i = 1; i <= listLength - k; i++) {
34+
endNode = endNode.next
35+
}
36+
37+
// attemp swap
38+
let temp = frontNode.val
39+
frontNode.val = endNode.val
40+
endNode.val = temp
41+
42+
return head
43+
}
44+
45+
module.exports = swapNodes
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { deserialize, serialize } = require('../../utils/singly-linked-lists')
2+
const swapNodes = require('./solution')
3+
4+
function getNode(head, k) {
5+
let node = head
6+
while (node.val !== k) {
7+
node = node.next
8+
}
9+
return node
10+
}
11+
12+
test('Example 1', () => {
13+
const head = deserialize([1, 2, 3, 4, 5])
14+
const k = 2
15+
swapNodes(head, k)
16+
expect(serialize(head)).toStrictEqual([1, 4, 3, 2, 5])
17+
})

0 commit comments

Comments
 (0)