-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add typescript solution to lc problem: No.2571 (#1894)
No.2571.Minimum Operations to Reduce an Integer to 0
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
solution/2500-2599/2571.Minimum Operations to Reduce an Integer to 0/Solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function minOperations(n: number): number { | ||
let [ans, cnt] = [0, 0]; | ||
for (; n; n >>= 1) { | ||
if (n & 1) { | ||
++cnt; | ||
} else if (cnt) { | ||
++ans; | ||
cnt = cnt === 1 ? 0 : 1; | ||
} | ||
} | ||
if (cnt === 1) { | ||
++ans; | ||
} else if (cnt > 1) { | ||
ans += 2; | ||
} | ||
return ans; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters