Home > Web Front-end > JS Tutorial > Sharing CodeWars Challenge Solutions: Least Larger, from Basic to Aris

Sharing CodeWars Challenge Solutions: Least Larger, from Basic to Aris

Susan Sarandon
Release: 2024-12-21 07:09:09
Original
287 people have browsed it

Sharing Solusi Tantangan CodeWars: Least Larger, dari Dasar hingga aris

Learn from the CodeWars Challenge: Least Larger

Hello, friends! This time I want to share about the Least Larger challenge from CodeWars. This challenge is really exciting because it trains logic and systematic thinking, as well as giving an idea of ​​how this concept can be useful in the real world. Come on, let's discuss it together!


What are the Challenges?

So, we are given an array of numbers and a certain index. Our task is to find the smallest number that is greater than the element at that index, then return the index. If there are no matching numbers, we must return -1.

Example:

Array: [4, 1, 3, 5, 6]

Index: 0 (reference number is 4)

The result is 3, because the smallest number greater than 4 is 5, which is at index 3.
why can it be 3?
The array starts from 0
so:
4 : index 0
1 : index 1
3 : index 2
5 : index 3
6 : index 4


The Solution I Created

To solve this challenge, I created a function called leastLarger. Here's the code:

function leastLarger(a, i) {
  let smaller = Infinity; // Nilai pembanding awal
  let result = -1;        // Default hasil kalau nggak ada elemen yang memenuhi

  for (let index = 0; index < a.length; index++) {
    if (a[index] > a[i] && a[index] < smaller) {
      smaller = a[index]; // Update nilai terkecil yang lebih besar
      result = index;     // Simpan indeks elemen
    }
  }

  return result; // Kembalikan hasil
}
Copy after login
Copy after login

How does it work?

  1. Initialization

    • I set smaller to Infinity as the initial value for comparison.
    • result is set to -1 in case there are no satisfying elements.
  2. Array Iteration

    • I check every element in the array.
    • There are two conditions that must be met:
      1. The element is greater than the reference element (a[i]).
      2. The element is smaller than the previous comparison (smaller).
    • If both conditions are met, I update smaller and save the element index in the result.
  3. Returning Results

    • If there is a satisfying element, the function returns its index.
    • If there is none, the result is still -1.

Usage Example

console.log(leastLarger([4, 1, 3, 5, 6], 0)); // Output: 3
Copy after login

Explanation:

  • The reference element is 4 (at index 0).
  • Elements greater than 4 are [5, 6].
  • Of those elements, the smallest is 5, and its position is at index 3.

Advance Solution: Concise in 1 Line

For those of you who like a more "JavaScript-only" approach, we can write this solution in one line of code using built-in methods like filter, Math.min, and findIndex:

function leastLarger(a, i) {
  let smaller = Infinity; // Nilai pembanding awal
  let result = -1;        // Default hasil kalau nggak ada elemen yang memenuhi

  for (let index = 0; index < a.length; index++) {
    if (a[index] > a[i] && a[index] < smaller) {
      smaller = a[index]; // Update nilai terkecil yang lebih besar
      result = index;     // Simpan indeks elemen
    }
  }

  return result; // Kembalikan hasil
}
Copy after login
Copy after login

This code does the same thing, but in a more functional and concise style. Great for situations where you want to write a quick solution without too many additional variables.


Real World Case Studies

A function like this is actually quite relevant in various real situations. Some examples:

  1. Ticket Booking System

    • In ticket systems, we often need to find the lowest seat price that is higher than a certain price (for example, to fill the next seat).
  2. Task Scheduling

    • When making a schedule, we can look for the next closest time that is greater than a certain time, for example to allocate meeting slots or the next task.
  3. Inventory Management

    • In a warehouse, if you need to look for a storage location with the smallest capacity that is still sufficient to store certain items.

This challenge looks simple, but when I tried it, I realized that neat logic is very important. What I like about this challenge is how relevant this concept is to apply in the real world.

Oh yes, if friends have other ways to complete this challenge, don't hesitate to share them in the comments column, OK! Who knows, we might learn from each other's different approaches. Hopefully this sharing is useful, and happy coding! ?

The above is the detailed content of Sharing CodeWars Challenge Solutions: Least Larger, from Basic to Aris. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template