Skip to content

Commit 1f9adf1

Browse files
committed
Add solution #1492
1 parent af3312c commit 1f9adf1

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
1475|[Final Prices With a Special Discount in a Shop](./1475-final-prices-with-a-special-discount-in-a-shop.js)|Easy|
172172
1480|[Running Sum of 1d Array](./1480-running-sum-of-1d-array.js)|Easy|
173173
1486|[XOR Operation in an Array](./1486-xor-operation-in-an-array.js)|Easy|
174+
1492|[The kth Factor of n](./1492-the-kth-factor-of-n.js)|Medium|
174175
1496|[Path Crossing](./1496-path-crossing.js)|Easy|
175176
1598|[Crawler Log Folder](./1598-crawler-log-folder.js)|Easy|
176177
1780|[Check if Number is a Sum of Powers of Three](./1780-check-if-number-is-a-sum-of-powers-of-three.js)|Medium|

solutions/1492-the-kth-factor-of-n.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 1492. The kth Factor of n
3+
* https://leetcode.com/problems/the-kth-factor-of-n/
4+
* Difficulty: Medium
5+
*
6+
* Given two positive integers n and k.
7+
*
8+
* A factor of an integer n is defined as an integer i where n % i == 0.
9+
*
10+
* Consider a list of all factors of n sorted in ascending order, return
11+
* the kth factor in this list or return -1 if n has less than k factors.
12+
*/
13+
14+
/**
15+
* @param {number} n
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
var kthFactor = function(n, k) {
20+
for (let i = 1; i <= n; i++) {
21+
if (n % i === 0) {
22+
k--;
23+
}
24+
if (!k) return i;
25+
}
26+
return -1;
27+
};

0 commit comments

Comments
 (0)