Day - 69

Day - 69

Assalamu alaykum, everyone! How are you dears?


We have about 31 days to complete the challenge. I am glad to share solutions everyday! This challenge helps not only to gain new knowledge, but also to make new acquaintances! I want this benefit not only for myself but also for you. You can also create your own challenges and achieve even higher goals.


So, let's start coding!


  1. Problem 1046. Last Stone Weight

Time Complexity:

The time complexity of this code is O(n log n), where n is the number of stones. This is because the while loop runs at most n times, and each time it runs, the Math.max function is called, which has a time complexity of O(log n)

Space Complexity:

The space complexity of this code is O(n), because the stones array is always stored in memory.

Code:

/*
?* @param {number[]} stones
?* @return {number}
?*/

var lastStoneWeight = function (stones) {
? while (stones.length > 1) {
? ? let max1 = Math.max(...stones);
? ? stones.splice(stones.indexOf(max1), 1);
? ? let max2 = Math.max(...stones);
? ? stones.splice(stones.indexOf(max2), 1);
? ? if (max1 !== max2) stones.push(Math.abs(max1 - max2));
? }
? return stones[0] || 0;
};        


That's all about today's challenge! It is time to share the post with your friends!


Thanks for your attention!


#coding #challenges #leetcode #javascript #typescript #100daysofcodechallenge #problems #solutions #like #share

要查看或添加评论,请登录

Abbosbek Sulaymonov的更多文章

  • Polyfills in JavaScript

    Polyfills in JavaScript

    Assalamu alaykum, everyone! I haven't been able to write anything for a long time. I hope you will forgive me for this.

  • Day - 100

    Day - 100

    Assalamu alaykum, everyone! And finally, today is the last day of my 100-day challenge. Getting to this day has not…

    2 条评论
  • Day - 99

    Day - 99

    Assalamu alaykum, everyone! How are you guys? It is the 99th day of my coding challenge! So, I am going to post a good…

    1 条评论
  • Day - 98

    Day - 98

    Assalamu alaykum, everyone! It is time to share the 98th day of my coding challenge! These days, I am solving problems…

  • Day - 97

    Day - 97

    Assalomu alaykum, everyone! It is the 97th day! I am glad to see you here! Today I am going to show a solution for…

    1 条评论
  • Day - 96

    Day - 96

    Assalamu alaykum, everyone! It is the 96th day of my coding challenge! We passed 96 days ). It is a great result! I…

  • Day - 95

    Day - 95

    Assalamu alaykum, everyone! Today, I am going to share the 95th day of my coding challenge! So, let's start the coding!…

  • Day - 94

    Day - 94

    Assalamu alaykum, everyone! It is the 94th day of my coding challenge! I do not know how to describe my thoughts! So…

  • Day - 93

    Day - 93

    Assalomu alaykum, everyone! I am glad to see you here! It is the 93rd day of my coding challenge! So, let's start the…

  • Day - 92

    Day - 92

    Assalamu alaykum, everyone! Day by day I see these actions are giving the result! It is the 92nd day of my coding…

社区洞察

其他会员也浏览了